How to have multiple input and multiple output

Oct 9, 2021 at 9:42am
Hi, we were given the task to create a program where the user has to input 10 grades and the output is supposed to give the corresponding grading letter. I have tried and make the code for it but I can't seem to create the multiple input and multiple output. Below is the sample input and output and my code.

Input:
95
100
99
96
98
97
94
94.5
90
88

output:
95=a
100=a
99=a
96=a
98=a
97=a
94=b
94.5=b
25=x
88=c

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
  #include <iostream>
using namespace std;
char getLetter(double);
int main (){
	
	double Numgrade;
	cin >> Numgrade;
	cout << Numgrade << "=" << getLetter(Numgrade);
	return 0;
	
}
char getLetter (double grade){
	if (grade >= 95 && grade <= 100){
		return 'A';
	}
	if (grade >= 90 && grade <= 94.99){
		return 'B';
	}
	if (grade >= 85 && grade <= 89){
		return 'C';
	}
	if (grade >= 80 && grade <= 84){
		return 'D';
	}
	if (grade >= 75 && grade <= 79){
		return 'E';
	}
	if (grade >= 40 && grade <= 74){
		return 'F';
	}
	else{
		return 'X';
	}
}

Oct 9, 2021 at 9:53am
Oct 9, 2021 at 8:12pm
@noir, how do you do things multiple times? ... You use a loop!

Put a simple for-loop around lines 7 and 8.
Oct 10, 2021 at 12:15am
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
#include <iostream>

using namespace std;

char getLetter(double);

int main ()
{
    double Numgrade{0};
    
    int grade_list[10];
    
    for(int i = 0; i < 10; i++)
    {
        cin >> Numgrade;
        grade_list[i] = Numgrade;
    }
    
    for(int i = 0; i < 10; i++)
    {
        cout << grade_list[i] << "=" << getLetter(grade_list[i]) << '\n';
    }
    
    return 0;
}

char getLetter (double grade)
{
    if (grade >= 95)
    {
        return 'a';
    }
    if (grade >= 90)
    {
        return 'b';
    }
    if (grade >= 85)
    {
        return 'c';
    }
    if (grade >= 80)
    {
        return 'd';
    }
    if (grade >= 75)
    {
        return 'e';
    }
    if (grade >= 40)
    {
        return 'f';
    }
    else
    {
        return 'x';
    }
}



95
100
99
96
98
97
94
94.5
90
88
95=a
100=a
99=a
96=a
98=a
97=a
94=b
94=b
90=b
88=c
Program ended with exit code: 0
Oct 10, 2021 at 1:16pm
@againtry is there any substitution to what you use? I still havent encountered those kind of variables that uses [] as well as {}. If its okay may I know what they do?
Oct 10, 2021 at 1:27pm
@noir234
double Numgrade{0};
is equivalent to what you are probably used to, ie double Numgrade = 0; The curly braces were brought in to make initialization uniform with other variable declarations.

The square brackets are used in this case for an array (multiple) of items because your assignmnet description says to have multiple input and multiple output. You pile in a batch (array) of numbers, and then the program spits out the answers, one at a time.

The easiest way to read about arrays is at http://www.cplusplus.com/doc/tutorial/arrays/




Oct 10, 2021 at 1:28pm
Thank you so much about this :D
Topic archived. No new replies allowed.