the whole program works but when i tried to put the last else statement in on line 113 there is a error and i know its the stupidest little syntax error and i want someone to show me how stupid i am for messing it up..
thanks in advance
#include <iostream>
#include <string>
usingnamespace std;
struct PersonRec
{
string firstName, lastName;
int age;
booloperator==(PersonRec& rhs)
{
//Will return true if both names are equal
return ( rhs.firstName == firstName && rhs.lastName == lastName );
}
};
void displayOldest(PersonRec rec[], int size)
{
int indexOfOldest = 0;
for(int i = 0; i < size; ++i)
{
if(rec[i].age > rec[indexOfOldest].age)
indexOfOldest = i;
}
cout << "\nThe oldest person is: \n"
<< "Name: " << rec[indexOfOldest].firstName << " " << rec[indexOfOldest].lastName << endl
<< "Age: " << rec[indexOfOldest].age << endl;
}
void displayYoungest (PersonRec rec[],int size)
{
int indexOfYoungest = 0;
for(int i = 0; i < size; ++i)
{
if(rec[i].age < rec[indexOfYoungest].age)
indexOfYoungest = i;
}
cout << "\nThe youngest person is: \n"
<< "Name: " << rec[indexOfYoungest].firstName << " " << rec[indexOfYoungest].lastName << endl
<< "Age: " << rec[indexOfYoungest].age << endl;
}
void printRecords(PersonRec rec[], int size)
{
for (int i = 0; i<size; i++)
{
cout<<rec[i].firstName<<' '<<rec[i].lastName<<' '<<rec[i].age<<endl;
}
}
int main()
{
PersonRec* thePeople = NULL;
int size = 0;
while(size < 1)
{
cout << "How many people's information would you like to store: ";
cin >> size;
}
thePeople = new PersonRec[size];
//Get the input
for(int i = 0; i < size; ++i)
{
cout << "Please enter the first name of person #" << i+1 << ": ";
cin >> thePeople[i].firstName;
cout << "Please enter the last name of person #" << i+1 << ": ";
cin >> thePeople[i].lastName;
cout << "Please enter the age of person #" << i+1 << ": ";
cin >> thePeople[i].age;
//Check for equality if we have mroe than 1 entry
if(i > 0)
{
for(int j = 0; j < i; ++j)
{
if(thePeople[i] == thePeople[j])
{
--i;
cout << "\n\nCannot enter same name, please try again...\n\n";
}
}
}
}
char choice;
if (choice = 'A'||'B'||'C')
{ cout<<"Plese Enter Max Number of Records"<<endl;//max number of records
cout<<"if you would like to see the oldest enter 'A' "<<endl;//ask if user wants the oldest information
cout<<"if you would like to see the youngest enter 'B' "<<endl;
cout<<"if you would like to see all records of informaion enter 'C'"<<endl;
cout<<"Please enter choice for which\n";
cout<<"records you would like to see\n";
cout<<"choice is CAP sensitive"<<endl;
cin>>choice;
switch(choice)
{case'A': displayOldest(thePeople,size);
break;
case'B':displayYoungest(thePeople,size);
break;
case'C':printRecords(thePeople,size);
break;
}
else
cout<<"Please Enter a valid character remember it is CAP sensitive"
cin.sync();
cin.get();
return 0;
}}
As an aside, this: if (choice = 'A'||'B'||'C') is almost certainly not what you meant.
(choice = 'A'||'B'||'C') sets the value of choice to A, and then comes out as true, always always always. Perhaps you meant: if (choice == 'A'||choice =='B'||choice =='C')
#include <iostream>
#include <string>
usingnamespace std;
struct RecsPerson
{
string firstName, lastName;
int age;
booloperator==(RecsPerson& rhs)
{
//Will return true if both names are equal
return ( rhs.firstName == firstName && rhs.lastName == lastName );
}
};
void displayOldest(RecsPerson rec[], int SIZE)
{
int indexOfOldest = 0;
for(int i = 0; i < SIZE; ++i)
{
if(rec[i].age > rec[indexOfOldest].age)
indexOfOldest = i;
}
cout << "\nThe oldest person is: \n"
<< "Name: " << rec[indexOfOldest].firstName << " " << rec[indexOfOldest].lastName << endl
<< "Age: " << rec[indexOfOldest].age << endl;
}
void displayYoungest (RecsPerson rec[],int SIZE)
{
int indexOfYoungest = 0;
for(int i = 0; i < SIZE; ++i)
{
if(rec[i].age < rec[indexOfYoungest].age)
indexOfYoungest = i;
}
cout << "\nThe youngest person is: \n"
<< "Name: " << rec[indexOfYoungest].firstName << " " << rec[indexOfYoungest].lastName << endl
<< "Age: " << rec[indexOfYoungest].age << endl;
}
void printRecords(RecsPerson rec[], int SIZE)
{
for (int i = 0; i<SIZE; i++)
{
cout<<rec[i].firstName<<' '<<rec[i].lastName<<' '<<rec[i].age<<endl;
}
}
int main()
{
RecsPerson* People = NULL;
int SIZE = 0;
while(SIZE < 1)
{
cout << "How many people's information would you like to store: ";
cin >> SIZE;
}
People = new RecsPerson[SIZE];
//Get the input
for(int i = 0; i < SIZE; ++i)
{
cout << "Please enter the first name of person #" << i+1 << ": ";
cin >> People[i].firstName;
cout << "Please enter the last name of person #" << i+1 << ": ";
cin >> People[i].lastName;
cout << "Please enter the age of person #" << i+1 << ": ";
cin >> People[i].age;
//Check for equality if we have mroe than 1 entry
if(i > 0)
{
for(int j = 0; j < i; ++j)
{
if(People[i] == People[j])
{
--i;
cout << "\n\nCannot enter same name, please try again...\n\n";
}
}
}
}
char choice;
cout<<"Please enter choice for which\n";
cout<<"records you would like to see\n";
cout<<"choice is CAP sensitive"<<endl;
cin>>choice;
if (choice == 'A'||choice =='B'||choice =='C')
{
cout<<"Plese Enter Max Number of Records"<<endl;//max number of records
cout<<"if you would like to see the oldest enter 'A' "<<endl;//ask if user wants the oldest information
cout<<"if you would like to see the youngest enter 'B' "<<endl;
cout<<"if you would like to see all records of informaion enter 'C'"<<endl;
switch(choice)
{case'A': displayOldest(People,SIZE);
break;
case'B':displayYoungest(People,SIZE);
break;
case'C':printRecords(People,SIZE);
break;
}}
else
cout<<"Please Enter a valid character remember it is CAP sensitive"<<endl;
cin.sync();
cin.get();
return 0;
}
The menu pops up AFTER you enter a choice. (maybe that's why you think it's "looping" -- because you were waiting for the menu to show up, when the program was actually waiting for you to input the menu choice)