Value returning and non value returning functions

Hey, homework help, so I'm only asking if anyone can tell me which section of my code needs fixin'.

My program has 2 non main functions. One that takes a number from 0 to 100(such as a school grade), assigns it to a letter A-F.
Then that letter is supposed to get plugged into the void function to spit out a gif and a statement.

However, it simply runs and only accepts the input for the number grade.

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
62
63
64
65
  #include <iostream>
using namespace std;


int getInt (int x) { 
      
	if (x >= 90){
	    x = 'A';
	}
	if (x >=80 && x <= 89){
	    x = 'B';
	}
	if (x >=70 && x <=79){
	    x = 'C';
	}
	if (x >=60 && x <=69){
	    x = 'D';
	}
	if (x < 60){
	    x = 'F';
	}
    return x;
}


    void giveGradeFeedback(char letter) {
    string path = "https://vignette.wikia.nocookie.net/sailormoon/images/";

    
    if (letter == 'A') {
        cout << "You did excellent!" << endl;
        cout << "<img src='" << path << "8/8d/Burns-excellent.gif'>" << endl;
    }
    if (letter == 'B') {
        cout << "You did good!" << endl;
        cout << "<img src='" << path << "/e/e8/123_StarTrekhighfive.gif'>" << endl;
    }   
    if (letter == 'C') {
        cout << "You did average." << endl;
        cout << "<img src='" << path << "/a/a9/6d899681ac5be99a97b3b6220a576b10.gif'>" << endl;
    }
    if (letter == 'D') {
        cout << "At least you are done." << endl;
        cout << "<img src='" << path << "d/d7/D52.gif'>" << endl;
    }
    else  {
        cout << "You can do better." << endl;
        cout << "<img src='" << path << "/8/8c/120.gif'>" << endl;   
    } 
}

int main()
{
    cout << "What was the score: ";
    int score;
    cin >> score;  
   
    
    getInt (score);

    int letter = score;
    
    giveGradeFeedback(letter);  
}


If I change the final if statement in the void function to an else, I of course have it cout the parameters for an F, so I know that there is a problem between my value and non value returning functions.
Last edited on
it works like this


int letter = getInt (score);
giveGradeFeedback(letter);

your code throws away the result of getInt when you call it. You MUST save the result into a variable if you want to keep it. getInt does NOT modify score, it returns a new value and does not touch score (it just reads it).
Last edited on
The use of functions: http://www.cplusplus.com/doc/tutorial/functions/


The getInt() returns an int that has a number that corresponds some character. Should it rather return a char?

The giveGradeFeedback() expects char parameter, but you call it with int value.


1
2
3
4
5
6
if ( condA )
{
  // something
}

if ( condB )

These if are independent. The condB is evaluated no matter what the condA was.

In you getInt() you do modify the x in the if's bodies. In other words, if condA is true, you do change the x and then the condB could be true too. Is that a logical way? Perhaps you should have a second variable for the result?

1
2
3
4
5
6
if ( condA )
{
  // foo
}
else
if ( condB )

These if's are dependent. If condA is true, then condB is never evaluated. It is skipped even if foo would change variables that the condB depends on.

If condA is false, then condB is evaluated, just like with the independent ifs.
Topic archived. No new replies allowed.