Simple quiz program problem

This is a discussion on Simple quiz program problem within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight That is due to integer division. You can cast one of the two operands to float ...

  1. #16
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by laserlight View Post
    That is due to integer division. You can cast one of the two operands to float or double as you divide.
    *head explodes*

    What do you mean?

    I fixed it by adding

    cout<< "You scored, " << correct / total << " percent\n";

    to the end, but now it always outputs "you scored 45 percent"

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main ()
    {
        int add;
        int sub;
        int div;
        int correct;
        int incorrect =0;
        int total =3;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
    
    
    if (add == 4)
    {
      ++correct;
      }
    else
    {
         if (add != 4)
          {
           ++incorrect;
           }
         else
          {
              if (sub == 0)
              {
                ++correct;
                }
              else
              {
                   if (sub != 0)
                   {
                     ++incorrect;
                     }
                   else
                   {
                        if (div == 5)
                        {
                          ++correct;
                          }
                        else
                        {
                            if (div != 5)
              {
                ++correct;
                }
                                       }
    
                        }
                   }
              }
         }
         {
         cout<< "You scored, " << correct / total << " percent\n";
    }
    system("Pause");
    return 0;
    }

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by paulb39
    What do you mean?
    Something like this:
    Code:
    cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
    Before we continue, I suggest that you indent your code properly, e.g.,
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        int add;
        int sub;
        int div;
        int correct;
        int incorrect = 0;
        int total = 3;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
    
        if (add == 4)
        {
            ++correct;
        }
        else
        {
            if (add != 4)
            {
                ++incorrect;
            }
            else
            {
                if (sub == 0)
                {
                    ++correct;
                }
                else
                {
                    if (sub != 0)
                    {
                        ++incorrect;
                    }
                    else
                    {
                        if (div == 5)
                        {
                            ++correct;
                        }
                        else
                        {
                            if (div != 5)
                            {
                                ++correct;
                            }
                        }
                    }
                }
            }
        }
    
        {
            cout<< "You scored, " << correct / total << " percent\n";
        }
        system("Pause");
        return 0;
    }
    Note that those nested if-else statements would more typically be written as:
    Code:
    if (add == 4)
    {
        ++correct;
    }
    else if (add != 4)
    {
        ++incorrect;
    }
    else if (sub == 0)
    {
        ++correct;
    }
    else if (sub != 0)
    {
        ++incorrect;
    }
    else if (div == 5)
    {
        ++correct;
    }
    else if (div != 5)
    {
        ++correct;
    }
    Either way, with indentation, it is clear that the quiz taker can only get at most 1 question reported as correct. Even if he/she got the other questions correct, they would never be checked for correctness. Likewise, the quiz taker can get at most 1 question reported as incorrect.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Also, one more issue.
    Code:
    int correct;
    ...
    ++correct;
    You should initialize the variable before using it.

  4. #19
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by _Mike View Post
    Also, one more issue.
    Code:
    int correct;
    ...
    ++correct;
    You should initialize the variable before using it.
    This initializes it right?

    Code:
    int correct;
        int incorrect;
        int total;
        correct = 0;
        incorrect = 0;
        total = 3;

    Quote Originally Posted by laserlight View Post
    Either way, with indentation, it is clear that the quiz taker can only get at most 1 question reported as correct. Even if he/she got the other questions correct, they would never be checked for correctness. Likewise, the quiz taker can get at most 1 question reported as incorrect.
    I don't understand why though.

    The way I have it now, it always outputs "your score is 33 percent"

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        int add;
        int sub;
        int div;
        int correct;
        int incorrect;
        int total;
        correct = 0;
        incorrect = 0;
        total = 3;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
    
        if (add == 4)
        {
        ++correct;
        }
        else if (add != 4)
        {
        ++incorrect;
        }
        else if (sub == 0)
        {
        ++correct;
        }
        else if (sub != 0)
        {
        ++incorrect;
        }
        else if (div == 5)
        {
        ++correct;
        }
        else if (div != 5)
        {
        ++correct;
        }
        {
            cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
        }
        system("Pause");
        return 0;
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by paulb39
    This initializes it right?
    No, you should initialise it to 0:
    Code:
    int correct = 0;
    Quote Originally Posted by paulb39
    I don't understand why though.
    Trace the logic. For example, suppose that add == 4. Then ++correct. But the else branch will not be followed, hence the next thing that happens is that the result is printed. But suppose that add != 4. You know that add == 4 will evaluate to false, hence the else branch will be followed. Now, add != 4 evaluates to true, hence ++incorrect. But now the else branch here will not be followed, hence the next thing that happens is that the result is printed. Since either add == 4 or add != 4, there is no way that sub == 0 will ever get checked.

    Rather, you should have three separate if-else statements, each handling a question independently of the others.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by laserlight View Post
    Rather, you should have three separate if-else statements, each handling a question independently of the others.
    Woot, thank you, working now

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        int add;
        int sub;
        int div;
        int correct = 0;
        int incorrect = 0;
        int total = 3;
    
        cout << "Whats 2 + 2? ";
        cin >> add;
        cout << "Whats 2 - 2? ";
        cin >> sub;
        cout << "Whats 20 / 4? ";
        cin >> div;
    
        if (add == 4)
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (div == 5)
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (sub == 0)
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        {
            cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
        }
        system("Pause");
        return 0;
    }
    One question, for

    Code:
    cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
    Could you explain what it means? I don't understand what the "static_cast<int>" part is for, and why does correct need two parentheses in front of if instead of one?

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by paulb39
    I don't understand what the "static_cast<int>" part is for
    From what I see, you want to output an int rather than a double, so this a type cast to coerce the double to int. It truncates the double in the process (i.e., the portion after the decimal point is "chopped off"), but other than that it probably does what you want.

    Quote Originally Posted by paulb39
    and why does correct need two parentheses in front of if instead of one?
    Because I chose to use parentheses to group (correct * 100.0). This is actually unnecessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Quote Originally Posted by laserlight View Post
    From what I see, you want to output an int rather than a double, so this a type cast to coerce the double to int. It truncates the double in the process (i.e., the portion after the decimal point is "chopped off"), but other than that it probably does what you want.


    Because I chose to use parentheses to group (correct * 100.0). This is actually unnecessary.
    Thanks.

    Wrote another program, does same sort of thing but I tried to make it a little long/ more complicated.

    Got it to work in my first try, which is pretty awesome, but it doesn't calculate the percent correct, it always says "you scored 25 percent"

    I'm guessing its because I have int and char variables.

    Any ideas?

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        int mole;
        char gas_law[7];
        int num;
        char place_p[11];
        char place_v[1];
        char place_n[1];
        char place_r[1];
        char place_t[1];
        int correct = 0;
        int incorrect = 0;
        int total = 8;
    
        cout << "Write a number 1 - 10 ";
        cin >> num;
        cout << "What is the 16th letter in the alphabet? ";
        cin >> place_p;
        cout << "What is the 22nd letter in the alphabet? ";
        cin >> place_v;
        cout << "What is the 14th letter in the alphabet? ";
        cin >> place_n;
        cout << "What is the 18th letter in the alphabet? ";
        cin >> place_r;
        cout << "What is the 20th letter in the alphabet? ";
        cin >> place_t;
        cout << "What is the ideal gas law? (use all lower case) Hint: ^^ " ;
        cin >> gas_law;
        cout << "How many moles are in 80 grams of Calcium? Hint: calcium is Ca ";
        cin >> mole;
    
        if (gas_law == "pv=nrt")
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (place_p == "p")
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (place_v == "v")
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (place_n == "n")
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (place_r == "r")
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (place_t == "t")
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (mole == 2)
        {
        ++correct;
        }
        else
        {
        ++incorrect;
        }
        if (num == 3)
        {
        ++correct;
        cout << "You guessed the number correctly! Aren't you a genius...\n";
        }
        else if (num < 3)
        {
        ++incorrect;
        cout << "You should of gussed higher for the number!\n";
        }
        else if (num > 3)
        {
        cout << "You should of guessed lower for the number!\n";
        }
        {
            cout << "You scored, " << static_cast<int>((correct * 100.0) / total) << " percent\n";
        }
        system("Pause");
        return 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with a simple program
    By alfredd in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2009, 02:48 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 04:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21