Ferrill Rushton
Ferrill Rushton

Reputation: 11

My while loop goes into it's second iteration before my switch statement executes completely, I'm coding in c++

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void ReadFile()
{

    string fileName;
    ifstream inFile;

    cout << "Please enter the password for the external file: ";
    getline(cin, fileName);

    inFile.open(fileName);

}//End of ReadFile() function


int main()
{
    vector<string> studentName, studentNumber, studentClass;
    char option;

    while (option != 'g' && option != 'G')
    {

        cout << "\t\t\t" << "Student List Menu\n\n";
        cout << "A.  Reading the Student List from a file\n";
        cout << "B.  Adding Student's Informations into the Student List\n";
        cout << "C.  Displaying the content of the Student List\n";
        cout << "D.  Sorting and Displaying the content of the Student List\n";
        cout << "E.  Writing the Student List to a file\n";
        cout << "F.  Searching for a Student's Information from the Student List\n";
        cout << "G.  Ending the program\n\n";
        cout << "Please enter an option:     ";
        cin >> option;
        cout << endl;

        switch (option)
        {

            case 'A':
            case 'a':
                ReadFile();
                break;

            case 'B':
            case 'b':
                break;

            case 'C':
            case 'c':
                break;

            case 'D':
            case 'd':
                break;

            case 'E':
            case 'e':
                break;

            case 'F':
            case 'f':
                break;

            case 'G':
            case 'g':
                cout << "Thank you for using the program!";
                break;

            default: cout << "Invalid option choice\n\n";

        }

    }

    return 0;
}//End of main function

When I select option 'A', the switch statement calls the ReadFile() function, but when it asks for the "password" (the file name) to be entered, "Student List Menu" is read, which I think means the do-while loop continued to run while executing the ReadFile function, so it read the input until the newline character. What can I do to get it to run the option first, then continue through the do-while loop?

Upvotes: 1

Views: 155

Answers (3)

R Sahu
R Sahu

Reputation: 206697

When you type

a

in your terminal and press Enter, two characters entered in the input stream: a and '\n'.

When you use

cin >> option;

on such an input stream, 'a' is read first. The newline character is still in the input stream.

Then you call ReadFile(), which calls getline(cin, fileName). That call gets an empty string since the newline character is still there in the input stream -- it doesn't wait for you to enter a filename. After that, there is nothing left in the input stream. Also, ReadFile() returns. That's why you see the student menu.

The fix to the problem is to ignore the rest of the line after option is read.

cin >> option;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Upvotes: 2

ExceptionalHandler
ExceptionalHandler

Reputation: 72

replacegetline(cin, fileName);

with cin>> fileName;

Upvotes: 1

Yasir Majeed
Yasir Majeed

Reputation: 741

The bug in your code is due to the line

getline(cin, fileName);

You must use

std::cin.getline(fileName,name length);

Upvotes: 0

Related Questions