Program is printing '0' with my Result

So I'm taking an introduction to C++ class at the university (really enjoying it), and we were asked to develop a program where we create our own grading threshold, and the three values we enter must be (100>a>b>c>0). Then we must enter a numerical score (Ex. 77) then our return result, assuming you entered appropriate values for the threshold (Ex. 100 > 90(a) > 80(b) > 70(c) > 0), along with your numerical value, the program should print "You've entered a valid threshold" "You've received a C in the class." If you entered invalid values for the threshold (ex. 100> 90(a)>95(b)>80(c)>0) Then the program will print "You've entered an invalid threshold" It seems to be functional, however when the program prints my results, it comes out in an awkward order, and also prints "0" Someone said my code could be bugged, but I've rewrote and even tried it on my peers compilers so I'm not sure what could be the issue.any help would be great!

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
  #include <iostream>
#include <stdio.h>

using namespace std;

int score(int a, int b, int c, int g)
{

   if ((100>=g)&&(g>a))
        cout<<"Your final grade in the class is an A."<<endl;
   else if ((a>g)&&(g>=b))
        cout<<"Your final grade in the class is a B."<<endl;
   else if ((b>g)&&(g>=c))
        cout<<"Your final grade in the class is a C."<<endl;
   else
        cout<<"Your final grade in the class is an F."<<endl;
   return 0;
}

bool val(int a, int b, int c)

{
    if ((100>=a)&&(a>b)&&(b>c)&&(a>c)&&(c>0))
        return true;
    else
        return false;
}

int main()
{
    int a,b,c,g;
    cout<<"Enter the values for your threshold."<<endl;
    cin>>a>>b>>c;
    cout<<"Now enter your numerical score."<<endl;
    cin>>g;
    if (val(a,b,c))
        cout<<"Your threshold is valid."<<endl<<score(a,b,c,g)<<endl;
    else
        cout<<"Your threshold is invalid."<<endl;
    return 0;

}
The score function is returning 0, which is what will get printed out at line 37. Should that be a void function and return nothing (in which case it would need to be moved out of a cout << statement)?

e.g.
1
2
3
4
    if (val(a,b,c)){
        cout<<"Your threshold is valid."<<endl;
        score(a,b,c,g);
    }
Last edited on
You are doing cout << ... << score(a,b,c,g) << endl
there you are telling it to print the result of the function, and your function returns 0.


> when the program prints my results, it comes out in an awkward order
notice that score() has side effects (it prints to the screen)
so when trying to execute line 37, the compiler decided to eval score() first.
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
#include <iostream>

bool valid( int a, int b, int c ) // must be (100>a>b>c>0)
{ return 100>a && a>b && b>c && c>0 ; }

char grade( int a, int b, int c, int score )
{
    if( score >= a ) return 'A' ;
    else if( score >= b ) return 'B' ;
    else if( score >= c ) return 'C' ;
    else return 'F' ;
}

int main()
{
    std::cout << "enter thresholds for grades A, B, C (three integers):\n" ;
    int a, b, c ;
    std::cin >> a >> b >> c ;

    if( valid(a,b,c) )
    {
        std::cout << "your thresholds are valid.\n"
                  << "now enter your numerical (integer) score: " ;
        int score ;
        std::cin >> score ;

        std::cout << "your grade is " << grade( a, b, c, score ) << '\n' ;
    }

    else std::cout << "your thresholds are invalid.\n" ;
}
thanks for the help guys!, This clarified it so much :)
Topic archived. No new replies allowed.