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



LinkBack URL
About LinkBacks



its almost as bad as goto
Have a nice day.