| | |
need help problem with cin.getline method
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2004
Posts: 11
Reputation:
Solved Threads: 0
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;
}
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;
}
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 
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.

C++ Syntax (Toggle Plain Text)
#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; }
"Study the past if you would define the future" - Confucius
•
•
Join Date: Oct 2003
Posts: 21
Reputation:
Solved Threads: 2
•
•
•
•
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)
#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
C++ Syntax (Toggle Plain Text)
#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; }
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.
Hope it helps.
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)
#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; }
"Study the past if you would define the future" - Confucius
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
![]() |
Similar Threads
- problem with gets() and cin.getline() in linux (C++)
- need help problem with cin.getline method (C++)
Other Threads in the C++ Forum
- Previous Thread: fatal error C1010
- Next Thread: Delete a .cpp file
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






