By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Why this code is not working properly

P: 51
Hi!!!!!!! could you please tell me why this code in not working properly, the thing is that when I read one string after another, the second one is not read, and I don't know why, I am using Microsoft Visual c++, here is the code:

#include<stdio.h>
#include <cctype>
#include <iostream>
#include <string>
using std::string;
using namespace std;



int main(){
int x;
string a,b;

cout << "Enter your first name: " << endl;
cout.flush();
getline(cin,a);
cin.clear();

cout << "Enter your surname: " << endl;
cout.flush();
getline(cin,b);
cin.clear();

cout << "Enter your age: " << endl;
cout.flush();
cin >> x;

cout << "Your Name is: " << a << endl;
cout << "Your Surname is: " << b << endl;
cout << "Your age is: " << x << endl;


return 0;

}

And this is the output:

Enter your first name:
Edwin

Enter your surname:
Alvear
Enter your age:
Your Name is: Edwin
Your Surname is:
Your age is: -858993460
Press any key to continue

Thanks for your help. . . .
Sep 21 '06 #1
Share this Question
Share on Google+
4 Replies


100+
P: 293
D_C
I replied to a similar topic, I think it may have been your's too.

When you type in the first name, then press enter, you take the first name, but leave the "enter keystroke" in cin. You should flush() cin instead of cout.
Sep 21 '06 #2

100+
P: 144
I've replied to your earlier post. The cin stream doesn't have a flush function. It only has a clear function for clearing leftovers out of the buffer. A correct implementation of getline by default uses the '\n' character as a delimeter and discards that delimeter after reading all characters up to that delimeter. Your code is logically correct, and should work. If you insist on using cin for input my only recommendation it to try a different compiler.
Sep 21 '06 #3

100+
P: 144
One last gimmick you may try. It may be possible that cin.clear() is not working correctly either. Try replacing it with cin.ignore() to remove the leftover '\n' from the input buffer.
Sep 21 '06 #4

P: 51
Thanks for your help, now my code is working good, I did all modification that you told and now it is working good, this the woking code:

#include<stdio.h>
#include <cctype>
#include <iostream>
#include <string>
using std::string;
using namespace std;



int main(){
int x;
string a,b;
cin.clear();

cout << "Enter your first name: " << endl;
cin.clear();
getline(cin,a, '\n');

cout << "Enter your surname: " << endl;
cin.ignore();
getline(cin,b, '\n');


cout << "Enter your age: " << endl;
cin.ignore();
cin >> x;

cout << "Your Name is: " << a << endl;
cout << "Your Surname is: " << b << endl;
cout << "Your age is: " << x << endl;


return 0;

}

output:
Enter your first name:
Edwin Alexis

Enter your surname:
Alvear Candelo

Enter your age:
22
Your Name is: Edwin Alexi
Your Surname is: Alvear C
Your age is: 22
Press any key to continue

Thanks for helping me.
Sep 22 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.