Data Structure issue

Greetings,

I was trying out data structures, but I ran into the following issue:
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
#include <iostream>

struct person{
    std::string name;
    short age;
} person1, person2;

void askinfo1();
void askinfo2( person p );

int main(){
    askinfo1();
    askinfo2( person2 );
    
    std::cout << person1.name << " (" << person1.age << ")" << std::endl;
    std::cout << person2.name << " (" << person2.age << ")" << std::endl;
}

void askinfo1(){
    std::cout << "What is the first person's name? ";
    getline(std::cin, person1.name);
    std::cout << "How old is " << person1.name << "? ";
    std::cin >> person1.age;
}

void askinfo2( person p ){
    std::cout << "What is the second person's name? ";
    getline(std::cin, p.name);
    std::cout << "How old is " << p.name << "? ";
    std::cin >> p.age;
}

Running the program:
What is the first person's name? John Lemon
How old is John Lemon? 93
What is the second person's name? How old is ? test
John Lemon (93)
 (0)

RUN SUCCESSFUL (total time: 13s)

Normally, it should ask for the second person's name, and then ask for his/her age, just like with the first person. However, it skips both name and age of the second person.

Help will be appreciated.
getline reads up to endline and discards that endline. What about cin>>?

You type a number and hit Enter. cin>> grabs that number, but newline remains. Following getline reads all the 0 characters preceding that newline, and "test" is not short. Thus, you should do something for cin before getline (proper syntax eludes me).


You do have other issues too.
* Line 6 seems to introduce two global variables. Avoid globals.
* askinfo2 takes argument by-value. p is a mere local copy that destructs itself at the end of that function. person2 is not modified.
Thank you for your response, this is what I came up with:
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
#include <iostream>

struct person{
    std::string name;
    short age;
} person1, person2;
void clearbuffer();
void askinfo1();
void askinfo2( person& p );

int main(){
    askinfo1();
    clearbuffer();
    askinfo2( person2 );
    
    std::cout << std::endl << person1.name << " (" << person1.age << ")" << std::endl;
    std::cout << person2.name << " (" << person2.age << ")" << std::endl;
}

void clearbuffer(){
    std::string temp;
    getline( std::cin, temp);
}

void askinfo1(){
    std::cout << "What is the first person's name? ";
    getline(std::cin, person1.name);
    std::cout << "How old is " << person1.name << "? ";
    std::cin >> person1.age;
}

void askinfo2( person& p ){
    std::cout << "What is the second person's name? ";
    getline(std::cin, p.name);
    std::cout << "How old is " << p.name << "? ";
    std::cin >> p.age;
}


So now, it clears the input buffer after executing the function askinfo1, meaning that the second getline doesn't get values from previous input. I also passed person2 as a reference to askinfo2.
What is the first person's name? John Hackarow
How old is John Hackarow? 43
What is the second person's name? Gobu Brohan
How old is Gobu Brohan? 12

John Hackarow (43)
Gobu Brohan (12)

RUN SUCCESSFUL (total time: 13s)

But, why shouldn't one use global variables?
Topic archived. No new replies allowed.