need help problem with cin.getline method

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2004
Posts: 11
Reputation: steelers_fan is an unknown quantity at this point 
Solved Threads: 0
steelers_fan steelers_fan is offline Offline
Newbie Poster

need help problem with cin.getline method

 
1
  #1
Jul 28th, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: need help problem with cin.getline method

 
1
  #2
Jul 28th, 2004
Greetings.
Err, I don't really get what you mean.
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
  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. }
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 21
Reputation: xlulux is an unknown quantity at this point 
Solved Threads: 2
xlulux xlulux is offline Offline
Newbie Poster

Re: need help problem with cin.getline method

 
1
  #3
Jul 28th, 2004
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
  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


  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: need help problem with cin.getline method

 
0
  #4
Jul 28th, 2004
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.

  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.
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 238
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help problem with cin.getline method

 
0
  #5
Jul 29th, 2004
Anyone who recommends fflush(stdin) is awarded instant newbie status. It is not correct, and there are other ways to accompish its intended effect.
"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
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: need help problem with cin.getline method

 
0
  #6
Jul 29th, 2004
Greetings.
Sorry if I was wrong.
steerles_fan, sorry for giving the wrong advice.
Dave, maybe you could advise steerles_fan on this?
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 11
Reputation: steelers_fan is an unknown quantity at this point 
Solved Threads: 0
steelers_fan steelers_fan is offline Offline
Newbie Poster

Re: need help problem with cin.getline method

 
0
  #7
Aug 5th, 2004
Thank you for your response it was very helpful
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC