i hae no idea whats wrong. the next two parts are also part of the program. when i put the loops after or before the percentage statement it eithers skips the letter grade or the percentage. help? please?
Not sure what you quite mean, could you please rephrase what your problem is.
Also before you output the grade why do you try to take the variable "total" from cin?
when i run the program it does not run the while loops. so it will say the percent that the test taker got but not the letter grade. and if i dont put the cin >> total there it wont display the percentage ??
I'm not sure how to fix your problem However, someone told me to just recheck all of my work when i had a problem i couldn't figure out.
I refined the code, made it more efficient and tidier to see what's going on and pretty much re-write the whole thing. It should've essentially worked exactly the same but i ended up doing something to fix it.
Basically go through your program, make it as efficient and well working as you can, then if you still get the same problem it's much easier to see and figure out what your code is doing!
cout << "20. 9090909090909090 * b^0 = x\n";
cout << "A) \n";
cout << "B) 9090909090909090\n";
cout << "C) Not enough information\n";
cout << "D) None of the above\n";
cout << "Enter the letter of the answer you have chosen. ";
cin >> choice;
switch(choice)
{
case'B':
{
cout << "Correct.\n";
++correct;
break;
}
default:
{
cout << "Incorrect. The answer is B.\n";
break;
}
}
The other thing is that if you are using cout then you should use endl like this:
cout << "Correct." << endl;
You have done it correctly later in the code.
Now, you have a lot code here - a bunch of code that is kind of similar for each question asked. The code is not the same because the questions are different.
A more elegant way to do this is to have a 2 dimensional array that holds the questions and their answers . Loop through the array to ask the question and process the answers.
It will be harder to do, but a much better solution.
To be honest it's not hard to do at all. It'd be wise to have a separate file with the code to declare the 2xArray with [0][i] being the questions and [1][i] being the answers to the questions (the separate file to keep your code clear).
Then you have a simple for loop asking questions taken from the 2xArray and asking for an answer.
@TheIdeasMan@ Switches are only better if you're expecting at least 3 different comparisons, here we only want CORRECT or INCORRECT, i.e. true/false
An array of structs would be even better than a 2d array, now that I think about it a bit more. The struct would hold the Question and the answers as strings, and this setup would be easier to reference than 2d array.
That is a C approach.
The C++ approach would be to have the Questions and answers in a <vector>, then make a <list> of the vectors. Use an iterator to cycle through the list to ask the Q's and checks the A's and keep tabs on the score.
If this is an assignment, I am not sure what your teacher's view is, - it could be construed as cheating a bit because C++ has a lot of stuff that makes things like this fairly easy. The whole point of your course might be to learn about arrays, structs etc.
@TheIdeasMan@ Switches are only better if you're expecting at least 3 different comparisons, here we only want CORRECT or INCORRECT, i.e. true/false
Fair enough, I have switches on the brain at the moment - If you read some of my other posts you will see why.
Ok a struct is like a record in a database 1 row of info. An array of structs is like a database table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct Person { //uses C++ string won't work in C
string FirstName; //The persons first name
string LastName; //The persons last name
string DateOfBirth; //in "dd/mm/yyyy" format
unsigned AgeInYears; // The age in years as of today
float Salary; //Annual Pay in dollars
};
struct Person Me;
struct Person You;
Me.FirstName = "Fred";
Me.LastName = "Smith";
Me.DateOfBirth = "01/01/1900";
Me.AgeInYears = 112;
Me.Salary = 1000000.00;
Check out the reference documentation for some other examples. typedef is handy
Cheers (I think i sort of get it, not enough to use it yet but i'll do some research)
:)
EDIT: Right, i've found out it's like an object that's able to hold a group of different types of objects. Then I am able to create an instance of this object to hold my values. I assume that this is correct (I'm checking cos the info's from wikipedia) as it seems to fit in nicely with your example.
If so this is indeed a good idea of how to do this. However it's obviously more benifitial with the more variables that's needed.
You can also do pointers to structs, this is handy for you because you are going to have an array of structs and a reference to an array index is a pointer.
struct Person { //uses C++ string won't work in C
string FirstName; //The persons first name
string LastName; //The persons last name
string DateOfBirth; //in "dd/mm/yyyy" format
unsigned AgeInYears; // The age in years as of today
float Salary; //Annual Pay in dollars
};
struct Person Me;
struct Person You;
Me.FirstName = "Fred";
Me.LastName = "Smith";
Me.DateOfBirth = "01/01/1900";
Me.AgeInYears = 112;
Me.Salary = 1000000.00;
struct Person* pPersonObj ; //a pointer to a Person struct
pPersonObj = &Me; //pointer initialisation, it now points to the Me Person struct
cout >> "My First name is " >> pPersonObj->FirstName >> endl;
//if you have an array of Person struct you can use the -> like this
PeopleArray[0]->FirstName = "Bevis";
Look for some examples on how to make an array of structs.
Yeah, i've been doing some reading of the downloadable pdf tutorial on this site.
It cover's quite a range including structs so i've learned enough about them to recognize what it does and how it's used in a program to be able to help if anyone has a problem (well, i can try :D )