Code Improvisation

Hi I decided to test my knowledge of C++ by setting out to do a project which allows users to enter in their grades for subjects, then for it to convert these grades to a number and add them together to get a final grade.

So far I have managed to write code that allows the user to enter in grades for 9 subjects then I used an if statment to test what letter was in the varaibe then reassign a number to that variable.

The problem however is that the code to evaluate the character in each variable is massive and will take up lots of space, can any suggest an easier way I don't know of to do this?

Also I output the value of the variable to make sure that the if statement worked and it output a weird simple instead of the values I put in, any idea what it could be?

any healp is greatly appreciated guys XD

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
#include <iostream>
#include <string.h>
using namespace std;




 char  ifs, ces, cs, pp, mn, itts, ct, mcs, cn; //Varaible to hold the results for each subject



void input_results(){   //function which allows users to manually input grades for each subjuct

	cout<<"Please enter in the grades you obtained in each unit. ";

	cout<<endl<<"Information Systems = ";
	cin>>ifs;

	cout<<endl<<"Communication and Employability Skills = ";
	cin>>ces;

	cout<<endl<<"Computer Systems = ";
	cin>>cs;

	cout<<endl<<"Procedural Programming = ";
	cin>>pp;

	cout<<endl<<"Managing Networks = ";
	cin>>mn;

	cout<<endl<<"IT Technical Support = ";
	cin>>itts;

	cout<<endl<<"Communications Technology = ";
	cin>>ct;

	cout<<endl<<"Maintaining Computer Systems = ";
	cin>>mcs;

	cout<<endl<<"Computer Networks = ";
	cin>>cn;

}



void conversion (){  //function to convert the stored char in each variable to an integer
	if (ifs== 'D')
	{
	ifs = 15;
	}

	else
		if (ifs== 'M')
	{
	ifs = 10;
	}
		else 
			if (ifs== 'P')
	{
	ifs = 5;
	}
}

	


void main (){  //main function

	input_results(); //call the first function to allow user to enter grades
	cout<<endl<<endl<<endl; // create space
	conversion();  //convert stored grades to 

	cout<<"value is: "<<ifs<<endl<<endl<<endl; //tests to see if the "if loop" works by displaying what integer was assigned to it


	system("pause");  // pause command prompt


}
For one thing, try to get rid of the global variables, and instead pass them into the functions as parameters.


The problem however is that the code to evaluate the character in each variable is massive and will take up lots of space, can any suggest an easier way I don't know of to do this?


Perhaps a switch-case would do? Maybe a hash table/map of some sort*

* http://en.wikipedia.org/wiki/Hash_table
Thanks passing parameters has always confused me but il go teach myself, Il do the same for hash tables.

i cringe whenever i come across system("pause") now after reading so much on it... try running the program without it, if not use:
1
2
std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

make sure you have #include <limits>

the system("pause") command is extremely memory dependent, and often tricks beginners into thinking of it as a common line of code. The code above is much more efficient for your programs, reference:
http://www.cplusplus.com/reference/iostream/istream/ignore/
Last edited on
Topic archived. No new replies allowed.