0

I am completely new to C++. I want to know how to make a simple text program that takes user input and performs one of two operations depending on what the user entered. thanks for all help.

4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Salem
0

In some cases, the 'switch' function may help you.

#include <iostream>
using namespace std;

int main()
{
   char UserChoice;

   //Declare 'inputSection' for the goto statement,
   // used further down
inputSection:

     //Tells the user to enter their choice
     cout << "Do you want to continue? [Y/N] :";

     //gets the user's input character (Y or an N)
     cin >> UserChoice;

     //Tells the computer what to do with whatever
     //character the user entered.
     switch(UserChoice)
       {
           //If the input is a 'Y' or a 'y'
          case 'Y' :
          case 'y' :
	/* CODE GOES HERE TO 
	CONTINUE WITH THE PROGRAM */
          break;

          //If the input is an 'N' or an 'n'
          case 'N' :
          case 'n' :
	The user does not want to continue,
                //so exit the program
	return 0;

          break;

          /* If the user doesn't enter a 'Y'
          or an 'N', do this.             */
         default :
	cout << "You must either enter a ";
	cout << "'Y' to continue, or an 'N' to quit." << endl << endl;

	//Wait until the user is ready (Prints out
	//"Press any key to continue...")
	system("pause");
			
	//Clear the screen
	system("cls");

	//go back to the input stage of the program
	goto inputSection;
         break;
    }
}

Let me know if it works :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.