Cin and Structs

Hello. I'm still very new to C++ and this is one of the first programs I've tried writing on my own. I'm trying to read input from the user and store it in a member of a struct until I have filled the struct, then print that info to the console. I have my functions for the struct and printing prepared and have tested them by inputing the info in the code myself. But I really want to be able to read info into the structs in the program. I tried using cin to read strings into the name, but it won't work. The program compiles just fine, but it doesn't use the function I wrote. Here is my code I have so far. Can someone suggest a mock up function to read info in the struct members? Thanks!

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
  #include <iostream>
#include <string>

using namespace std;

struct BandMember
{
    string              strMemberName;
    unsigned int        unMemberAge;
    unsigned int        unMemberHeight; //in inches
    unsigned int        unMemberWeight; //in pounds
};

void PrintInfo(BandMember sBandMember)
{
    cout << "Name: \t \t" << sBandMember.strMemberName << endl;
    cout << "Age: \t \t" << sBandMember.unMemberAge << endl;
    cout << "Height: \t" << sBandMember.unMemberHeight << "\"" << endl;
    cout << "Weight: \t" << sBandMember.unMemberWeight << " pounds" << endl;
}

int main()
{
    PrintInfo(sCaleb);
    return 0;
}

Keeping things simple you can populate like this.

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

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

struct Band
{
	string              strMemberName;
	unsigned int        unMemberAge;
	unsigned int        unMemberHeight; //in inches
	unsigned int        unMemberWeight; //in pounds
}BandMember;


// function prototypes
void PrintInfo();
void GetBand();

int main()
{
	GetBand();
	PrintInfo();
	return 0;
}

void PrintInfo()
{
	cout << endl;
	cout << "Name: \t \t" << BandMember.strMemberName << endl;
	cout << "Age: \t \t" << BandMember.unMemberAge << endl;
	cout << "Height: \t" << BandMember.unMemberHeight << "\"" << endl;
	cout << "Weight: \t" << BandMember.unMemberWeight << " pounds" << endl;
}

void GetBand()
{
	cout << "Enter Name:  ";
	cin >> BandMember.strMemberName;
	cout << "Enter Age: ";
	cin >> BandMember.unMemberAge;
	cout << "Enter Height: ";
	cin >> BandMember.unMemberHeight;
	cout << "Enter Weight: ";
	cin >> BandMember.unMemberWeight;

	cin.sync();

}
Ok, so I take it cin.sync puts all those inputs together and stores them in the struct? Why doesn't it have a return value? Also, I think my problem before was having GetBand take the struct BandMember as parameters. I don't know why but I thought that I had to somehow pass the struct info to the GetBand function in order to fill in the information.

Details on cin.sync can be found here: http://www.cplusplus.com/forum/beginner/45543/

Saves me re-inventing the wheel :)

Topic archived. No new replies allowed.