#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
usingnamespace std;
int main()
{
char choice; //variable for menu choice
int quit, restart=0, index; //variables for restarting and quiting, also index for loop counting
constint 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 ==========
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
usingnamespace std;
int main()
{
char choice; //variable for menu choice
int quit, restart=0, index; //variables for restarting and quiting, also index for loop counting
constint 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;
}
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)
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
usingnamespace std;
int main()
{
char choice; //variable for menu choice
int quit, restart=0, index; //variables for restarting and quiting, also index for loop counting
constint 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, constint 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]<<".";
}
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.....
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, constint 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]<<".";
}
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.