943,829 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 626
  • C++ RSS
May 12th, 2009
0

cin.getline() question

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. void Join()
  2. {
  3. char* Username = new char[255];
  4. char* Password = new char[255];
  5.  
  6. ofstream DataBase("DataBase.txt", ios::app);
  7. cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
  8. cout << "Username: ";
  9. cin.getline(Username, 255);
  10. DataBase << Username << "\n";
  11. cout << "\nPassword: ";
  12. cin.getline(Password, 255);
  13. DataBase << Password << "\n\n";
  14. cout << "\nSaved To Database!\n";
  15. DataBase.close();
  16.  
  17. delete [] Username;
  18. delete [] Password;
  19. }

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)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. int main (int argc, char* argv[]) {
  7. char* string1 = new char[255];
  8. char* string2 = new char[255];
  9.  
  10. cout << "String1: ";
  11. cin.getline(string1, 255);
  12.  
  13. cout << "String2: ";
  14. cin.getline(string2, 255);
  15.  
  16. cout << "\nString1 = " << string1 << endl;
  17. cout << "String2 = " << string2 << endl;
  18.  
  19. delete[] string1;
  20. delete[] string2;
  21.  
  22. getch();
  23. return 0;
  24. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Deme is offline Offline
13 posts
since May 2009
May 12th, 2009
0

Re: cin.getline() question

Before the call to Join(), is there another function that leaves unread input (such as a trailing newline) in cin ?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 12th, 2009
0

Re: cin.getline() question

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)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. char* Username = new char[255];
  8. char* Password = new char[255];
  9. char* User = new char[255];
  10.  
  11. void Join()
  12. {
  13. ofstream DataBase("DataBase.txt", ios::app);
  14. cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
  15. cout << "Username: ";
  16. cin.getline(Username, 255);
  17. DataBase << Username << "\n";
  18. cout << "\nPassword: ";
  19. cin.getline(Password, 255);
  20. DataBase << Password << "\n\n";
  21. cout << "\nSaved To Database!\n";
  22. DataBase.close();
  23.  
  24. delete [] Username;
  25. delete [] Password;
  26. }
  27.  
  28. void Login() {
  29.  
  30. ifstream DataBase("DataBase.txt");
  31. cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
  32. cout << "Username: ";
  33. cin >> User;
  34. DataBase >> Username;
  35. if (Username==User) cout << "\nUsername Correct!";
  36. else cout << "\nUsername Incorrect!";
  37.  
  38.  
  39.  
  40. delete [] User;
  41. delete [] Username;
  42. delete [] Password;
  43. }
  44.  
  45. void About() {
  46. }
  47.  
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51.  
  52. int option;
  53.  
  54. cout << "DataBase v1.00 BETA!\n\n";
  55. cout << "1.Login\n";
  56. cout << "2.Sign-Up\n";
  57. cout << "3.About\n";
  58. cout << "4.Exit\n\n";
  59. cout << "Option(1-4): ";
  60. cin >> option;
  61.  
  62. switch (option) {
  63. case 1:
  64. Login();
  65. break;
  66. case 2:
  67. Join();
  68. break;
  69. case 3:
  70. About();
  71. break;
  72. case 4:
  73. break;
  74. default:
  75. cout << "Not An Option!";
  76. break;
  77. }
  78.  
  79.  
  80. system("PAUSE");
  81. return EXIT_SUCCESS;
  82. }

Thanks for replying.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Deme is offline Offline
13 posts
since May 2009
May 13th, 2009
0

Re: cin.getline() question

How about in you use Login() where you use cin instead of getline?
C++ Syntax (Toggle Plain Text)
  1. cin >> User;
Last edited by Dave Sinkula; May 13th, 2009 at 12:11 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 13th, 2009
0

Re: cin.getline() question

You'll have to use cin.ignore() to clean the input buffer, read this thread
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 13th, 2009
0

Re: cin.getline() question

Click to Expand / Collapse  Quote originally posted by Deme ...
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)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. char* Username = new char[255];
  8. char* Password = new char[255];
  9. char* User = new char[255];
  10.  
  11. void Join()
  12. {
  13. ofstream DataBase("DataBase.txt", ios::app);
  14. cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
  15. cout << "Username: ";
  16. cin.getline(Username, 255);
  17. DataBase << Username << "\n";
  18. cout << "\nPassword: ";
  19. cin.getline(Password, 255);
  20. DataBase << Password << "\n\n";
  21. cout << "\nSaved To Database!\n";
  22. DataBase.close();
  23.  
  24. delete [] Username;
  25. delete [] Password;
  26. }
  27.  
  28. void Login() {
  29.  
  30. ifstream DataBase("DataBase.txt");
  31. cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
  32. cout << "Username: ";
  33. cin >> User;
  34. DataBase >> Username;
  35. if (Username==User) cout << "\nUsername Correct!";
  36. else cout << "\nUsername Incorrect!";
  37.  
  38.  
  39.  
  40. delete [] User;
  41. delete [] Username;
  42. delete [] Password;
  43. }
  44.  
  45. void About() {
  46. }
  47.  
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51.  
  52. int option;
  53.  
  54. cout << "DataBase v1.00 BETA!\n\n";
  55. cout << "1.Login\n";
  56. cout << "2.Sign-Up\n";
  57. cout << "3.About\n";
  58. cout << "4.Exit\n\n";
  59. cout << "Option(1-4): ";
  60. cin >> option;
  61.  
  62. switch (option) {
  63. case 1:
  64. Login();
  65. break;
  66. case 2:
  67. Join();
  68. break;
  69. case 3:
  70. About();
  71. break;
  72. case 4:
  73. break;
  74. default:
  75. cout << "Not An Option!";
  76. break;
  77. }
  78.  
  79.  
  80. system("PAUSE");
  81. return EXIT_SUCCESS;
  82. }

Thanks for replying.
You could be facing the problem at either of these lines :
C++ Syntax (Toggle Plain Text)
  1. cin >> User;
C++ Syntax (Toggle Plain Text)
  1. 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)
  1. cin.ignore();
  2. cin.getline();

cin.ignore() also has overloaded versions. You should check them out.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
Bhoot is offline Offline
53 posts
since Oct 2008
May 13th, 2009
0

Re: cin.getline() question

Thanks i'll try cin.ignore(), and by the way, i don't think i'd want to just use cin, because when i do it doesnt allow spaces...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Deme is offline Offline
13 posts
since May 2009
May 13th, 2009
0

Re: cin.getline() question

Thanks guys, it worked like a charm.. ^_^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Deme is offline Offline
13 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Disable Close button on form
Next Thread in C++ Forum Timeline: Function get





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC