C++ program not running correctly.

This is my program in it's full extent, everything works fine until the second time I have entered my data, when I press enter at that point the program immediately runs the last line and finishes without giving me any opportunity to fill in the third data (location)

Using Code::Blocks as IDE and GNU GCC as compiler.

EDIT: I am not getting an error when I try to compile the code:
"undefined refernce to 'WinMain@16'"

Solved had written "Main" instead of "main".

a small bit of googling leads me to believe I have created the project incorrectly, I have been unable to find a tutorial on this so I have been tinkering along on my own, any help to be had on this?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string name, location;
int age;

cout << "Please enter your name:" << endl;
getline (cin, name);
cout << "Please enter your age: " << endl;
cin >> age;
cout << "Please enter your location: " << endl;
getline (cin, location);
cout << "\nYour name is: " << name << ".\nYou are " << age << " years old.\nAnd you live in " << location << '.' << endl;
}

Last edited on
Because getline checks what's on the the input stream, which in this case, is still age.

Add this before your second getline
cin.ignore();
I am pretty much following this guide, in particular there's this example:
http://www.cplusplus.com/doc/tutorial/basic_io/

// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}


I do not see the difference between that line of code and my lines of code with the exception of this example having the same name for both the inputs, suppose that's where the problem stems?

I think I understand now, the fact that I have a "cin" between the two "getline" functions buggs my program?
Last edited on
Topic archived. No new replies allowed.