-
help with a loop
I'm stuck on a portion of my code. I'm not sure how to print a message without showing a number that determines the message.
Instructions: Write a program to compute student GPA's. The program should repeatedly ask the user for a student ID number, probation status and the number of classes taken by the student this semester and then ask the user for the number of credit hours and grades for the classes. Based on this information the program should compute and output the student's semester GPA. The grades will be entered as numbers where 4.0=A, 3.0=B, 2.0=C, 1.0=D, 0.0=F. The program should also determine whether a student already on probation should be suspended or removed from probation and whether a student not already on probation should be placed on probation. At the end, the program should output a count of the total number of students, the number now on probation and the number now suspended.
Code:
#include<iostream>
using namespace std;
int main()
{
int studentid, probationstat, numclasses, credithours, totalhours=0;
double gpa, classgrade, semestergpa, totalgrade=0;
cout<<"-------------------------------------------------- \n";
cout <<"Enter the student ID: ";
cin >> studentid;
while (studentid != -1)
{
cout <<"Enter probation status: ";
cin >> probationstat;
cout <<"Enter number of classes taken by student: ";
cin >> numclasses;
while (numclasses > 0)
{
cout <<"Enter credit hours and grade for a class: ";
numclasses=numclasses-1;
cin>>credithours>>classgrade;
}
gpa=credithours*classgrade;
totalgrade=totalgrade+classgrade;
totalhours=totalhours+credithours;
semestergpa=gpa/totalhours;
cout.setf(ios::fixed | ios::showpoint); // display 4 decimals for GPA
cout.precision(4);
cout <<"Semester GPA: " << semestergpa <<"\n";
cout <<"New Status: " << probationstat <<"\n";
if ((probationstat == 1) && (gpa < 2.0))
{
cout <<"SUSPENDED"<<endl;
}
else if ((probationstat == 1) && (gpa > 2.5) && (credithours > 6))
{
cout <<"NOT ON PROBATION"<<endl;
}
else if ((probationstat == 1) && (gpa > 2.5) && (credithours < 6))
{
cout <<"ON PROBATION"<<endl;
}
else if ((probationstat == 1) && (gpa > 2.0) && (gpa < 2.5))
{
cout<<"ON PROBATION"<<endl;
}
else if ((probationstat == 2) && (gpa < 2.0))
{
cout<<"ON PROBATION"<<endl;
}
else if ((probationstat == 2) && ( gpa > 2.0))
{
cout<<"NOT ON PROBATION"<<endl;
}
cout <<"------------------------------------------------- \n";
cout <<"Enter the student ID: ";
cin >> studentid;
}
system("PAUSE");
return 0;
}
Whenver I run this, the output of the student's status looks like this if the condition is met:
Code:
New Status: 1 On Probation
I'm wondering how can I get it just to say "On Probation" or any other possible message without displaying the number?
P.S. I know the math isn't calculating perfectly. That's something I can work out.
-
>> cout <<"New Status: " << probationstat <<"\n";
Maybe I don't understand, but doesn't that line output the text you don't want? Perhaps removing that line will solve your problem?
-
Here's the sample I/O the progam is supposed to show.
Code:
-----------------------------------------------------
Enter student ID: 1207
Enter probation status: 2
Enter number of classes taken by student: 3
Enter credit hours and grade for a class: 3 0.0
Enter credit hours and grade for a class: 3 1.0
Enter credit hours and grade for a class: 3 2.0
SemesterGPA = 1.0000
New Status = ON-PROBATION
-----------------------------------------------------
Somehow I am able to get the message to print under "New Status" but I don't know how to get it without showing the number entered for the probation status.
-
Do you see where in the code the number is being output?
-
Thanks for trying to help. Someone was able to help me. It's the simple errors that leave me guessing where I went wrong.