Array with strings+int

Hi! I have a last minute assignment to create a program that converts the input of a number of student grade scores into letter grades and store it in an array with;
string courses [5] = {physics, math... etc} and
int score[5];
string grade [5];
And then prints it out.
I've been coding for 11h straight and my brain isnt really doing much for me right now haha, so Id be super grateful for some advice on how to go about it to get rolling! ^^

Thank you!
closed account (48T7M4Gy)
Use the principle of parallel arrays and combine that with a cin for the score each time and then process that score with an if - else if - else cascade

If you bwant to be very efficient and the grades are single characters rather that A+, B- etc then grade[] can be char rather than string.

Of course the immediate start is #include, int main() stuff.

There ya go Proper, nearly all done :)
Thanks, how do i properly prompt cin for score though?
when I do; std::cout << "Enter mathematics score:";
std::cin >> score;
I get a error message. :/
closed account (48T7M4Gy)
I get a error message. :/

I'm not surprised Proper.
Assuming you created score as an array of int's say then you would have something like
int score[5] = {0}; to initiate it

And from there you have to refer to score as an element of the array and therefore use an index value.

What that means is if you want to 'load-up' the 3rd score you would write cin >> score[2];
2 because the indices start at 0.

If you want to load up all 5 then you need a for loop.
Ahh thank you, that makes all the sense haha! :-)

Does this look alright?

int main()
{
std::string courses[5] = {"Matematik","Svenska","Engelska","Historia","Fysik"};
int score[5];
char grade[5];
int count;

for( int count = 0; count < 100; count++ ) {
std::cout << "Enter mathematics score:";
std::cin >> score[0];
std::cout << "Enter Swedish score: ";
std::cin >> score[1];
std::cout << "Enter English score ";
std::cin >> score[2];
std::cout << "Enter History score: ";
std::cin >> score[3];
std::cout << "Enter Physics score: ";
std::cin >> score[4];
}
}
closed account (48T7M4Gy)
Where did u get 100 from??

Read this and see if it makes sense, Proper

1
2
3
4
5
for( int count = 0; count < 5; count++ ) 
{
   std::cout << "Enter " << courses[count] << " score:";
   std::cin >> score[count];
}


If you have 100 students then you need to do a bit more planning, possibly with a 2d array ;)
Last edited on
Haha ops copy pasted it, had already changed it actually didnt see it was wrong nrs sorry! Thank you! :P
Does anyone have any tips of an efficient way to convert the score to grade and input it to array?
: )
closed account (48T7M4Gy)
Why don't u try it yourself? Takes about 10 minutes if you plan your if - else if - else structure
Im just a little uncertain how to get it properly stored in the array, should i do it within the for loop or a second function?
closed account (48T7M4Gy)
U can do it with either. A function would be good

eg
1
2
3
4
5
6
7
8
9
10
11
12
char workOutGrade( const int aScore)
{
   char grade;
   if (aScore > 90)
      return 'A';
   else if (aScore ...
      return ...
   ...
   ...
   else
      return ...
}
Ahh great, many thanks, lifesaver! : )
Hmm what is wrong with this code?

int main()
{
std::string courses[5] = {"Matematik","Svenska","Engelska","Historia","Fysik"};
int score[5];
char grade[5];
int count;

for( int count = 0; count < 100; count++ ) {
std::cout << "Enter mathematics score: ";
std::cin >> score[0];
std::cout << "Enter Swedish score: ";
std::cin >> score[1];
std::cout << "Enter English score ";
std::cin >> score[2];
std::cout << "Enter History score: ";
std::cin >> score[3];
std::cout << "Enter Physics score: ";
std::cin >> score[4];
}

char CalculateGrade(const int gScore)
char grade;
if (gScore > 95)
return 'A';
else if (gScore > 90)
return 'B';
else if (gScore > 80)
return 'C';
else if (gScore > 70)
return 'D';
else if (gScore > 60)
return 'E';
else
return 'F';
}
gScore was not declared~. Sorry for the dumb questions, been awake for 20h and coding for 14h straight, just need to finish this last assignment but the hamster already fell out haha. -.-
1) code tags please

2) you need opening brackets for your CalculateGrade function. you have a closing but no opening

3) I would highly recommend you make use of that "grade" variable. And also have one return statement.
Ahh sorry! Very hard to avoid even the simplest mistakes right now. :P
This is what it looks like now but im getting the error 'a function definition is not allowed here before { token on line 30.
Also how do I get my CalculateGrade function to store the converted input data in the array?

Thanks so much for your help, pardon the stupid questions but can barely keep my eyes open haha.

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







int main()
{
std::string courses[5] = {"Matematik","Svenska","Engelska","Historia","Fysik"};
int score[5];
char grade[5];
int count;

for(int count = 0; count < 5; count++) {
std::cout << "Enter mathematics score: ";
std::cin  >> score[0];
std::cout << "Enter Swedish score: ";
std::cin >> score[1];
std::cout << "Enter English score ";
std::cin >> score[2];
std::cout << "Enter History score: ";
std::cin >> score[3];
std::cout << "Enter Physics score: ";
std::cin >> score[4];
}

char CalculateGrade(const int gScore){
   char grade;
   if (gScore > 95)
      return 'A';
   else if (gScore  > 90)
        return 'B';
     else if (gScore > 80)
        return 'C';
            else if (gScore > 70)
                return 'D';
                    else if (gScore > 60)
                        return 'E';
                            else
                         return 'F';
    }
return 0;
}

closed account (48T7M4Gy)
Does it work Proper? The if cascade I mean. There are many ways to do it and if you have tested it thoroughly then give it a tick. If I get a chance I'll try it myself but I doubt whether I can 'break' it. :)

I'm a bit worried about that 100 previously. Just make sure you only have to deal with one student.

grade[index] = CalculateGrade( score[index] ) sounds good to me.
Check it out :)
Last edited on
closed account (48T7M4Gy)
BTW Depending on your decision with returns. If you use return 'A' etc then char grade is not used and that line can be deleted. Or you can say grade = 'A' etc and return grade at the end. Probably a bit more efficient the 1st way but it is a 50/50 decision, especially with just one student.
Last edited on
And don't write functions in 'main' function do it outside.
You can either write an prototype of it before main function like this:
 
char CalculateGrade(const int );

and after main function you can fill it in.
or
write your whole function before main function.
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
#include <iostream>
#include <string>

char CalculateGrade(const int );

int main()
{
	std::string courses[5] = { "Matematik","Svenska","Engelska","Historia","Fysik" };
	int score[5];
	char grade[5];
	int count;

	for (int count = 0; count < 5; count++) {
		std::cout << "Enter mathematics score: ";
		std::cin >> score[0];
		std::cout << "Enter Swedish score: ";
		std::cin >> score[1];
		std::cout << "Enter English score ";
		std::cin >> score[2];
		std::cout << "Enter History score: ";
		std::cin >> score[3];
		std::cout << "Enter Physics score: ";
		std::cin >> score[4];
	}
	//like kemort said
	for (int i = 0; i < 5; i++)
	{
		grade[i] = CalculateGrade(score[i]);
	}

	return 0;
}

char CalculateGrade(const int gScore) {
	char grade;
	if (gScore > 95)
		return 'A';
	else if (gScore  > 90)
		return 'B';
	else if (gScore > 80)
		return 'C';
	else if (gScore > 70)
		return 'D';
	else if (gScore > 60)
		return 'E';
	else
		return 'F';
}
closed account (48T7M4Gy)
Thanks malemale777 thank goodness somebody was awake ;)
Topic archived. No new replies allowed.