Need HELP

I have to format my program in a certain structure. The structure is
//Program Header
int main()
//Declarations
//Inputs
//Calculations/Processings
//Outputs
//Exit

Here's my code:
I just need help figuring out what goes where.
Thanks in advance

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
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

#include <stdlib.h>

using namespace std;

static void do_scores(string name)
{
	string score;
	int i;
	int scores[10];
	int total = 0;
	double avg;
	const char *grade;
    
	for (i = 0; i < 10; i++) {
		cout << "Score " << setw(2) << i + 1 << "> ";
		getline(cin, score);
		stringstream(score) >> scores[i];
		total += scores[i];
	}
    
	avg = total / 10.0;
	switch ((int)avg) {
        case 90 ... 100:
            grade = "A";
            break;
        case 80 ... 89:
            grade = "B";
            break;
        case 70 ... 79:
            grade = "C";
            break;
        case 60 ... 69:
            grade = "D";
            break;
        default:
            grade = "F";
	}
    
	cout << "Student [" << name << "] scored an average of [" <<
    setiosflags(ios::fixed) << setprecision(1) << avg <<
    "] graded at [" << grade << "]\n\n";
}

int main(int argc, char *argv[])
{
	string name;
    
	for (;;) {
		cout << "Enter name> ";
		getline(cin, name);
		if (name.compare("done") == 0)
			break;
        
		do_scores(name);
	}
    
	exit(EXIT_SUCCESS);
}
Last edited on
When posting code, please put it code tags. Look for the "Format:" section to the right of the edit box. Highlight your code and click the <> button. Here is your code indented and tagged. See my comments below:
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>
#include <iomanip>
#include <string>
#include <sstream>

#include <stdlib.h>

using namespace std;

static void 
do_scores(string name)
{
    string score;
    int i;
    int scores[10];
    int total = 0;
    double avg;
    const char *grade;

    for (i = 0; i < 10; i++) {
        cout << "Score " << setw(2) << i + 1 << "> ";
        getline(cin, score);
        stringstream(score) >> scores[i];
        total += scores[i];
    }

    avg = total / 10.0;
    switch ((int) avg) {
    case 90...100:
        grade = "A";
        break;
    case 80...89:
        grade = "B";
        break;
    case 70...79:
        grade = "C";
        break;
    case 60...69:
        grade = "D";
        break;
    default:
        grade = "F";
    }

    cout << "Student [" << name << "] scored an average of [" <<
        setiosflags(ios :: fixed) << setprecision(1) << avg <<
        "] graded at [" << grade << "]\n\n";
}

int 
main(int argc, char *argv[])
{
    string name;

    for (;;) {
        cout << "Enter name> ";
        getline(cin, name);
        if (name.compare("done") == 0)
            break;

        do_scores(name);
    }

    exit(EXIT_SUCCESS);
}


The code to compute the average isn't right. You have the right idea but you can't use a range in a switch statement. Do it like this:
1
2
3
if (avg >= 90)  grade = "A";
else if (avg >= 80) grade = "B";
else ...

Note that when checking for a B you don't have to check if avg<90 because you're guaranteed that it is. If it were greater than 90, then the code that checks for an "A" would have caught it.

Other than this, the code looks good but you need to rearrange it to match the structure that the assignment calls for. That structure is good because it separates input, calculation and output. It's always a good idea to separate these three because it's quite common to have to change one without having to change the others. For example, a phone app might have a different interface than a PC program.

Try rearranging your code into these functions:
1
2
3
4
5
6
7
8
9
10
11
12
// Get the user name and, if the name isn't "done", the scores.  Return false if
// the name is "done", true otherwise:
bool getInput(string &name, int scores[10]);

// Compute the average from a set of grades
float computeAverage(int scores[], numScores);

// Compute the letter grade for an average
string computeGrade(float average);

// output the students name, numeric average and letter grade
void output(const string &name, float average, const string &grade);


Now the main program looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, char *argv[])
{
    string name;
    int scores[10];

    while (getInput(name, scores)) {
        float avg = computeAverage(scores,10);
        string grade = computeGrade(average);
        output(name, avg, grade);
    }
    return(EXIT_SUCCESS);
}
should I leave the case functions in?
How would you do that since as dhayden said, you can't use ranges in case branches?
Last edited on
i just need the code to be rearranged into the format above. I need help with that
The case statements that you have don't compile for me. If they work for you then your compiler supports a non-standard syntax. That's why I'm saying that you should change switch statement to an if/then/else
One option if you want to keep the switch statement is to divide avg by 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	switch ((int) avg/10) 
    {
    case 10:    // 100
    case 9:     // 90-99
        grade = "A";
        break;
    case 8:     // 80-89
        grade = "B";
        break;
    case 7:     // 70-79  
        grade = "C";
        break;
    case 6:     // 60-69
        grade = "D";
        break;
    default:
        grade = "F";
    }
Topic archived. No new replies allowed.