Variable not been incremented

Here my code for my basic program

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  //First crappy program by Harry with C++
//Need to make the array more dynamic possibly...

#include "stdafx.h"; //Must be before the oostream when compiling basic program
#include <iostream>
#include <string>
using namespace std;

//Global variable declared here
int numOfPoints = 0;
char grades[3]; //3 A-level results/grades
string name;

void getName() 
{
		cout << "Please enter your name please: ";
		cin >> name;
		if (name.length() >= 20 || name.length() <= 1) {
			do { //if bigger than 20
				cout << "Please enter your name again (< 20 & > 1): ";
				cin >> name;
			} while (name.length() > 20 && name.length() <= 1);
		}
}

void getGrades()
{
	cout << "We're now going to get the grades from you..." << endl;
	for (int i = 0; i <= 2; i++) { //0 to 2 (3 grades
		cout << "Please enter grade (A-U): ";
		cin >> grades[i];
	}
}

int calPoints(int numOfPoints) {	
	switch (grades[1] , grades[2] , grades[3]) {
		case 'A':
		case 'a':
			numOfPoints += 100;
			break;
		case 'B':
		case 'b':
			numOfPoints += 50;
			break;
		case 'C':
		case 'c':
			numOfPoints += 25;
			break;
		case 'D':
		case 'd':
			numOfPoints += 12;
			break;
		case 'E':
		case 'e':
			numOfPoints += 25;
			break;
		case 'U':
		case 'u':
			numOfPoints += 25;
			break;
		default:
			numOfPoints += 50;
	}

	cout << numOfPoints << endl; //Test if they actually been outputted
	return numOfPoints = grades[1] + grades[2] + grades[3];
}


//ANY METHODS BEFORE HERE
int main() //Main calling starts here
{
	cout << "Welcome to the first Visual Studio program by Harry Walker" << endl;
	getName();
	cout << endl << "Welcome, " << name << endl;

	getGrades();

	cout << endl << "Brilliant, we're just now going to calculate how many UCAS tarriff points that you have..." << endl;
	calPoints(numOfPoints);
	cout << "You have UCAS " << numOfPoints << " points" << endl;

	system("pause"); //keeps program from instantly crashing (readln in pascal)
	return 0;
}


The integer numOfPoints isn't incrementing just taking the default case 50, why is that? Could anyone help me?

Output:

http://i.imgur.com/BpNSuve.png
You're passing numOfPoints into calPoints() by copy. This means that the variable called numOfPoints inside the function is a copy of the one in the calling code. Changing the copy inside the function doesn't have any effect on the one in the calling code.

You need to either:

- pass the variable by reference
- or have the function return the new value to the calling code.
Thanks for that, I was being stupid. Is there a way that I could change the case statements to be more efficient because it looks badly hardcoded :/, any suggestions?
You're welcome! It's an easy thing to miss.

Your switch statement is equivalent to writing:

switch (grades[1])

Did you really mean to use the comma operator there? Are you sure you know what the comma operator does in C++?

An alternative for your case statements might be to use the toupper() function on the character you want to check, so that you only need case labels for the upper-case character.

Edit: Also, grades[3] is reading past the end of the array.
Last edited on
The comma will do it for each assigned element in the array correct?

Where would I add the toupper().
What do you think line 36 does?

Hint: grades[1] , grades[2] , does nothing.


Oh crap, so how would I add them two elements of the array to the switch?
Just use an if else cascade. Switches can't handle logical operators like ||.
The comma will do it for each assigned element in the array correct?

Nope.
http://www.cplusplus.com/doc/tutorial/operators/
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.

@MikeyBoy - Good catch on the out of bound reference. However, The rightmost term is what will be used as the switch variable.

so how would I add them two elements of the array to the switch?

Use a loop.
Ah k thanks, did that and it worsk for that now I think, but the general case statement is... messed idk why either.

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

int calPoints() {
	for (int i = 0; i < 3; i++) {
		switch (grades[i]) {
		case 'A':
			numOfPoints += 100;
			break;
		case 'B':
			numOfPoints += 50;
			break;
		case 'C':
			numOfPoints += 25;
			break;
		case 'D':
			numOfPoints += 12;
			break;
		case 'E':
			numOfPoints += 25;
			break;
		case 'U':
			numOfPoints += 25;
			break;
		}
	}

	return numOfPoints;
}


I updated the other method to turn them to upper case:

1
2
3
4
5
6
7
8
9
void getGrades()
{
	cout << "We're now going to get the grades from you..." << endl;
	for (int i = 0; i <= 2; i++) { //0 to 2 (3 grades
		cout << "Please enter grade (A-U): ";
		cin >> grades[i];
		toupper(grades[i]);
	}
}


Although despite this, it doesn't work when I enter lowercase values.
Never mind fixed it:

grades[i] = toupper(grades[i]);
toupper returns the converted character. So you'd need to do something like:

grades[i] = toupper(grades[i]);

Or, alternatively, in your function:

switch (toupper(grades[i])) {
Finally a lucrative and popular support forum, thanks guys, I hope to have a good understanding in the next 4 weeks or so.
Topic archived. No new replies allowed.