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?
#include <iostream>
#include <string.h>
usingnamespace 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;
}
elseif (ifs== 'M')
{
ifs = 10;
}
elseif (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*
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/