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. . . .
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
|
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...
|
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...
|
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...
|
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...
| |
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();
|
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);
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |