Output of IF statement


I have code but i'm having some difficulties.
I need to use the output of my IF statement in a calculation. But how can i extract the output from the IF statement in my code?
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
#include <iostream>
#include <cmath>

using namespace std;
int main(int argc, char *argv[]) {
	double x, y, z;
	cout<<"Please enter the student's three test grades:"<<endl;
	cin>>x>>y>>z;
	double maximum = max (x,y);
	cout<<"This is the max:" <<maximum<<endl;
	double maximum1 = max (y,z);
	if (x>maximum1)
	cout<<"This is the second max:" <<maximum1<<endl;
	else if (x<maximum1 && y>z && maximum==maximum1)
	cout<<"This is the second max:" <<z<<endl;
	else if (x<maximum1 && y<z && maximum==maximum1)
	cout<<"This is the second max:" <<y<<endl;	
	else 
	cout<<"This is the second max:" <<z<<endl; 
	int average=(((maximum+z)/2.0)+0.5);
	cout<<"The student's average is:" <<average<<endl;
	if (average>=90)
	cout<<"Student has an A score.";
	else if (average<90 && average>=80)
	cout<<"The student has a B score.";
	else if (average<80 && average>=70)
	cout<<"THe student has a C score.";
	else if (average<70 && average>=60)
	cout<<"The student has a D score.";
	else if (average<60)
	cout<<"The student has an F score.";
	return 0;
	
}

I need to use the output in my average.
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) What do you mean by "the output of my IF statement"? Do you mean the text that you're outputting to stdout, e.g.
THe student has a C score.
? Do you mean the grade itself? Do you mean the boolean value that a condition evaluates to?
If you look at the middle of the code, there's an IF statement followed by an average calculation: int average=(((maximum+z)/2.0)+0.5); I want to replace z with the output of the IF statement before it.

Sorry for the lack of coherence, assignment due soon.
Sort your test grades. It'll make your life easier.
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
#include <iostream>
#include <algorithm> //for swap

using namespace std;

template <typename T>
void sortThree(T& max, T& mid, T& min)
{
    if (max < mid) swap(max, mid);
    if (mid < min) swap(mid, min);
    if (max < mid) swap(max, mid);
}

int main(int argc, char *argv[])
{
    double x, y, z;
    cout<<"Please enter the student's three test grades:"<<endl;
    cin>>x>>y>>z;

    sortThree(x, y, z);
    //at this point, x holds the biggest value, y the middle value, and z holds the smalled value

    cout<<"This is the max:" << x <<endl;
    cout<<"This is the second max:" << y <<endl;

    int average= (x + y + z) / 3.0;
    cout<<"The student's average is:" <<average<<endl;

    if (average>=90)
        cout<<"Student has an A score.";
    else if (average>=80)
        cout<<"The student has a B score.";
    else if (average>=70)
        cout<<"THe student has a C score.";
    else if (average>=60)
        cout<<"The student has a D score.";
    else
        cout<<"The student has an F score.";

    return 0;
}
Topic archived. No new replies allowed.