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.

Dani AI

Generated

— a minimal, safe pattern is: prompt, read a full input line, normalize it, then branch to one of two actions. correctly pointed out that a branching construct can help; is right to question the use of goto. A simple loop plus if/else (or switch when you have many fixed cases) keeps flow clear and avoids goto.

The example below shows a short, modern approach that reads commands with std::getline, trims and lowercases the input, then performs one of two operations (add or multiply two integers). It handles EOF and basic invalid input without jumping around the program.

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

int main() {
    std::string line;
    while (true) {
        std::cout << "Choose 1=add, 2=mul, q=quit: ";
        if (!std::getline(std::cin, line)) return 0;
        auto start = line.find_first_not_of(" \t\r\n");
        if (start == std::string::npos) continue;
        auto end = line.find_last_not_of(" \t\r\n");
        std::string cmd = line.substr(start, end - start + 1);
        std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
        if (cmd == "q" || cmd == "quit") break;
        if (cmd == "1" || cmd == "add") {
            std::cout << "Enter two integers: ";
            if (!std::getline(std::cin, line)) return 0;
            std::istringstream iss(line); int a, b;
            if (iss >> a >> b) std::cout << "Sum: " << (a + b) << "\n";
            else std::cout << "Invalid numbers\n";
        } else if (cmd == "2" || cmd == "mul") {
            std::cout << "Enter two integers: ";
            if (!std::getline(std::cin, line)) return 0;
            std::istringstream iss(line); int a, b;
            if (iss >> a >> b) std::cout << "Product: " << (a * b) << "\n";
            else std::cout << "Invalid numbers\n";
        } else {
            std::cout << "Unknown option\n";
        }
    }
    return 0;
}

Notes and troubleshooting: avoid mixing operator>> and getline without clearing the leftover newline — this example uses getline consistently to prevent that class of bugs. Use std::istringstream or std::stoi with error checking to parse numbers. Start with small, testable steps: prompt, read raw input, then parse and act.

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

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.