without using functions? there isn't much you can do to make it shorter, but... if you made a function for each of your questions, fed score through all of them (or use score as a global variable), you'd at least make it a little more straightforward, ie
int q1(int score);
int q2(int score);
//so on so forth
int main()
{
int score = 0;
q1(score)//where each q has a return value of score
q2(score)
q3(score)
//so on so forth
cout<< "Score= " << score <<endl;
cin.get();
return 0;
}
//q's might generally look like this
q1(int score)
{
int ans;
cout<< "1. one plus one=?\n";
cout<< "1. two 2. three 3. four 4. five\n";
cin>> ans;
if (ans==1)
{
cout<< "Correct \n";
score += 1;
}
else
{
cout << "WRONG!\n";
}
return score;
}
First, you can use a do while loop to keep repeating the question. Second, functions can make it shorter and more readable. Third, you can use a case switch instead of the nested else if statements.
@Intrexa, I don't think it was actually supposed to be the same question over and over, which is why i didn't suggest it. Though I admit, I didn't think of the case switch, but considering there is 1 answer and multiple other wrong answers, it's likely shorter to just use an if-else statement
Edit, i take that back, i forgot about the default switch. the if-else and switch statements would be the same length.
you could have an array of question/answer strings and run the questions in a loop and check again the given answer, but I still wonder why there shouldn't be function calls?
After double checking, you're right that he was asking each question just once. Which would lead to the next improvement. You should set it up like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Ask Question One
Test for Correct answer
If correct, dothiselse
Do something else
Start entirely newif/else segment
Ask second question
Test for Correct answer
If correct, dothiselse
Do something else
Another thing I noticed,
score=score+1;
That is the same as the following 2:
1 2
score++;
score+=1;
The first one increments score by 1 everytime, the second one will add whatever the right hand side value is to score (in this case 1)
@Cranium: Oh there definitely should be function calls. It is good practice. As to array/answer strings, I think those might be a little trickier and would just add to confuse the OP at this stage of the learning process, without adding a whole lot to general programming flow knowledge (which I think is most important at this point)
i mentioned that there was very little, so my apologies, because if you are absolutely resistant to function calls, then yes: switches would be the better choice.
@cranium, a struct might actually be easier to set up for that. but i agree with intrexa, too much for someone at this stage