dimanche 29 mars 2015

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?




Aucun commentaire:

Enregistrer un commentaire