Simplifying Codes

Is it possible to shorten this code:

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
//Two questions are available.
#include <iostream>
using namespace std;
int main()
{
    int ans, score=0;
    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=score+1;
        cout<< "2. 1+1=? \n";
        cout<< "1.two     2.three      3.four     4.five \n";
        cin>> ans;
        if (ans==1)
        {
            cout<< "Correct \n";
            score=score+1;
        }
        else
        {
            cout<< "Wrong. The correct answer is two \n";
        }
    }
    else
    {
        cout<< "Wrong. The correct answer is two \n";
        cout<< "2. 1+1=? \n";
        cout<< "1.two     2.three      3.four     4.five \n";
        cin>> ans;
    if (ans==1)
    {
        cout<< "Correct.\n";
        score=score+1;
    }
    else
    {
        cout<< "Wrong.The correct answer is two.\n";
    }
    }
    cout<< "Score= " << score <<endl;
    cin.get();
    return 0;
}


Can it be without using functions? I can't imagine to make a long code with just 10 questions.
closed account (4Gb4jE8b)
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
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
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;
}


i gave you a lot to base it off of, good luck.
Last edited on
Yes, there are a few things.

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.

http://www.cplusplus.com/doc/tutorial/control/

Read that for information on loops and switches.
closed account (4Gb4jE8b)
@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.
Last edited on
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, do this
 else
        Do something else


Start entirely new if/else segment
Ask second question
Test for Correct answer
       If correct, do this
else
        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 want to know how to do it aside from using functions,but you said there is no way to simplify it. Thanks.
Last edited on
I mentioned 2 things that would simplify and shorten your code. Switches and removing the unnecessary nested if else statement.
closed account (4Gb4jE8b)
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
Topic archived. No new replies allowed.