943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7842
  • C++ RSS
Jul 28th, 2004
1

need help problem with cin.getline method

Expand Post »
Hello, everyone I am pretty much a new programmer programming a little in C++ and
basic. I am having trouble with this portion of the program that I am working on.
When I include line 16, cin >>FullTimePartTime;
the program skips line 21, cin.getline(response,256);
and does not let me enter anything for the response variable.
But when I comment or delete out line 21 the program does fall through to the
else command and prints out what was entered in the response variable.

For you experinced C++ programmers, I need to know
1) why is this skipping line 21 with line 16 included and

2) I really don't need to include the cin >>FullTimePartTIme statement, how can I
get the program to work without inluding this statement.

3) I'm trying to write my program and this is bogging me down, as I have spent the
last 3 or 4 days( a couple hours each day ) trying to fix the problem. Do you
guys think I am spending to much time on one problem or should I continue with the
program so I can write another one.


__________________________________________________________________________________
#include <iostream>
#include <string>

using namespace std;

int main()
{
char key;
using namespace std;
string FullTimePartTime;

char response[256];

cout << "\n\nDO YOU NEED TO CALCULATE FULLTIME OR PARTIME EXPENSES? ";
cin >>FullTimePartTime; //cin is placed here program skips
//cin.getline(response,256)

cout << endl;
cout << "What is your name? "; //
cin.getline(response,256);

if (strlen(response) == 0)
cout << "\nYou must tell me your name...";
else
cout << "It's nice to meet you, " << response;
cout << "\ntype a key and hit return ";
cin >> key;
return 0;
}
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
steelers_fan is offline Offline
11 posts
since Jun 2004
Jul 28th, 2004
1

Re: need help problem with cin.getline method

Greetings.
Err, I don't really get what you mean.
Quote ...
2) I really don't need to include the cin >>FullTimePartTIme statement, how can I
get the program to work without inluding this statement.
When I take those lines out, it worked just fine, as in, I could enter a name and get it displayed in return
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char key;
  9. using namespace std;
  10. char FullTimePartTime;
  11.  
  12. char response[256];
  13.  
  14. //cout << "\n\nDO YOU NEED TO CALCULATE FULLTIME OR PARTIME EXPENSES? ";
  15. //cin >>FullTimePartTime;
  16. //cin is placed here program skips
  17. //cin.getline(response,256)
  18.  
  19. cout << endl;
  20. cout << "What is your name? "; //
  21. cin.getline(response,256);
  22.  
  23. if (strlen(response) == 0)
  24. cout << "\nYou must tell me your name...";
  25. else
  26. cout << "It's nice to meet you, " << response;
  27. cout << "\ntype a key and hit return ";
  28. getchar();
  29. return 0;
  30. }
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 28th, 2004
1

Re: need help problem with cin.getline method

Quote originally posted by red_evolve ...
Greetings.
Err, I don't really get what you mean.

When I take those lines out, it worked just fine, as in, I could enter a name and get it displayed in return
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char key;
  9. using namespace std;
  10. char FullTimePartTime;
  11.  
  12. char response[256];
  13.  
  14. //cout << "\n\nDO YOU NEED TO CALCULATE FULLTIME OR PARTIME EXPENSES? ";
  15. //cin >>FullTimePartTime;
  16. //cin is placed here program skips
  17. //cin.getline(response,256)
  18.  
  19. cout << endl;
  20. cout << "What is your name? "; //
  21. cin.getline(response,256);
  22.  
  23. if (strlen(response) == 0)
  24. cout << "\nYou must tell me your name...";
  25. else
  26. cout << "It's nice to meet you, " << response;
  27. cout << "\ntype a key and hit return ";
  28. getchar();
  29. return 0;
  30. }



you need a semicolon on line 17 and heres a little better version of your code


C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. char key;
  10. using namespace std;
  11. char FullTimePartTime;
  12.  
  13. char response[256];
  14.  
  15. cout << "What is your name? "; //
  16. cin.getline(response,256);
  17.  
  18. if(strlen(response) == 0)
  19. {
  20. cout << "\nYou must tell me your name...";
  21. return main();
  22. }
  23.  
  24. cout << "It's nice to meet you, " << response;
  25. cout << "\ntype a key and hit return ";
  26. getchar();
  27. return 0;
  28. }
Reputation Points: 14
Solved Threads: 2
Newbie Poster
xlulux is offline Offline
21 posts
since Oct 2003
Jul 28th, 2004
0

Re: need help problem with cin.getline method

Greetings.
Umm, refering to the first post, I'd like to make some ammendments.
If you'd like to let's say maintain cin>>FullTimePartTime;
just before a cin.getline(...), you should clear your input buffer first.
Use fflush(stdin); to do that, and your cin.getline will not skip.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char key;
  9. using namespace std;
  10. char FullTimePartTime;
  11.  
  12. char response[256];
  13.  
  14. cout << "\n\nDO YOU NEED TO CALCULATE FULLTIME OR PARTIME EXPENSES? ";
  15. cin >>FullTimePartTime;
  16. fflush(stdin);
  17. cout << endl;
  18. cout << "What is your name? ";
  19. cin.getline(response,256);
  20.  
  21. if (strlen(response) == 0)
  22. cout << "\nYou must tell me your name...";
  23. else
  24. cout << "It's nice to meet you, " << response;
  25. cout << "\ntype a key and hit return ";
  26. getchar();
  27. return 0;
  28. }
Hope it helps.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 29th, 2004
0

Re: need help problem with cin.getline method

Anyone who recommends fflush(stdin) is awarded instant newbie status. It is not correct, and there are other ways to accompish its intended effect.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 29th, 2004
0

Re: need help problem with cin.getline method

Greetings.
Sorry if I was wrong.
steerles_fan, sorry for giving the wrong advice.
Dave, maybe you could advise steerles_fan on this?
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Aug 5th, 2004
0

Re: need help problem with cin.getline method

Thank you for your response it was very helpful
Reputation Points: 11
Solved Threads: 0
Newbie Poster
steelers_fan is offline Offline
11 posts
since Jun 2004

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: fatal error C1010
Next Thread in C++ Forum Timeline: Delete a .cpp file





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


Follow us on Twitter


© 2011 DaniWeb® LLC