How can I make this return the desired float back to main

How can I make this code return the desired float in the if else section back to main?. At the moment it's returning 1 all the time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include<iostream>

float myFunction(float theGradeScore);

int main()
{

float theScoreInput;

std::cout <<"Enter your grade.\n";
std::cin >>theScoreInput;


float theEndResult;

theEndResult=myFunction(theScoreInput);

std::cout <<" You scored " <<theEndResult<< " points " <<std::endl;

return 0;


}


float myFunction(float theGradeScore)
{
float theSend;

if(theGradeScore >101)
{
theSend = 5.0;
return theSend;
}
else if(theGradeScore >= 90 && theGradeScore < 100)
{
theSend = 4.0;
return theSend;
}
else if(theGradeScore >=80 && theGradeScore <=89)
{
theSend = 3.0;
return theSend;
}
else if(theGradeScore >=70 && theGradeScore <=79)
{
theSend = 2.0;
return theSend;
}
else if(theGradeScore >=60 && theGradeScore <=69)
{
theSend = 1.0;
return theSend;
}
else if(theGradeScore <59)
{
theSend = 0.0;
return theSend;
}

}
It works for me, but there should be an else statement, this catches input that doesn't satisfy any of the other conditions, like a default statement in a switch.

You can discover this yourself by turning on the warning levels when compiling. I used cpp.sh (the gear icon top right of the code) turn on all 3 warning levels. Warnings are your friend, they tell you what is wrong with the code.
Also, you might want to add an equals sign on line 30, 35 and line 55.
If the user inputs 100, 101 or 59 then it will return 100, 101 and 59 respectively.
Thanks for your help. Also how can I make it return the float properly, i.e. 4.0 points, not 4 points?

Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>

double score_to_grade( double score ) ;

int main()
{
    double score ;
    std::cout <<"Enter your score: " ;
    std::cin >> score ;

    const double grade = score_to_grade(score) ;

    // print grade with one digit after the decimal point
    std::cout << "your grade is: " << std::fixed << std::setprecision(1) << grade << '\n' ;
}

double score_to_grade( double score )
{
    if( score > 100 ) return 5.0 ;
    else if( score > 90 ) return 4.0 ;
    else if( score > 80 ) return 3.0 ;
    else if( score > 70 ) return 2.0 ;
    else if( score > 60 ) return 1.0 ;
    else return 0.0 ;
}
Also how can I make it return the float properly, i.e. 4.0 points, not 4 points?

Exactly how JLBorges did it. To clarify though, he included <iomanip> which allows you to use the fixed and setprecision() command. You can put whatever number you want into the parentheses following setprecision and it will output that number in decimal places.
i.e. setprecision(2) = 3.14, setprecision(3) = 3.141
Topic archived. No new replies allowed.