For loop problems.

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

#define num 3
string decorate = "####################";
int main()
{

struct aboutme {
string place_born;
int year;
int age;
string name;
} people [num];

for(int i = 0; i < num; i++)
{
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;
}



for(int b = 0; b < num; b++)
{
cout << decorate << endl << endl;
cout << "You name is: " << people[b].name << endl;
cout << "You were born in " << people[b].place_born << " on " << people[b].year << ".\n";
cout << "Your age is: " << people[b].age << endl << endl;
cout << decorate << endl << endl;
}

cout << "Program is exiting.\n";
return 0;
}

Last edited on
does the loop execute properly on the first iteration?
Yes it does, dwinters.
closed account (jwC5fSEw)
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?
OK, I see what you're saying, but isn't the getline() function only for strings?
Yes. The problem is likely that if any input is left over from a cin>> read then getline() will read that, seemingly "skipping" over that read.
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
using namespace std;

//it would be better to use the "const" keyword
//to define constants, rather than the macro "#define"
const int 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;
}
Not if there was more than one character left in the stream, as cin.get() reads only one character.
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
Last edited on
thanks roshi. A master, you are.
Topic archived. No new replies allowed.