I’m having a problem trying to getline lines from a .txt file. I used a similar algorithm in another piece of code and document with seemingly no issues (= all lines were printed). However, when I try to the same thing using getline (As usual) i get my string line = “”. My lines are not empty and I have a pre-test to check if the file opens, which it does. Below is a function from my program. Could anyone provide some help? Thanks!
I am trying to run case ‘A’
void Database::LoginMenu(string code)
{
cout << endl;
cout << "Thank You for Login In, Would You Like To..." << endl;
cout << "====================================================" << endl;
cout << "A. View your registration information" << endl;
cout << "B. Search for others" << endl;
cout << "C. Search for Upcoming" << endl;
cout << "D. Enter the Database" << endl;
char choice, YN;
cout << endl;
cout << "Please choose an option: ";
cin >> choice;
switch(choice)
{
case 'A':
{
ifstream myfile("RegistrationInfo.txt");
string line;
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(cin, line);
if(line[5] == ':')
{
line = line.substr(7, line.size());
}
if(line == code)
{
while(!line.empty())
{
cout << line << endl;
}
}
}
}
cout << "Would you like to return to the menu? (Y/N)";
cin >> YN;
if(YN == 'Y')
{
LoginMenu(code);
}
myfile.close();
}
break;
case 'B':
{
ifstream filein("RegistrationInfo.txt");
string temp, YN, enteredInfo;
int n = 0;
cout << "Who do you wish to search for? :";
cin >> enteredInfo;
while(!filein.eof())
{
getline(cin, temp);
}
}
break;
case 'C': upcomingComps();
break;
case 'D': DatabaseMenu();
break;
}
}
Here are a few lines form my txt file.
- CCode: abd50896
- Names: TomMullen NicoleLaBerge
- Compt: Rutgers DanceSport 2012
- Level: Newcomer
- Dance: Tango Waltz Quickstep Rumba ChaCha Jive
Suggest the call use myfile as the first argument, instead of cin —
getline(myfile, line)— because cin represents stdin, which is not the file you opened.