using functions to evaluate a students grades

Hi, can anyone help?
I have an assignment to read in three grades for a student, and evaluate if they are valid. A valid score is in the range from 0 to 100.

1. The function aretheyvalid()will receive three integers representing the grades. The function will determine if each grade is valid. It will send the grades one at a time to a function isvalid(), to determine whether the three grades are valid. It will keep track of how many scores are valid(using boolean operation). If all are valid it will return true to the main program, meaning this is a valid set of scores. If any or all are invalid it will return false.

2. The function isvalid() will receive 1 integer(a grade). If the score is invalid, isvalid() will print it with a message saying why it is invalid. If the score is valid, isvalid() will return true, saying this is a valid score.


I'm new to C++. below is my code for the two functions. I know i have errors. Please help.


bool aretheyvalid(int score1, int score2, int score3)
{
isvalid(score1);
isvalid(score2);
isvalid(score3);

if(score1>=0||score1<=100&&score2>=0||score2<=100&&score3>=0||score3<=100)
return true;

else
return false;

}
//Function to determine if each score is valid and print a message in each case.
bool isvalid(int num)
{
if(num<0)
cout<<num<<"is too small";
return false;
if(num>100)
cout<<num<<"is too big";
else
aretheyvalid(num)
cout<<num<<"is a valid score";
return true;

}
can anyone help? I'm getting an error with these too functions. it says too few arguments to bool aretheyvalid(int,int,int)
here's one thing to look at:
1
2
3
4
5
6
7
bool invalid(int num)
{
if (num<0)
cout << num << "is too small";
return false;
if (num>100)
etc...     


Can you see why this function will ALWAYS return false?
Look at it when it is indented.

1
2
3
4
5
6
7
bool invalid(int num)
    {
       if (num<0)
            cout << num << "is too small";
       return false;
       if (num>100)
       etc...     


Another thing is that you call isvalid from aretheyvalid, and then call aretheyvalid from within isvalid. This would lead to an infinite loop, it just doesn't in this case because that function is always returning false. And in isvalid, when you do call aretheyvalid, you only send one parameter:
aretheyvalid(num)
Your aretheyvalid function takes 3 parameters. You shouldn't have to make that function call at all.

I hope I'm not confusing you even more, I want to help, but I don't want to write it for you.


Last edited on
Thanks for your help. Is this correct?
bool isvalid(int num)
{
int num;

if(num<0)
cout<<num<<"is too small";
else if(num>100)
cout<<num<<"is too big";
return false;
else
return true;
aretheyvalid(num);
cout<<num<<"is a valid score";


}
Not quite. With indentation:

1
2
3
4
5
6
7
8
9
10
11
12
bool isvalid(int num) {
    int num;
    if(num<0)
        cout<<num<<"is too small";
    else if(num>100)
        cout<<num<<"is too big";
    return false;
    else
        return true;
    aretheyvalid(num);
    cout<<num<<"is a valid score";
}
This is my entire code. Please help me without doing it for me. I've been at this for a few days now. I'd like to figure this out on my own but i'm lost.

The main is suppose to use a do..while loop and the user response method to determine the end of the data. It calls the function introduction(), next in the loop the program will read in three scores then print the scores and add 1 to the number of groups of students scores. Then it calls aretheyvalid() to determine if the scores are valid. aretheyvalid() will return true or false. If aretheyvalid() says it is a valid group, the main will add 1 to validgroups and then call classifyscores(). However if the groups are invalid, the main will add 1 to invalid groups. When the program is done it will print the scores, the number of groups processed, the number of valid and invalid groups.

This is the what the output is supposed to look like:
the scores are:50 40 30
the grades are valid
the letter grades are decreasing
the average is 40
the letter grade is F


//This program will read in and evaluate a stuent's scores.
#include<iostream>
using namespace std;
void introduction();
bool aretheyvalid(int, int, int);
void classifyscores(int, int, int);
bool isvalid(int);
void aretheysame(int,int,int);
double findavg(int,int,int);
void makeletter(double);
int main()
{

int numgroups=0,validgroups=0,invalidgroups=0;
int score1, score2, score3;
char letter,answer;

introduction();

do{
cout<<"Type in the student's three scores from 0 to 100"<<endl;

cin>>score1;
cin>>score2;
cin>>score3;
cout<<"the scores are: "<<score1, score2, score3;
numgroups++;
if (aretheyvalid(score1, score2, score3));
validgroups++;
cout<<"the grades are valid"<<endl;
classifyscores(score1,score2,score3);

else
cout<<"the grades are invalid"<<endl;
invalidgroups++;
cin>>score1;
cin>>score2;
cin>>score3;
cout<<endl;
cout<<"the scores are:"<<score1, score2, score3;
cout<<"Type in y to continue, n to stop"<<endl;
cin>>answer;
}while(answer=='y');

cout<<"We processed"<<numgroups<<"groups"<<endl;
cout<<"We processed"<<validgroups<<"validgroups"<<endl;
cout<<"We processed"<<invalidgroups<<"invalidgroups"<<endl;

system("pause");
return 0;
}
------------------------------------------------------------------------------------------------
//Function to send to the file a description of the prgogram with the grading
//scheme.
void introduction()
{
cout<<"this program will read in a student's 3 grades,"
<<"evaluate if they are valid, whether they increase,"
<<"decrease or stay the same, calculate the average"
<<"score and assign a letter grade using a grading scheme:"
<<"A(89-100)"
<<"B(79-88)"
<<"C(69-78)"
<<"D(59-68)"
<<"F(0-58)"<<endl;
return;
}
------------------------------------------------------------------------------------------------
//Function to receive three test scores and determine if the scores are valid.
//I.E. between the range of 0 to 100.
bool aretheyvalid(int score1, int score2, int score3)
{
isvalid(score1);
isvalid(score2);
isvalid(score3);
{
{if(score1==1&&score2==1&&score3==1)
return true;

else
return false;
}
------------------------------------------------------------------------------------------------
//Function to determine if each score is valid and print a message in each case.

bool isvalid(int num)
{

int num;
if(num<0)
cout<<num<<"is too small";
else if(num>100)
cout<<num<<"is too big";
return false;
else
return true;
aretheyvalid(num);
cout<<num<<"is a valid score";

}
------------------------------------------------------------------------------------------------
//Function classify:
//Input:
//Three scores per student.
//Process:
//Calls a function to determine if all three scores are the same.
//Will evaluate the grades and print a message that describes the pattern
//followed by the grades.
//Calls a function to assign a letter grade to each student after receiving the
//average grade. The function will translate the average score into a letter grade.

void classifyscores(int score1, int score2, int score3)
{
double average;

aretheysame(score1, score2, score3);
findavg(score1, score2, score3);
cout<<"the average is"<<average;
makeletter (average);
return(score1, score2, score3);
}
------------------------------------------------------------------------------------------------
//Function to determine if the three scores are the same.
//The function will evaluate the grades and print a message.
//That describes the pattern of the grades:The same; increasing; decreasing;
//decreasing up and then down; down and then up; the first two are the same and
//the third is either higher or lower; the second are the same and the first is
//either higher or lower.

void aretheysame(int score1, int score2, int score3)
{
if(score1==score2&&score1==score3&&score2==score3)
cout<<"the grades are all the same";
else if (score1<score2&&score2<score3)
cout<<"the grades are increasing";
else if(score1>score2&&score2>score3)
cout<<"the grades are decreasing";
else if(score1<score2&&score2>score3)
cout<<"the grades go up and then down";
else if (score1>score2&&score2<score3)
cout<<"the grades go down and then up";
else if(score1=score2&&score1<score3&&score2<score3)
cout<<"the first two are the same, and the third is higher or lower";
else if(score2=score3&&score2<score1&&score3<score1)
cout<<"the second are the same, and the first is higher or lower";
return(score1,score2,score3);
}
------------------------------------------------------------------------------------------------
//Function to find the average of the three scores
void findavg(int score1, int score2, int score3);
{
double average;

average=(score1+score2+score3)/3;

return (average);
}
------------------------------------------------------------------------------------------------
//Function to assign a letter grade to each students set of scores
void makeletter(double average)
{
int avgnum;

if(avgnum>=89||avgnum<=100)
cout<<"the letter grade is A";
else if(avgnum>=79||avgnum<=88)
cout<<"the letter grade is B";
else if(avgnum>=69||avgnum<=78)
cout<<"the letter grade is C";
else if(avgnum>=59||avgnum<=68)
cout<<"the letter grade is D";
else
cout<<"the letter grade is F"<<endl;

return(avgnum);
}








Last edited on
Two things I noticed right off...as mentioned above, isvalid always will return false. If you want to have an if or else statement encompass multiple lines, you need to use curly braces {}

Ex:
1
2
3
4
5
if(...) {
    //...
} else {
   //...
}


Second is that you have multiple functions with a return type of void, meaning they do not return anything; then you attempt to return values from them.

Also, please use [code][/code] tags around your posts, it improves readability and preserves indentation.
Hi, I've made some changes to the program but I'm having problems with function makeletter(). It is not assigning the correct grade letter. it assigns F for every average. Also if I enter two ore more invalid scores in a group of scores such as : -23 115 98 the program only evaluates the first invalid score it sees. The output is supposed to look like this:
the scores are: -23 115 98
-23 is too small
115 is too big
the grades are invalid

I'm not sure what i'm doing wrong. Please help. This is my updated code

//This program will read in and evaluate a stuent's scores.
#include<iostream>
#include<fstream>
using namespace std;
void introduction();
bool aretheyvalid(int, int, int);
void classifyscores(int, int, int);
bool isvalid(int);
void aretheysame(int,int,int);
double findavg(int,int,int);
void makeletter(double);
ofstream fout;
int main()
{

int numgroups=0;
int validgroups=0;
int invalidgroups=0;
int score1, score2, score3;
char answer;


fout.open("program4.out");

introduction();

do {
cout<<"Type in the student's three scores from 0 to 100"<<endl;

cin>>score1;
cin>>score2;
cin>>score3;
fout<<"the scores are: "<< score1 <<" "<< score2<<" " <<score3<<endl;
if (aretheyvalid(score1, score2, score3)==true){
validgroups++;
fout<<"the grades are valid"<<endl;
classifyscores(score1,score2,score3);}

if(aretheyvalid(score1,score2,score3)==false){
invalidgroups++;
fout<<"the grades are invalid"<<endl;}
cout<<"Enter the next set of scores"<<endl<<endl;
numgroups++;
cout<<"Type in y to continue, n to stop"<<endl;
cin>>answer;
fout<<endl<<endl;
}while(answer=='y');

cout<<"We processed "<< numgroups <<" groups"<<endl;
cout<<"We processed "<< validgroups <<" valid groups"<<endl;
cout<<"We processed "<< invalidgroups<<" invalid groups"<<endl;

fout.close();
system("pause");
return 0;
}
//Function to send to the file a description of the prgogram with the grading
//scheme.
void introduction()
{
cout<<"This program will read in a student's 3 grades,"<<endl;
cout<<"evaluate if they are valid, it will check if they"<<endl;
cout<<"increase,decrease or stay the same,"<<endl;
cout<<"it will also calculate the average of the scores"<<endl;
cout<<"and assign a letter grade using a grading scheme:"<<endl;
cout<<"A(89-100)"<<endl;
cout<<"B(79-88)"<<endl;
cout<<"C(69-78)"<<endl;
cout<<"D(59-68)"<<endl;
cout<<"F(0-58)"<<endl;
return;
}

//Function to receive three test scores and determine if the scores are valid.
//I.e. between the range of 0 to 100.
//This function will send the grades to another function to determine if they are
//valid. Then it will return the results to the main program.
bool aretheyvalid(int score1, int score2, int score3)
{

if(isvalid(score1)==true){
if(isvalid(score2)==true)
if(isvalid(score3)==true)
return true;}
else
return false;

}

//Function to determine if each score is valid and print a message in each case.

bool isvalid(int num)
{

if(num<0){
fout<<num<<" is too small"<<endl;
return false;}
else if(num>100){
fout<<num<<" is too big"<<endl;
return false;}
else if(num>=0&&num<=100){
return true;}

}



//Function classify:
//Input:
//Three scores per student.
//Process:
//Calls a function to determine if all three scores are the same.
//Will evaluate the grades and print a message that describes the pattern
//followed by the grades.
//Calls a function to assign a letter grade to each student after receiving the
//average grade. The function will translate the average score into a letter grade.

void classifyscores(int score1, int score2, int score3)
{
double average;
aretheysame(score1, score2, score3);
fout<<"the average is "<<findavg(score1, score2, score3)<<endl;
makeletter(average);
return;
}



//Function to determine if the three scores are the same.
//The function will evaluate the grades and print a message.
//That describes the pattern of the grades:The same; increasing; decreasing;
//decreasing up and then down; down and then up; the first two are the same and
//the third is either higher or lower; the second are the same and the first is
//either higher or lower.

void aretheysame(int score1, int score2, int score3)
{
if(score1==score2&&score1==score3&&score2==score3)
fout<<"the grades are all the same"<<endl;
else if (score1<score2&&score2<score3)
fout<<"the grades are increasing"<<endl;
else if(score1>score2&&score2>score3)
fout<<"the grades are decreasing"<<endl;
else if(score1<score2&&score2>score3)
fout<<"the grades go up and then down"<<endl;
else if (score1>score2&&score2<score3)
fout<<"the grades go down and then up"<<endl;
else if(score1==score2&&score1<score3&&score2<score3)
fout<<"the first two grades are the same, and the third is higher"<<endl;
else if(score1==score2&&score1>score3&&score2>score3)
fout<<"the first two grades are the same, and the third is lower"<<endl;
else if(score2==score3&&score2>score1&&score3>score1)
fout<<"the last two grades are the same, and the first is lower"<<endl;
else if(score2==score3&&score2<score1&&score3<score1)
fout<<"the last two grades are the same, and the first is higher "<<endl;
return ;
}



//Function to find the average of the three scores
double findavg(int score1, int score2, int score3)
{
double average;
average=(score1+score2+score3)/3;
return average;
}



//Function to assign a letter grade to each students set of scores
void makeletter(double average)
{

if(average<58){
fout<<"the letter grade is F"<<endl;
else if(average>58 && average<68)
fout<<"the letter grade is D"<<endl;
else if(average>68 && average<78)
fout<<"the letter grade is C"<<endl;
else if(average>78 && average<88)
fout<<"the letter grade is B"<<endl;
else if(average>88 && average <100)
fout<<"the letter grade is A"<<endl;
return ;
}








Last edited on
Topic archived. No new replies allowed.