473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why this code is not working properly

51 New Member
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 2789
D_C
293 Contributor
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 New Member
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 New Member
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
candexis
51 New Member
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
1995
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 "\nWelcome to the Temperature program" while True: low1 = raw_input("Please enter a low temperature: ").upper() if low1 != 'I QUIT': low = int(low1)
6
2916
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 set up my pages for access directly via localhost (not using the WebMatrix server). My login page loads, and any validation controls on the page work fine, so I know it's recognizing the ASP.Net code there, however, when I try to log on, the...
171
7692
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) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
17
2694
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 know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
4
2108
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 project. Now I have to get my plugin working with the newer version of the software. When I start the debugger with it aimed at that program and my program installed, the program doesn't run properly unless I have the Working Directory set to...
9
5296
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"> //--------------- LOCALIZEABLE GLOBALS --------------- var d=new Date();
3
2005
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" type="text/javascript"> function call() { var box=new Array(50);
7
2216
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 more information i m giving the whole code below. please give me the solution, the code is executing upto line no 95 (echo $halfday;) and after this line not a single code is executing. i am not able to get solution. So plaease help me in solving...
8
6327
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. Just to get it to work, I hard-coded the username and password of a SQL Server account in the connectionstring in web.config. Once I confirmed that this worked on the web server, I wanted to remove the hard-coded password from web.config, so I...
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.