Using cin.get

Hello... I have made a project for school, just something stupid simple. (I am new to C++ programming hence why I took the class). The program I made (below) is the project. If you copy and paste it to Microsoft Visual C++ 2008 Express Edition, and run the program, it works. But if you take a look at where I am prompting the user for age (in bold at the bottom of the prompts) It HAS to be there. I had it in the middle of the prompt list and the program wouldn't execute correctly. I had it in the beginning, still wouldn't work. Finally when I moved it down there it works correctly.

My main question is why? I obviously know it works, but why does it have to be like that?

Thank you.



#include<iostream>
using namespace std;

int main()
{

const int SIZE = 100;
char college[SIZE],
name[SIZE], //users name
city[SIZE], //Home city of user
profession[SIZE], //Users profession
animal[SIZE], //animal type
pName[SIZE]; //Pets name



int age; //age of user


cout << "Please enter the following information: \n";

cout << "Your name: ";
cin.getline(name, SIZE);

cout << "Name of a city: ";
cin.getline(city, SIZE);

cout << "Name of a college: ";
cin.getline(college, SIZE);

cout << "A profession: ";
cin.getline(profession, SIZE);

cout << "A type of animal: ";
cin.getline(animal, SIZE);

cout << "A pet's name: ";
cin.getline(pName, SIZE);

cout << "Your age: ";
cin >> age;


cout << "There once was a person named " << name << " who lived in " << city << ". At the age of \n";
cout << age << ", " << name << " went to college at " << college << ". " << name << " graduated and went to work \n";
cout << "as a " << profession << ". Then, " << name << " adopted a(n) " << animal << " named " << pName << ". They \n";
cout << "both lived happily ever after!\n";

return 0;
}
Topic archived. No new replies allowed.