//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>
usingnamespace 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?
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?
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.
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?
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.