Thread: Question type program for beginners

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 02:46 PM.

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, 12:33 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12: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, 01:03 PM