Program help please!!!
ok i got my loop to work but i need to make it where the user has to enter a grade between 0 and 100, I tried by using
int grade(0<100);
but it doesnt work. what do i need to do? also how do i get the program to print out the highest to lowest scores after they have all been entered?
//The program has a two argument constructor to the definition of the Person structure.
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const int MAX = 3;
struct Person
{
string Firstname, Lastname;
int grade;
Person()
{
Firstname="No name assigned";
Lastname="No name assigned";
grade = -1;
}
Person (string l, string f, int g)
{
Firstname = f;
Lastname =l;
grade = g;
}
};
int main()
{
int x = 10;
while (x++ < 20)
{
int grade(0<100);
string strFirstname, strLastname;
cout<<"Enter person's first name: ";
getline(cin, strFirstname);
cout<<"Enter person's last name: ";
getline(cin, strLastname);
cout <<"Enter the grade: ";
cin>>grade;
cin.ignore();
Person p1(strLastname, strFirstname, grade);
cout <<"The person's name is: "
<<p1.Lastname<<", "<<p1.Firstname<<endl;
cout <<"The grade is: "
<<p1.grade<<endl;
}
return 0;
}
bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
line 30 is illegal syntax. just initialize the int to 0
int grade = 0;
now after the user enters the grade at line 37 you need to validate it. If not valid then make him enter the grade again. You probably need to do all that in a do loop or a while loop, either one will work ok. Something like this sudo code.
while grade is not ok
get grade from keyboard
is grade between 0 and 100
if it is then exit the loop
otherwise if not display an error message
end of while loop
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You can use an enum
enum myenum {
myenumStart=0,
...
myenumEnd=100
};
ithelp
Nearly a Posting Maven
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
Use a for loop rather than while. you can control more the flow of your program if it is in for loop
A for loop is not appropriate here -- there is nothing to be incremented and need to loop an infinite number of times until specified condition is met.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343