hey guys my program assigns a letter grade to a specfic percentage for an exam score and the problem is taht if i enter a number that is not specified in my if statements it should say invalid number but the program juss prints out the regular output with no letter grade instead any help would be very appreciated ty

#include <iostream>
#include <string>
using namespace std;
void GetScore(double&);
bool IsValid(double&);
string LetterGrade(double&);
void Display(double&, string&, bool&);
 
int main()
{
double exam_score;
bool check;
string letter_grade;
GetScore(exam_score);
check = IsValid(exam_score);
letter_grade = LetterGrade(exam_score);
Display(exam_score, letter_grade, check);
cout<<"Written By Ravi Mandair CPSC 1103 Section 10"<<endl;
return 0;
}
void GetScore(double& get_data)
{
cout<<"Please enter your exam score as a percentage\n"
<<"but without the actual % sign: ";
cin>>get_data;
return;
}
bool IsValid (double& data)
{
bool check_data;
if ((data >= 0) && (data <= 100))
check_data = true;
else
check_data = false;
 
return check_data;
}
 
string LetterGrade(double& exam_percentage)
{
string assign_grade;
if (exam_percentage >= 80 && exam_percentage <= 100)
assign_grade = 'A';
if (exam_percentage >= 60 && exam_percentage <= 79)
assign_grade = 'B';
if (exam_percentage >= 50 && exam_percentage <= 59)
assign_grade = 'A';
if (exam_percentage <= 49)
assign_grade = 'F';
 
return assign_grade;
}
void Display(double& exam_score, string& letter_grade, bool& check)
{
if (check = true)
cout<<"You recieved a score of "<<exam_score<<" which\n"
<<"is equivalent to the letter grade "<<letter_grade<<endl;
else 
cout<<"You entered a score of "<<exam_score<<endl
<<" ,invalid mark is out of range";
 
return;
}

To get around this problem, try placing the literal value before the variable while comparing.
Something like:

while( true == check )
// here if you make a mistake of using an assignement operator
// an error will be flaged

Since check is a boolean, you may as well do if ( check ) I personally dislike the swapping over of variables in an attempt to get an error message on the mis-use of =

OK what i did first of all is clear up your code.. Please start using spaces, ur killing us here! I simply sent in the check boolean variable into the IsValid function by reference. This clears up your whole program

#include <iostream>
#include <string>

using namespace std;

void GetScore(double &);
bool IsValid(double &, bool &);
string LetterGrade(double &);
void Display(double &, string &, bool &);
 
int main()
{
    double exam_score;
    bool check;
    string letter_grade;

    GetScore(exam_score);

    check = IsValid(exam_score, check);

    letter_grade = LetterGrade(exam_score);

    Display(exam_score, letter_grade, check);

    cout<<"Written By Ravi Mandair CPSC 1103 Section 10"<<endl;

    return 0;
}

void GetScore(double & get_data)
{
    cout << "Please enter your exam score as a percentage\n";
    cout << "but without the actual % sign: ";
    cin >> get_data;
    return;
}

bool IsValid(double & data, bool & check_data)
{
    if ((data >= 0) && (data <= 100))
        check_data = true;
            else
        check_data = false;
     
    return check_data;
}
 
string LetterGrade(double & exam_percentage)
{
    string assign_grade;
    if (exam_percentage >= 80 && exam_percentage <= 100)
        assign_grade = 'A';
    if (exam_percentage >= 60 && exam_percentage <= 79)
        assign_grade = 'B';
    if (exam_percentage >= 50 && exam_percentage <= 59)
        assign_grade = 'C';
    if (exam_percentage <= 49)
        assign_grade = 'F';
 
    return assign_grade;
}

void Display(double & exam_score, string & letter_grade, bool & check)
{
    if (check == true)
    {
        cout << "You recieved a score of " << exam_score << " which\n";
        cout << "is equivalent to the letter grade " << letter_grade << endl;
    } else 
    {
        cout << "You entered a score of " << exam_score;
        cout << " ,invalid mark is out of range" << endl;
    }
 
    return;
}
This article has been dead for over six months. Start a new discussion instead.