473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why this code is not working properly

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
4 2775
D_C
293 100+
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
tyreld
144 100+
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
tyreld
144 100+
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
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

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

Similar topics

8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
6
by: Brian Miller | last post by:
I've been constructing an ASP.Net application using the 1.1 framework, and have been using Web Matrix for development purposes. Now that my application is near completion, I wanted to see if I can...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
4
by: qbproger | last post by:
I'm developing a plugin for some software. The previous version of the software didn't require a start in directory to be set. This allowed me to leave the working directory to the default in the...
9
by: javakid | last post by:
Hi Following form validatioin code is working fine on IE but not working on Mozilla Firefox V2. Can any body suggest? Regards <script ="JAVASCRIPT" type="text/javascript">...
3
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
7
by: robin1983 | last post by:
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For...
8
by: Brett | last post by:
I wrote an ASP.NET application that queries a SQL Server database (on a different box from the web server) and displays the result in a GridView. The datasource for the GridView is a SQLDataSource....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.