Bug in my first project? (strings and input)

I suppose I'm learning the true definition of a 'bug.'

I guess it's just an error, mistake or otherwise incompetence that 'bugs' you so to speak.

http://i1299.photobucket.com/albums/ag70/blink3290/firstcprojectBUG_zpse06c0924.jpg


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{ int MyAge; string MyName, MyCity;


cout<<"What is your age?"<<endl;
cin>>MyAge;
cout<<"Ok. You're " << MyAge << "." <<endl;
cout<<endl;

cout<<"What is your name?"<<endl;
getline(cin, MyName);
cout<<"You're name is " << MyName << "? Ok."<<endl;
cout<<endl;

cout<<"What city do you live in?"<<endl;
cin>>MyCity;
cout<<"Oh, you live in " << MyCity << ". Ok."<<endl;
cout<<endl;

cout<<"Here's my information: "" My name is " <<MyName<<"," " I'm "<<MyAge<< " and I live in "<<MyCity<<"."<<endl;

system("Pause");
return EXIT_SUCCESS;

As you can see when I entered my name as Detective Jon Kimble it answers the second following as well using that input.

What is the cause of this problem that I have created? I imagine one way to solve it would be to specify to the person to use first middle and last name but I don't know how to set a multi-word memory spot. or would I have to make 3 strings? MyFirstN, MyMiddleN, MyLastN... and if the person only enters a first name would it just mean the other strings go unused?

Also if this is a dumb question please tell me so I can set a higher standard for myself.
Last edited on
cin grabs input from the user for as long as any white space(i.e. Spaces, newline etc) is not encountered. If you wanna grab your full name, use the getline function. Thus, having:
 
getline(cin, myname);
It is a really silly way to present your problem though. I can't see anything without making my monitor feel uncomfortable.

Edit: and I can't compile an image to test the prog.
Last edited on
I replaced it with that and it only skipped the input process and saw out that my name is simply "?"

I'm using DevCC don't know if that matters.
SORRY ABOUT THAT! here is the code



#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{ int MyAge; string MyName, MyCity;


cout<<"What is your age?"<<endl;
cin>>MyAge;
cout<<"Ok. You're " << MyAge << "." <<endl;
cout<<endl;

cout<<"What is your name?"<<endl;
getline(cin, MyName);
cout<<"You're name is " << MyName << "? Ok."<<endl;
cout<<endl;

cout<<"What city do you live in?"<<endl;
cin>>MyCity;
cout<<"Oh, you live in " << MyCity << ". Ok."<<endl;
cout<<endl;

cout<<"Here's my information: "" My name is " <<MyName<<"," " I'm "<<MyAge<< " and I live in "<<MyCity<<"."<<endl;

system("Pause");
return EXIT_SUCCESS;
Add cin.ignore(); just above the getline to flush the buffer (At least I think that's the technical term for it). Something from MyAge got pushed into getline and it automatically cin-ed.

Obviously that's a very poor explanation, but is layman's terms.

Edit: Also use code tags please. [code] place your code here [/code"] <--- Without the quotation mark. Or highlight your code and click the <> under Format.
Last edited on
Wonderful, Olysold. Thanks for the answers!
Topic archived. No new replies allowed.