Looking for help on a basic program

This is a discussion on Looking for help on a basic program within the C++ Programming forums, part of the General Programming Boards category; I'm a new member to the board and a fairly new member to programming. I picked up a book on ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    California
    Posts
    3

    Looking for help on a basic program

    I'm a new member to the board and a fairly new member to programming. I picked up a book on beginning programming and dabbled for QBasic for a while, but I've become really interested in C++ and started to teach it to myself. I haven't gotten very far, only enough to make simple programs that ask for input and store variables to display later...things of that sort.

    Anyways, I'm trying to make a little game that quizzes the user. It asks three multiple choice questions and waits for a response in between each (the subject matter of the questions is irrelevant at this point, I just want to get the thing running). I've gotten far enough where the program will ask the questions and take the input, but I'm running into variable problems.

    At the end of the program, I put in code to tell the user how many questions they got right out of three and then calculate and display the percentage as well. But no matter how I try storing variables for input, the program always displays the same ridiculous number (~4,000,000) out of three and an even larger one for the percent. Here is the code I've written:

    Code:
    #include <iostream>
    
    using namespace std; // So the program can see cout and endl
    
    int main()
    {
    int x;
    int y; 
    int z;
    char letter;
    int A = 1;
    int B = 2;
    int C = 3;
    int D = 4;
    int total;
    int average;
    int percent;
    
    
        //Question 1 code
        cout<<"1. Who was the first president of the United States of America?\n A. Washington B. Lincoln\n C. Jefferson D. Roosevelt\n";
        cin>>letter;
        //cin.ignore();
        if(letter == 1){
                  x == 1;
                  //endl; 
                  cin.ignore();
                  }
        else {
             x == 0;
             cin.ignore();
             }
       //Question 2 code
        cout<<"2. In what year were the North American colonies liberated?\n A. 1776 B. 1781\n C. 1783 D. 1795\n";
        cin>>letter;
        //cin.ignore();
        if(letter == 3){
                  y == 1;
                  //endl; might not be necessary
                  cin.ignore();
                  }
        else {
             y == 0;
             cin.ignore();
             }
       //Question 3 code
        cout<<"3. When were the slaves officially freed?\n A. 1861 B. 1863\n C. 1870 D. 1875\n";
        cin>>letter;
        //cin.ignore();
        if(letter == 2){
                  z == 1;
                  cin.ignore();
                  }
        else {
             z == 0;
             cin.ignore();
             }
        total == x + y + z;
        cout<<"\nGuess what?\n";
        cin.ignore();
        cout<<"You got " <<total <<" out of 3 questions correct!\n";
        average == total / 3;
        percent == average * 100;
        cout<<"That's "<<percent <<"% !!!";
        cin.get();
    }
    Could anyone offer any advice? I am looking for the right answer to my problem, but I would also appreciate some reasoning on where I went wrong or why it should be different. Thanks in advance to anyone who can help out!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, I think you have missed the level of cleverness in cin. When you use cin >> letter, and someone types A, you will not get the value of your A, B, C or D constants, you will get 'A'.

    So your comparisons if (letter = 1), etc, will all fail.

    You use x == 1, y == 1, which are comparisons to see if the variable is equal to a value (which is then ignored, becasue there's nothing to do with this "true-or-false" value).

    Since x, y and z are never actually given a value (they are just compared with 0 once each - 0 since all your compares of letter are comparing the wrong thing), the value is undefined, and you get whatever happens to be the content of that bit of memory - usually some strange large value.

    As to your cin.ignore() that you have plenty of, you should move those so that they are outside the if/else statement - that way you only need half as many, and the code is easier to maintain and understand. The goal is, as long as it's possible, to have as little code duplicated between the if and the else branch of a if-else statement - anything that is identical on both sides of the else should be moved outside the if-else-statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    California
    Posts
    3
    Ahh I see where I'm going wrong here. I knew I was complicating things with the A-D variables etc. But now the question I have is this: what should I replace "letter == 1" with in my if-else statements? Can letter == A? I was never clear on whether or not you could ask a question like that with a letter, hence all of the numbers I tried to store in A-D.

    Aside from that, thank you for the advice! I'm going to trim out some bits and pieces that now seem pretty unnecessary...

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    Try typecasting. Maybe that will work

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    letter == 'A'

  6. #6
    Registered User
    Join Date
    Sep 2007
    Location
    California
    Posts
    3
    Thanks for the responses, I've fixed the problem! I think my code was too excessive at the beginning, and I was trying to make things more confusing than necessary. Here is the final code I came up with:
    Code:
    int main()
    {
    float x = 0;
    char letter;
    
    
    
    
    
        //Question 1 code
        cout<<"1. Who was the first president of the United States of America?\n A. Washington B. Lincoln\n C. Jefferson D. Roosevelt\n";
        cin>>letter;
        //cin.ignore();
        if(letter == 'A'){
                  x++;
                  //endl; 
                  }
        else {
             }
        cin.ignore();
       //Question 2 code
        cout<<"2. In what year were the North American colonies liberated?\n A. 1776 B. 1781\n C. 1783 D. 1795\n";
        cin>>letter;
        //cin.ignore();
        if(letter == 'C'){
                  x++;
                  //endl; might not be necessary
                  }
        else {
             }
       cin.ignore();
       //Question 3 code
        cout<<"3. When were the slaves officially freed?\n A. 1861 B. 1863\n C. 1870 D. 1875\n";
        cin>>letter;
        //cin.ignore();
        if(letter == 'B'){
                  x++;
                  //endl; might not be necessary
                  }
        else {
             }
        cin.ignore();
        cout<<"\nGuess what?\n";
        cin.ignore();
        cout<<"You got " <<x <<" out of 3 questions correct!\n";
        cout<<"That's "<<( x / 3 )* 100 <<"% !!!";
        cin.get();
    }
    It took some logical thinking, but I completely understand the errors I made. Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. basic animation program
    By Noobie in forum C++ Programming
    Replies: 16
    Last Post: 02-17-2003, 05:33 PM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

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