-3

How do i use two characters (A and B) to control which part of the code is executed when the user enters either of them..? Forexaple when a user Enters A, go to a certain transaction type and ask the user to enter info for that kind of a transaction. If B, execute some other traction type and the user will be asked to enter info for such a transaction. I just started learning programming (C++)please..

Thanks

  • 4
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 04 '17 at 16:20
  • 1
    Here is [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Feb 04 '17 at 16:28

5 Answers5

2

C++ as an imperative programming language provides several control statements like if/else, while, switch or for to control the flow of your program. They are explained in your C++-book you're currently reading.

The Techel
  • 918
  • 8
  • 13
1

Try this.

#include <iostream>

int main() {

    char in;
    std::cin >> in;

    switch (in) {
        case 'A':
            // Do something.
            break;
        case 'B':
            // Do something else.
            break;
        default:
            std::cerr << "Invalid character." << std::endl;
            return 1;
    }

}
Sam Marinelli
  • 999
  • 1
  • 6
  • 16
1

There are a lot of ways in C++ to achieve selection structure.

  1. if else (basically every programming language have it)


if (a)
{
    //do something
}
else 
{
    //do something else
}
  1. switch control


switch (input)
{
case a:
     // do something
     break;
case b:
     // do something else
     break;
default:
     // default when the user input is not expected a or b
}
  1. labels and goto keyword


int main(void)
{
    //something....
    if (a) goto label_a;
    else if (b) goto label_b;
label_a:
    //something...
    goto end;
label_b:
    //something else
    goto end;
end:    
    return 0;
}
  1. Function calls


void first() { /*something*/ }
void second() { /*something else*/ }

int main(void)
{
    //your previous codes
    if (a) 
    {
        first();
    }
    else
    {
        if (b)
        {
            second();
        }
    }

    return 0;
}


More things to read: CPlusPlus Statements and Controls
Or you can invest some money to buy a good C++ Reference books like C++ Primer

Zhou Zhi Hua
  • 371
  • 1
  • 10
0

One possible solution is:

#include <iostream>
#include <string>

void input_A() {
    //Do things after input A
    std::cout << "You used function A" << std::endl;
}

void input_B() {
    //Do things after input B
    std::cout << "You used function B" << std::endl;
}

void input_recv(std::string input)
{
    // Use if condition or switch statement for input evaluation
    if (input == "A"){
        input_A();
    }
    else if(input == "B"){
        input_B();
    }
    else {
        std::cout << "No valid input" << std::endl;
    }
}

int main()
{
    std::string input = "";
    std::cout << "Input: ";

    // Wait for input
    std::getline(std::cin, input);

    // Evaluate input
    input_recv(input);

    return 0;
}
Nils
  • 2,665
  • 15
  • 30
0

Thanks to this community. A switch/ case control worked for me for now. I am also trying out other controls mentioned here. Thanks everyone who helped...