It seems like my for loop is running through code when it should wait.
Could someone look at it & tell me why it's doing this?
Extra caveats:
The problem is on the line 17 for loop.
I think the getline() function I'm using could be running through before ready causing the program to jump ahead of schedule.
Is there some keyword I put before I use the getline() function to keep it from running before I put input in?
I had a problem, and someone told me it had to do with mixing cin and getline. I switched them all to cin, and it worked. Try switching them all to getline?
the problem is that the last cin command ("cin >> people[i].age;") leaves a '\n' char in the input buffer. Then when "getline(cin, people[i].place_born);" is executed sees this '\n' and assumes that the user pressed enter, so it makes people[i].place_born an empty string and moves to the next getline command. If you add a "cin.get();" command at the end of your loop to get rid of that '\n' it will be ok. Here is a working version:
#include <iostream>
#include <string>
usingnamespace std;
//it would be better to use the "const" keyword
//to define constants, rather than the macro "#define"
constint num=3;
const string decorate = "####################";
//it's a good idea to make struct definitions public
struct aboutme
{
string place_born;
int year;
int age;
string name;
} people [num];
int main()
{
int i;
for(i = 0; i < num; i++)
{
system("cls");
cout << "Where were you born:";
getline(cin, people[i].place_born);
cout << "What's your name?";
getline(cin, people[i].name);
cout << "What year were you born:";
cin >> people[i].year;
cout << "How old are you?";
cin >> people[i].age;
cin.get(); //<- this will make your loop work smoothly :D
}
for(i = 0; i < num; i++)
{
system("cls");
cout << decorate << endl << endl;
cout << "You name is: " << people[i].name << endl;
cout << "You were born in " << people[i].place_born << " on " << people[i].year << ".\n";
cout << "Your age is: " << people[i].age << endl << endl;
cout << decorate << endl << endl;
system("pause");
}
system("cls");
cout << "Program is exiting.\n";
system("pause");
return 0;
}
you are right, but if the user gives a valid input for the age then the only char remaining would be '\n'. If the user doesn't enter a valid input for the age (he enters a very big value or a string for example) this would be another problem and if you made a topic requesting help on it, I would be very glad to provide you with an answer :D