954,148 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help problem with cin.getline method

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
#include

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;
}

steelers_fan
Newbie Poster
11 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

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 ;)

#include <iostream>
#include <string>

using namespace std;

int main()
{
char key;
using namespace std;
char 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 ";
getchar();
return 0;
}
red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

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 ;)

#include <iostream>
#include <string>

using namespace std;

int main()
{
char key;
using namespace std;
char 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 ";
getchar();
return 0;
}


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

#include <iostream>
#include <string>

using namespace std;

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

char response[256];

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

if(strlen(response) == 0)
{
cout << "\nYou must tell me your name...";
return main();
}

cout << "It's nice to meet you, " << response;
cout << "\ntype a key and hit return ";
getchar();
return 0;
}
xlulux
Newbie Poster
21 posts since Oct 2003
Reputation Points: 14
Solved Threads: 3
 

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.

#include <iostream>
#include <string>

using namespace std;

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

char response[256];

cout << "\n\nDO YOU NEED TO CALCULATE FULLTIME OR PARTIME EXPENSES? ";
cin >>FullTimePartTime; 
fflush(stdin);
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 ";
getchar();
return 0;
}

Hope it helps.

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

Anyone who recommends fflush(stdin) is awarded instant newbie status. It is not correct , and there are other ways to accompish its intended effect .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Greetings.
Sorry if I was wrong. ;)
steerles_fan, sorry for giving the wrong advice.
Dave, maybe you could advise steerles_fan on this?

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

Thank you for your response it was very helpful

steelers_fan
Newbie Poster
11 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You