| | |
cin.getline() question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 8
Reputation:
Solved Threads: 0
Well, once again, hello. I'm trying to figure out why when i use 2 cin.getlines after eachother it skips one of them and I cant use it. Take a look if you will:
for some reason when I run the program and goto this function it will skip the first cin.getline and only let me type for the cin.getline that gets the Password. It's wierd because I ran a simple program similar to it, and it works fine:
This code doesnt skip any cin.getline()'s for some reason, could it not work in the other one because of the file i/o? Maybe its making it act wierd? Any help is appreciated, thanks in advance.
C++ Syntax (Toggle Plain Text)
void Join() { char* Username = new char[255]; char* Password = new char[255]; ofstream DataBase("DataBase.txt", ios::app); cout << "Welcome New Member, Please Enter a New Username and Password:\n\n"; cout << "Username: "; cin.getline(Username, 255); DataBase << Username << "\n"; cout << "\nPassword: "; cin.getline(Password, 255); DataBase << Password << "\n\n"; cout << "\nSaved To Database!\n"; DataBase.close(); delete [] Username; delete [] Password; }
for some reason when I run the program and goto this function it will skip the first cin.getline and only let me type for the cin.getline that gets the Password. It's wierd because I ran a simple program similar to it, and it works fine:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <conio.h> using namespace std; int main (int argc, char* argv[]) { char* string1 = new char[255]; char* string2 = new char[255]; cout << "String1: "; cin.getline(string1, 255); cout << "String2: "; cin.getline(string2, 255); cout << "\nString1 = " << string1 << endl; cout << "String2 = " << string2 << endl; delete[] string1; delete[] string2; getch(); return 0; }
This code doesnt skip any cin.getline()'s for some reason, could it not work in the other one because of the file i/o? Maybe its making it act wierd? Any help is appreciated, thanks in advance.
Before the call to Join(), is there another function that leaves unread input (such as a trailing newline) in
cin ? "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: May 2009
Posts: 8
Reputation:
Solved Threads: 0
I don't think so, I just use a switch/case which then calls it and thats all, heres the complete code:
Thanks for replying.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; char* Username = new char[255]; char* Password = new char[255]; char* User = new char[255]; void Join() { ofstream DataBase("DataBase.txt", ios::app); cout << "Welcome New Member, Please Enter a New Username and Password:\n\n"; cout << "Username: "; cin.getline(Username, 255); DataBase << Username << "\n"; cout << "\nPassword: "; cin.getline(Password, 255); DataBase << Password << "\n\n"; cout << "\nSaved To Database!\n"; DataBase.close(); delete [] Username; delete [] Password; } void Login() { ifstream DataBase("DataBase.txt"); cout << "Welcome Member, Please Enter Your Username and Password:\n\n"; cout << "Username: "; cin >> User; DataBase >> Username; if (Username==User) cout << "\nUsername Correct!"; else cout << "\nUsername Incorrect!"; delete [] User; delete [] Username; delete [] Password; } void About() { } int main(int argc, char *argv[]) { int option; cout << "DataBase v1.00 BETA!\n\n"; cout << "1.Login\n"; cout << "2.Sign-Up\n"; cout << "3.About\n"; cout << "4.Exit\n\n"; cout << "Option(1-4): "; cin >> option; switch (option) { case 1: Login(); break; case 2: Join(); break; case 3: About(); break; case 4: break; default: cout << "Not An Option!"; break; } system("PAUSE"); return EXIT_SUCCESS; }
Thanks for replying.
How about in you use Login() where you use cin instead of getline?
C++ Syntax (Toggle Plain Text)
cin >> User;
Last edited by Dave Sinkula; May 13th, 2009 at 12:11 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
I don't think so, I just use a switch/case which then calls it and thats all, heres the complete code:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; char* Username = new char[255]; char* Password = new char[255]; char* User = new char[255]; void Join() { ofstream DataBase("DataBase.txt", ios::app); cout << "Welcome New Member, Please Enter a New Username and Password:\n\n"; cout << "Username: "; cin.getline(Username, 255); DataBase << Username << "\n"; cout << "\nPassword: "; cin.getline(Password, 255); DataBase << Password << "\n\n"; cout << "\nSaved To Database!\n"; DataBase.close(); delete [] Username; delete [] Password; } void Login() { ifstream DataBase("DataBase.txt"); cout << "Welcome Member, Please Enter Your Username and Password:\n\n"; cout << "Username: "; cin >> User; DataBase >> Username; if (Username==User) cout << "\nUsername Correct!"; else cout << "\nUsername Incorrect!"; delete [] User; delete [] Username; delete [] Password; } void About() { } int main(int argc, char *argv[]) { int option; cout << "DataBase v1.00 BETA!\n\n"; cout << "1.Login\n"; cout << "2.Sign-Up\n"; cout << "3.About\n"; cout << "4.Exit\n\n"; cout << "Option(1-4): "; cin >> option; switch (option) { case 1: Login(); break; case 2: Join(); break; case 3: About(); break; case 4: break; default: cout << "Not An Option!"; break; } system("PAUSE"); return EXIT_SUCCESS; }
Thanks for replying.
C++ Syntax (Toggle Plain Text)
cin >> User;
C++ Syntax (Toggle Plain Text)
cin >> option;
Most probably, they would leave a newline behind, in the input stream. Hence, the first getline() used after cin would just 'eat' that newline, and wont take any input.
You should better keep habit of using cin.ignore() to flush the input stream before using getline ( or even cin; i had faced this problem in that too..tho not so sure
) C++ Syntax (Toggle Plain Text)
cin.ignore(); cin.getline();
cin.ignore() also has overloaded versions. You should check them out.
Bhoot
![]() |
Similar Threads
- cin.getline problem (C++)
- need help problem with cin.getline method (C++)
- cin.getline (C++)
- need help problem with cin.getline method (C++)
Other Threads in the C++ Forum
- Previous Thread: Disable Close button on form
- Next Thread: Function get
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






