Question type program for beginners

This is a discussion on Question type program for beginners within the C++ Programming forums, part of the General Programming Boards category; I made this program for someone who asked me for a little help with C++, it is for begginers and ...

  1. #1
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105

    Question type program for beginners

    I made this program for someone who asked me for a little help with C++, it is for begginers and shows how you could make a basic Question type program.

    Code:
    #include <iostream> // For cout, cin etc..
    #include <conio.h> 
    
    int Question; // We store the answer to the question here
    
    int main()  //Required in almost every program
    {
        cout << "What is the answer to 4 + 8?\n\n";  // We ask the user a question
        
        cout << "1) 13\n2) 12\n3) 14\n\n"; //The possible answers *note* \n is for a newline
     
        cin >> Question;        //The user needs to put an input here which will be stored
                                //in out int variable "QUESTION"
           
    if(Question  == 2)           //If the user entered the correct answer which is 12
    {                           //the program will do this piece of code
        cout << "\nYou answered correct\n";
        cout << "\nPress any key to continue...\n\n";
        getch();
    }
    
    else                        //If it was not what the if statment asked for do this
    {
        cout << "\nYou answered incorrectly\n";
        cout << "\nPress any key to continue...\n\n";
        getch();
    }
    
    cout << "Have a nice day\n";
    
    
    return 0;                       //Exit the program
    
    }                   //End Int Main()
    
    //Incase your not familar with using "if" and "else". if statments will preform
    //a piece of code if the code in the brackets "()" if true, below is an example
    //of an alternative way the if statments could of been used you can check for
    //more than one scenario just by adding "else if" below the if section:
    //  if(Question == 1)
    //  {
    //  [Code goes here]
    //  }
    //
    //  else if(Question== 2)
    //  {
    //  [Code goes here]        // In this program this would be the correct option
    //  }
    //
    //  else if(Question == 3)
    //  {
    //  [Code goes here]
    //  }
    //
    //  else
    //  {
    //  [Code goes here]
    //  }
    
    //It is always wise to include an else statment just incase the user enters something 
    //different to what you wanted them to enter, for example if they entered a char type 
    //instead of an int the program would not know what to do and something would
    //go wrong
    Last edited by Kirdra; 09-16-2002 at 03:46 PM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    254
    Code:
    system("pause");
    dont use system its almost as bad as goto

  3. #3
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    Do you have an alternative?

    I don't like my programs quitting before they start, with my comp I can't even see the text before it terminates.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    254

  5. #5
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    LoL =)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To compliment the if statement structure (or rather, suggest an alternative), here's the switch method:
    Code:
        switch (QUESTION)
        {
            case 1:
                cout <<"Close, but not quite\n";
                break;
            case 2:
                cout <<"Correct\n";
                break;
            case 3:
                cout <<"Hmmm, not bad, try again.\n";
                break;
            default:
                cout <<"Nah... you're loosing it now!\n";
                break;
        }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Isn't the FAQ a little weak at the pause section?
    Both examples requires conio.h, which isn't standard.

    There are better ways to pause a program.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Code:
    int QUESTION; // We store the answer to the question here
    I'd recommend to use full CAPS for macros only eg.
    Code:
    #define QUESTION 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  3. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 01:33 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 01:51 PM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 02:03 PM

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