Jan 15, 2018 at 2:30pm UTC
why does it not return the float value and return true or false instead.
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
#include<iostream>
using namespace std;
float myFunc(float );
float theProg();
int main(){
theProg();}
float theProg(){
float score;
float theScore;
cout << "Enter your score.\n" ;
cin >> score;
theScore=myFunc(score);
cout << "You scored " << theScore << endl;
}
float myFunc(float score){
if (score >101){
cout << "Out of range.\n" ;}
else if (score <=90 && score >=100){
return 4.0;}
else if (score <=80 && score >=89){
return 3.0;}
else if (score <=80 && score >=79){
return 2.0;}
else if (score <=60 && score >-69){
return 1.0;}
else if (score <=0 && score >= 59){
return 0;}
}
Last edited on Jan 15, 2018 at 2:57pm UTC
Jan 15, 2018 at 2:39pm UTC
Look again at your if statements.
Can you tell me under what conditions score <=90 && score >=100
will ever be true?
Last edited on Jan 15, 2018 at 2:39pm UTC
Jan 15, 2018 at 2:59pm UTC
sorry I'm low level dyslexic, cheers for not making fun of me :-)
Jan 15, 2018 at 3:06pm UTC
No worries! I wasn't sure if it was just a typo, or whether you'd actually misunderstood the meanings of the operators, which is why I asked the question.
Jan 15, 2018 at 6:27pm UTC
Hi al8888, in addition, you also must return a value here, or otherwise account for it.
1 2 3 4
if (score >101){
cout << "Out of range.\n" ;
// error: nothing is returned!
}
Not returning a value from a function that expects to return a value invokes undefined behavior.
Perhaps have -1.0 as a return value signifying an error, or throw an exception.
Last edited on Jan 15, 2018 at 6:28pm UTC