Using Vectors and Arrays to display five names, heights, weights

I have this code that should display five names, heights, and weights:
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
54
55
56
57
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
	char choice; //variable for menu choice
	int quit, restart=0, index; //variables for restarting and quiting, also index for loop counting
	const int PEOPLE=5;  //people integar of 5 for number of people
	vector<int> height(PEOPLE);
	vector<int> weight(PEOPLE);
	vector<string> name(PEOPLE);
	int ht;
	int wt;

	//Dislay the menu
	do
	{
		if (restart==1)
			cout <<endl<<endl<<endl;
		cout <<"A.  Enter Female Data\nB.  Enter Male Data\nC.  Quit"<<endl;
		cout <<"Enter Your Choice: ";
		cin >>choice;
		cout <<endl;
		
		//validate the choice
		while (choice!='A'&&choice!='B'&&choice!='C'&&choice!='a'&&choice!='b'&&choice!='c')
		{
			cout <<"You entered '"<<choice<<"' which is not valid\n";
			cout <<"Enter a valid choice! ";
			cin >>choice;
		}
		
	//Process the users choice
		if (choice!='c'&&choice!='C')
		{
			cout <<"Enter the names of the five participants";
			for (index=0;index<PEOPLE;index++)
			{
				cout <<"Name of participant #" <<(index+1)<<": ";
				cin >>name[index];
				cout <<"Height of participant #" <<(index+1)<<": ";
				cin >>ht[index];
				cout <<"Weight of participant #"<<(index+1)<<": ";
				cin >>wt[index];
			}
		}
		for (index=0;index<PEOPLE;index++)
			cout <<name[index]<<" has a height of "<<height[index]<<" and a weight of "<<weight[index]<<".";
		else
			quit=1;
	} while (quit==0);
	system("pause");
	return 0;
}


But it gives me this error message
1
2
3
4
5
1
1>  Assessment 3.1.cpp
1>Assessment 3.1.cpp(45): error C2109: subscript requires array or pointer type
1>Assessment 3.1.cpp(47): error C2109: subscript requires array or pointer type
1>Assessment 3.1.cpp(52): error C2181: illegal else without matching if
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The error is pretty clear. ht and wt are not arrays.
I think you meant to display height[index] and weight[index].
So I updated the program and I still get the "else without matching if" error. The matching if should be at line 37.

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
54
55
56
57
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
	char choice; //variable for menu choice
	int quit, restart=0, index; //variables for restarting and quiting, also index for loop counting
	const int PEOPLE=5;  //people integar of 5 for number of people
	vector<int> height(PEOPLE);
	vector<int> weight(PEOPLE);
	vector<string> name(PEOPLE);
	int ht[PEOPLE];
	int wt[PEOPLE];

	//Dislay the menu
	do
	{
		if (restart==1)
			cout <<endl<<endl<<endl;
		cout <<"A.  Enter Female Data\nB.  Enter Male Data\nC.  Quit"<<endl;
		cout <<"Enter Your Choice: ";
		cin >>choice;
		cout <<endl;
		
		//validate the choice
		while (choice!='A'&&choice!='B'&&choice!='C'&&choice!='a'&&choice!='b'&&choice!='c')
		{
			cout <<"You entered '"<<choice<<"' which is not valid\n";
			cout <<"Enter a valid choice! ";
			cin >>choice;
		}
		
	//Process the users choice
		if (choice!='c'&&choice!='C')
		{
			cout <<"Enter the names of the five participants";
			for (index=0;index<PEOPLE;index++)
			{
				cout <<"Name of participant #" <<(index+1)<<": ";
				cin >>name[index];
				cout <<"Height of participant #" <<(index+1)<<": ";
				cin >>ht[index];
				cout <<"Weight of participant #"<<(index+1)<<": ";
				cin >>wt[index];
			}
		}
		for (index=0;index<PEOPLE;index++)
			cout <<name[index]<<" has a height of "<<height[index]<<" and a weight of "<<weight[index]<<".";
		else
			quit=1;
	} while (quit==0);
	system("pause");
	return 0;
}
Last edited on
It's because the else near the end of your code is after the for loop, thus the compiler sees it as a solitary else. You'll need to swap the else and the for loop. (lines 52-53 and 50-51)
I added a function and a function call to fix the else if problem, but now I get a subscript requires array or pointer type error. New code below:

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
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
	char choice; //variable for menu choice
	int quit, restart=0, index; //variables for restarting and quiting, also index for loop counting
	const int PEOPLE=5;  //people integar of 5 for number of people
	vector<int> ht(PEOPLE);
	vector<int> wt(PEOPLE);
	vector<string> name(PEOPLE);

	void header();
	void inputValues ();

	//Dislay the menu
	do
	{
		if (restart==1)
			cout <<endl<<endl<<endl;
		cout <<"A.  Enter Female Data\nB.  Enter Male Data\nC.  Quit"<<endl;
		cout <<"Enter Your Choice: ";
		cin >>choice;
		cout <<endl;
		
		//validate the choice
		while (choice!='A'&&choice!='B'&&choice!='C'&&choice!='a'&&choice!='b'&&choice!='c')
		{
			cout <<"You entered '"<<choice<<"' which is not valid\n";
			cout <<"Enter a valid choice! ";
			cin >>choice;
		}
		
		//Process the users choice
		if (choice!='c'&&choice!='C')
		inputValue();
		else
			quit=1;
	} while (quit==0);
	system("pause");
	return 0;
}

void inputValues (int ht, int wt, string name,int index, const int PEOPLE)
{
	cout <<"Enter the names of the five participants";
	for (index=0;index<PEOPLE;index++)
	{
		cout <<"Name of participant #" <<(index+1)<<": ";
		cin >>name[index];
		cout <<"Height of participant #" <<(index+1)<<": ";
		cin >>ht[index];
		cout <<"Weight of participant #"<<(index+1)<<": ";
		cin >>wt[index];
	}
	for (index=0;index<PEOPLE;index++)
		cout <<name[index]<<" has a height of "<<ht[index]<<" and a weight of "<<wt[index]<<".";
}
You didn't correct the first error.
As i said in my previous post:
The error is pretty clear. ht and wt are not arrays.
I think you meant to display height[index] and weight[index].
Did lines 12 and 13 not make ht and wt arrays? I thought that was what a vector did :P
1
2
	vector<int> ht(PEOPLE);
	vector<int> wt(PEOPLE);


Try this:

int a_ht[57];
and
char a_name{100]

where a_ht[57] defines an array containing 57 ints
and
a_name is an array of 100 chars

you would then use pointers to point to each separate element in the array to read or write data.

like this

a_ht[43] = 34567
or

int i_current_arry_position = 28;
a_ht [i_current_arry_position] = 6543


That is a bit confusing to me. Do you mean to replace he vectors with that? Maybe it would help if I understood a bit about what a pointer actually was.....
You did correct the error in main(), but your inputValues() function still receives ht and wt as ints.

Also, you should pass arguments to the function when you call it (line 39).
Well ht and wt are ints. When the user enters the heights and weights they will be stored in a array of integars.

I see my error on line 39, and I will put the arguments in it and edit this post with the results.

EDIT: I changed line 39 and get the same error. Then new code from line 39 down is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
inputValue(ht, wt, name,index,PEOPLE);
		else
			quit=1;
	} while (quit==0);
	system("pause");
	return 0;
}

void inputValues (int ht, int wt, string name,int index, const int PEOPLE)
{
	cout <<"Enter the names of the five participants";
	for (index=0;index<PEOPLE;index++)
	{
		cout <<"Name of participant #" <<(index+1)<<": ";
		cin >>name[index];
		cout <<"Height of participant #" <<(index+1)<<": ";
		cin >>ht[index];
		cout <<"Weight of participant #"<<(index+1)<<": ";
		cin >>wt[index];
	}
	for (index=0;index<PEOPLE;index++)
		cout <<name[index]<<" has a height of "<<ht[index]<<" and a weight of "<<wt[index]<<".";
}
Last edited on
In your inputValues() function, you pass ht and wt as ints, yet you use ht[index] and wt[index] in your code. See the problem ?
So I need to pass ht and wt as arrays? How would I do this?
How can you not do it ?
You said that in main() you defined ht and wt as vectors. Passing them to a function that expects ints should result in a compilation error.
Okay, I think I'm understanding now. Somewhere in the text I missed something :P. I eventually rectified the problem and turned the assignment in.
Topic archived. No new replies allowed.