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.

Recommended Answers

All 3 Replies

I recommend you check out this thread I found by doing a simple search. Hope it helps!

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 :)

Nice, but could you have done it without the goto ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.