So basically this is suppose to be a grade calculator and the program works, I now just have to put it into a do-while loop so it can work for more than one student. My professor wants it to say Y for another student and N to quit at the end. I'm having a hard time figuring how to make that work. Also he wants the averages to come out to two decimal places even if its just 88.00 but it isn't working when I use setprecision. Any suggestions would be grealy appreciated.

#include <iostream>
#include <iomanip>
using namespace std;


int main (){
    system ("color f0");


string firstName, lastName, fullName;
char middle;
float score1, score2, score3, score4, score5, score6, score7;


cout<<"Student Grade Calculator"<<endl;

cout<<endl;

do{
cout<<"Please enter your first name, middle intial, and last name :"<<" ";
cin>>firstName>>middle>>lastName;

fullName=firstName+" "+middle+" "+lastName;

cout<<endl;

cout<<"Thank you, your name is"<<" "<<fullName<<endl; 

cout<<endl;


cout<<"Please enter the first program score (0-100): ";
cin>>score1;

cout<<endl;
cout<<"You entered"<<" "<<score1<<endl;

cout<<endl;

cout<<"Please enter the second program score (0-100): ";
cin>>score2;

cout<<endl;
cout<<"You entered"<<" "<<score2<<endl;

cout<<endl;

cout<<"Please enter the third program score (0-100): ";
cin>>score3;

cout<<endl;
cout<<"You enterd"<<" "<<score3<<endl;

cout<<endl;

cout<<"Please enter the fourth program score (0-100): ";
cin>>score4;

cout<<endl;
cout<<"You entered"<<" "<<score4<<endl;

cout<<endl;

cout<<"Please enter the first test score (0-100): ";
cin>>score5;

cout<<endl;
cout<<"You entered"<<" "<<score5<<endl;

cout<<endl;

cout<<"Please enter the second test score (0-100): ";
cin>>score6;

cout<<endl;
cout<<"You entered"<<" "<<score6<<endl;

cout<<endl;

cout<<"Please enter the third test score (0-100): ";
cin>>score7;

cout<<endl;
cout<<"You entered"<<" "<<score7<<endl;

cout<<endl;

cout<<setw(32)<<"*"<<endl;

double avr1,avr2,avr3; 
char grade,Y,N;

avr1=(score1+score2+score3+score4)/4;
avr2=(score5+score6+score7)/3;
avr3=(avr1+avr2)/2;

cout<<"Name: "<<" "<<fullName<<endl;
cout<<endl;
cout<<"Program Average.........."<<" "<<avr1<<endl;
cout<<endl;
cout<<"Test Average............."<<" "<<avr2<<endl;
cout<<endl;
cout<<"Course Average..........."<<" "<<avr3<<endl;
cout<<endl;
//Nested if-statement
if(avr3>=95)
{
         cout<<"Leter Grade.............. A"<<endl;
}
else if (avr3<=95 && avr3>90)
{
     cout<<"Letter Grade................. A-"<<endl;
}
else if (avr3<90 && avr3>=85)
{
     cout<<"Letter grade................. B"<<endl;
}
else if (avr3<=85 && avr3>80)
{
     cout<<"Letter Grade................. B-"<<endl;
}
else if(avr3<80 && avr3>=70)
{
     cout<<"Letter grade................. C"<<endl;
}
else 
{
     cout<<"Letter grade................. F"<<endl;
}
cout<<setw(32)<<"*"<<endl;
cout<<"Please enter Y for another student or N to quit: "<<endl;
cin>>Y,N;
}while(N!=0);











    system ("pause");
    return 0; 
}//end of main

Recommended Answers

All 21 Replies

please use code tags to post your code

Replaces the variables Y and N by a variable 'Answer' of type char which also receives user input (Y or N), and on condition of while loop you can see if it is equal to Y. So if it is equal to Y, the cycle repeats. For maximum effectiveness could you put the functions

cin >> Answer;

into another cycle to repeat while the user did not enter Y or N. Instead of always repeating unnecessarily

cout << "Please enter the third program score (0-100):";
cin >> score;

you could also write this only once within a cycle that is repeated as often as thou wilt, keeping the value received from the keyboard in a vector of variables; makes you much to code view.
I hope this helps.

Makes sense I'll try it out. And for the score part if I didn't put score1, score2, score3, etc. how would I calculate the averages?

Ok! You said you used setprecision() from iomanip. Did you use the appropriate flag to be used prior to setprecision?

When you say flag do you mean like cout<<setprecision(2) or something else?

In fact No.

setprecision can be used for two purposes.

1. To set the total number of digits to be shown when a floating point number is printed (Not talking about digits after the decimal point).

so if you want to pring 88.00, you should put output it like this

cout<<setprecision(4)<<floatvariable;

Remember here we made it 4, because we need to show two digits after decimal + two digits before.

And another feature of this function is that, it rounds the value.
so something like

cout<<setprecision(5)<<123.456;

Will outut 123.46 Notice that rounding at the end.

2. To set the number of decimal places to be shown(This is something you have been trying to do)
for that you should set an ios flag

flag for the present task is

cout.setf(ios::fixed);

once this is done, the value you put for setprecision will be the number of decimal places only.

And here no rounding takes place.

basic syntax is

cout.setf(ios::fixed);
cout<<setprecision(5)<<12.345678;

And output will be 12.34567 No rounding this time.


Remember once used, setprecision becomes sticky, ie. it applies to all the cout statements outputting an float value, no matter if you didn't add a setprecision() statement. But you can change the precision level to any integral value as you wish.

If you want to turn it off, you can replace setf in ios flag to unsetf I hope you understood this, well if you are having doubt about the "sticky" part, just let me know.

Thanks that helped a lot, I got it to work. I'm still having trouble with the loop though. I've tried declaring answer as a variable type char and I aslo declared N = 0 as char. I then put at the end of the loop

while(answer!='N');

However it's still not working, my compiler says answer is undeclared. Do you have any suggestions for this?

Basic syntax of a do while loop for your case is

char choice;
do
{


//Your code


cout<<"Enter the choice(Y/N)"<<endl;
cin>>choice;

}
while(choice=='y'||choice=='Y')
//here the loop continues, only if user presses y or Y

You can do it your way though

like this

while(choice!='n'||choice!='N')

Here if anything other than n is n or N is pressed, the loop continues.

I would recommend the first way though.

And please forget about N equating to 0. No need, this will do. Try it and let me know.

I did it your way and it still says answer is undeclared

Can you post your new code, inside code tags...

Remember N, or Y are not variables and don't declare them like that. Just use a single variable "answer".

I believe I got it to work, I declared y as a variable type char, and the loop seems to be working fine now. That does sound right, right? My professor is a perfectionist, so if there's a better or more accurate way of doing it I'd change it.

Any further help would require you to post the entire new code (off course in code tags). As far as i can see, declaring two variables for the loop condition is not the perfect way to go about it. Still the difference in efficiency is soooooo small....

#include <iostream>
#include <iomanip>
using namespace std;


int main (){
    system ("color f0");


//Intializations and executables 
string firstName, lastName, fullName;
char middle;
float score1, score2, score3, score4, score5, score6, score7;
float avr1,avr2,avr3; 
char grade, answer, y;

do//beginning of while
{
cout<<"Student Grade Calculator"<<endl;

cout<<endl;


cout<<"Please enter your first name, middle intial, and last name :"<<" ";
cin>>firstName>>middle>>lastName;

fullName=firstName+" "+middle+" "+lastName;

cout<<endl;

cout<<"Thank you, your name is"<<" "<<fullName<<endl; //Outputs student full name

cout<<endl;

//Input of student program and test score
cout<<"Please enter the first program score (0-100): ";
cin>>score1;

cout<<endl;
cout<<"You entered"<<" "<<score1<<endl;

cout<<endl;

cout<<"Please enter the second program score (0-100): ";
cin>>score2;

cout<<endl;
cout<<"You entered"<<" "<<score2<<endl;

cout<<endl;

cout<<"Please enter the third program score (0-100): ";
cin>>score3;

cout<<endl;
cout<<"You enterd"<<" "<<score3<<endl;

cout<<endl;

cout<<"Please enter the fourth program score (0-100): ";
cin>>score4;

cout<<endl;
cout<<"You entered"<<" "<<score4<<endl;

cout<<endl;

cout<<"Please enter the first test score (0-100): ";
cin>>score5;

cout<<endl;
cout<<"You entered"<<" "<<score5<<endl;

cout<<endl;

cout<<"Please enter the second test score (0-100): ";
cin>>score6;

cout<<endl;
cout<<"You entered"<<" "<<score6<<endl;

cout<<endl;

cout<<"Please enter the third test score (0-100): ";
cin>>score7;

cout<<endl;
cout<<"You entered"<<" "<<score7<<endl;

cout<<endl;

cout<<setfill('*')<<setw(32)<<"*"<<endl;




//Calculates averages
avr1=(score1+score2+score3+score4)/4;
avr2=(score5+score6+score7)/3;
avr3=(avr1+avr2)/2;

//Outputs calculations 
cout<<"Name: "<<" "<<fullName<<endl;
cout<<endl;
cout<<setprecision(4);
cout<<"Program Average.........."<<" "<<avr1<<endl;
cout<<endl;
cout<<"Test Average............."<<" "<<avr2<<endl;
cout<<endl;
cout<<"Course Average..........."<<" "<<avr3<<endl;
cout<<endl;


//Nested if-statement
if(avr3>=95)
{
         cout<<"Leter Grade.............. A"<<endl;
}
else if (avr3<=95 && avr3>90)
{
     cout<<"Letter Grade................. A-"<<endl;
}
else if (avr3<90 && avr3>=85)
{
     cout<<"Letter grade................. B"<<endl;
}
else if (avr3<=85 && avr3>80)
{
     cout<<"Letter Grade................. B-"<<endl;
}
else if(avr3<80 && avr3>=70)
{
     cout<<"Letter grade................. C"<<endl;
}
else 
{
     cout<<"Letter grade................. F"<<endl;
}
cout<<setw(32)<<"*"<<endl;
cout<<"Please enter Y for another student or N to quit: "<<endl;
cin>>answer;





}while (answer == 'y' || answer == 'Y');//end of while 





    
    
    
    
    
    
    
    system ("pause");
    return 0; 
}//end of main

Well! see, you have never used that char variable y. so you don't require that, just delete it. All you have used is char variable answer, which i was saying all the time.

Good job. This code is good, after you remove that char variable y.

Ok great, thank you so much for your help! I have a test on this stuff on thursday, I hope it sticks.

Replaces the variables Y and N by a variable 'Answer' of type char which also receives user input (Y or N), and on condition of while loop you can see if it is equal to Y. So if it is equal to Y, the cycle repeats. For maximum effectiveness could you put the functions

cin >> Answer;

into another cycle to repeat while the user did not enter Y or N. Instead of always repeating unnecessarily

cout << "Please enter the third program score (0-100):";
cin >> score;

you could also write this only once within a cycle that is repeated as often as thou wilt, keeping the value received from the keyboard in a vector of variables; makes you much to code view.
I hope this helps.

Here Al41007 gave the code of

cin >> Answer;

But you never declared "Answer" as a specific datatype... declare it before that line and it should take away the error

I didn't read the program assignment clearly the first time, the determinants for the letter grades are:

course average
>=90 A
80-89 B (however if ANY TEST SCORE is >=95) the letter grade is an A-
70-79 C (However if ANY TEST SCORE is >=85) the letter grades is a B-
<70 F

I changed the nested if to fit that statement but now the grades for A- and B- only come out of any one of the test scores is exactly equal to 85 or 95 not if its above.

Can anyone help me with this problem, my program is due tomorrow morning and I thought it was all done but it's not.

#include <iostream>
#include <iomanip>
using namespace std;


int main (){
    system ("color f0");
 

//Intializations and executables 
string firstName, lastName, fullName;
char middle;
float score1, score2, score3, score4, score5, score6, score7;
float avr1,avr2,avr3; 
char grade, answer, y;

do//beginning of while
{
cout<<"Student Grade Calculator"<<endl;

cout<<endl;


cout<<"Please enter your first name, middle intial, and last name :"<<" ";
cin>>firstName>>middle>>lastName;

fullName=firstName+" "+middle+" "+lastName;

cout<<endl;

cout<<"Thank you, your name is"<<" "<<fullName<<endl; //Outputs student full name

cout<<endl;

//Input of student program and test score
cout<<"Please enter the first program score (0-100): ";
cin>>score1;

cout<<endl;
cout<<"You entered"<<" "<<score1<<endl;

cout<<endl;

cout<<"Please enter the second program score (0-100): ";
cin>>score2;

cout<<endl;
cout<<"You entered"<<" "<<score2<<endl;

cout<<endl;

cout<<"Please enter the third program score (0-100): ";
cin>>score3;

cout<<endl;
cout<<"You enterd"<<" "<<score3<<endl;

cout<<endl;

cout<<"Please enter the fourth program score (0-100): ";
cin>>score4;

cout<<endl;
cout<<"You entered"<<" "<<score4<<endl;

cout<<endl;

cout<<"Please enter the first test score (0-100): ";
cin>>score5;

cout<<endl;
cout<<"You entered"<<" "<<score5<<endl;

cout<<endl;

cout<<"Please enter the second test score (0-100): ";
cin>>score6;

cout<<endl;
cout<<"You entered"<<" "<<score6<<endl;

cout<<endl;

cout<<"Please enter the third test score (0-100): ";
cin>>score7;

cout<<endl;
cout<<"You entered"<<" "<<score7<<endl;

cout<<endl;
cout<<"123456789012345678901234567890123456789012345"<<endl;
cout<<setfill('*')<<setw(45)<<"*"<<endl;




//Calculates averages
avr1=(score1+score2+score3+score4)/4;
avr2=(score5+score6+score7)/3;
avr3=(avr1+avr2)/2;

//Outputs calculations 
cout<<"Name: "<<" "<<fullName<<endl;
cout<<endl;
cout<<setprecision(4)<<showpoint;
cout<<"Program Average.........."<<" "<<avr1<<endl;
cout<<endl;
cout<<"Test Average............."<<" "<<avr2<<endl;
cout<<endl;
cout<<"Course Average..........."<<" "<<avr3<<endl;
cout<<endl;


//Nested if-statement
if(score5,score6,score7>=95)
{
         cout<<"Leter Grade.............. A-"<<endl;
}
else if (avr3>=90)
{
     cout<<"Letter Grade................. A"<<endl;
}
else if (avr3<=89 && avr3>=80)
{
     cout<<"Letter grade................. B"<<endl;
}
else if (score5,score6,score7 >=85)
{
     cout<<"Letter Grade................. B-"<<endl;
}
else if(avr3<=79 && avr3>=70)
{
     cout<<"Letter grade................. C"<<endl;
}
else 
{
     cout<<"Letter grade................. F"<<endl;
}
cout<<setw(45)<<"*"<<endl;

cout<<"Please enter Y for another student or N to quit: ";
cin>>answer;

cout<<endl;





}while (answer == 'y' || answer == 'Y');//end of while 





    
    
    
    
    
    
    
    system ("pause");
    return 0; 
}//end of main
if(score5,score6,score7>=95)

This syntax is wrong. For "or", "||" should be used, like this,

if(score5>=95||score6>=95||score7>=95)

Thats how its done. Now try it in the code.....

However with the condition you gave, this should be the if statements, nested ifs makes sense here...

if (avr3>=90)
{
     cout<<"Letter Grade................. A"<<endl;
}
else if (avr3<=89 && avr3>=80)
{
     if(score5>=95||score6>=95||score>=95)
     {
          cout<<"Letter grade................. A-"<<endl;
     }
     else
     {
          cout<<"Letter grade.................B"<<endl;
     }
}
else if(avr3<=79 && avr3>=70)
{
     if(score5>=85||score6>=85||score>=85)
     {
          cout<<"Letter grade...............B-"<<endl;
     }
     else
     {
          cout<<"Letter grade................. C"<<endl;
     }
}
else 
{
     cout<<"Letter grade................. F"<<endl;
}

Always start with the main conditions, then look at sub conditions, then remember to add nested ifs for sub conditions

#include <iostream>
#include <iomanip>
using namespace std;


int main (){
system ("color f0");


string firstName, lastName, fullName;
char middle;
float score1, score2, score3, score4, score5, score6, score7;
int N;

cout<<"Student Grade Calculator"<<endl;

cout<<endl;

do{
cout<<"Please enter your first name, middle intial, and last name :"<<" ";
cin>>firstName>>middle>>lastName;

fullName=firstName+" "+middle+" "+lastName;

cout<<endl;

cout<<"Thank you, your name is"<<" "<<fullName<<endl;

cout<<endl;


cout<<"Please enter the first program score (0-100): ";
cin>>score1;

cout<<endl;
cout<<"You entered"<<" "<<score1<<endl;

cout<<endl;

cout<<"Please enter the second program score (0-100): ";
cin>>score2;

cout<<endl;
cout<<"You entered"<<" "<<score2<<endl;

cout<<endl;

cout<<"Please enter the third program score (0-100): ";
cin>>score3;

cout<<endl;
cout<<"You enterd"<<" "<<score3<<endl;

cout<<endl;

cout<<"Please enter the fourth program score (0-100): ";
cin>>score4;

cout<<endl;
cout<<"You entered"<<" "<<score4<<endl;

cout<<endl;

cout<<"Please enter the first test score (0-100): ";
cin>>score5;

cout<<endl;
cout<<"You entered"<<" "<<score5<<endl;

cout<<endl;

cout<<"Please enter the second test score (0-100): ";
cin>>score6;

cout<<endl;
cout<<"You entered"<<" "<<score6<<endl;

cout<<endl;

cout<<"Please enter the third test score (0-100): ";
cin>>score7;

cout<<endl;
cout<<"You entered"<<" "<<score7<<endl;

cout<<endl;

cout<<setw(32)<<"*"<<endl;

double avr1,avr2,avr3;
char grade,Y,N;

avr1=(score1+score2+score3+score4)/4;
avr2=(score5+score6+score7)/3;
avr3=(avr1+avr2)/2;

cout<<"Name: "<<" "<<fullName<<endl;
cout<<endl;
cout<<"Program Average.........."<<" "<<avr1<<endl;
cout<<endl;
cout<<"Test Average............."<<" "<<avr2<<endl;
cout<<endl;
cout<<"Course Average..........."<<" "<<avr3<<endl;
cout<<endl;
//Nested if-statement
if(avr3>=95)
{
cout<<"Leter Grade.............. A"<<endl;
}
else if (avr3<=95 && avr3>90)
{
cout<<"Letter Grade................. A-"<<endl;
}
else if (avr3<90 && avr3>=85)
{
cout<<"Letter grade................. B"<<endl;
}
else if (avr3<=85 && avr3>80)
{
cout<<"Letter Grade................. B-"<<endl;
}
else if(avr3<80 && avr3>=70)
{
cout<<"Letter grade................. C"<<endl;
}
else
{
cout<<"Letter grade................. F"<<endl;
}
cout<<setw(32)<<"*"<<endl;
cout<<"Please enter Y for another student or N to quit: "<<endl;
cin>>Y,N;
}while(N!=0);











system ("pause");
return 0;
}//end of main

there u go it's working now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.