Home > Software engineering >  calling function on while loop
calling function on while loop

Time:12-01

I'm making a calculator program but I already encounter a problem. Well, my code is in a loop that will call a function to display the choices and then ask the user to pick, a/s/m/d are the choices. If the input is on the choices, it will proceed to the next step. Otherwise, it will loop and then call the function again.

#include <iostream>
using namespace std;

void home()
{
    cout << "\nChoose your operation:" << endl;
    cout << "\tType [A] for Addition" << endl;
    cout << "\tType [S] for Subtraction"<< endl;
    cout << "\tType [M] for Multiplication" << endl;
    cout << "\tType [D] for Division" << endl;
}

int main()
{
    char operation;
    bool no_operator = true;
    int design = 73;

    for (int i = 0; i < design; i  ){
        if (i == 25){
            cout << " WELCOME TO CALCULATOR ";
            i  = 22;
        }
        else i == 72 ? cout << "*\n" : cout << "*";
    }
    while (no_operator){
        home();
        cout << "\nOperation: ";
        cin >> operation;

        if (operation == 'A' || operation == 'a')
        {
            cout << "\nIt will going to add numbers";
            no_operator = false;
        }
        else if (operation == 'S' || operation == 's')
        {
            no_operator = false;
            cout << "\nIt will going to subtract numbers";
        }
        else if (operation == 'M' || operation == 'm')
        {
            no_operator = false;
            cout << "\nIt will going to multiply numbers";
        }
        else if (operation == 'D' || operation == 'd')
        {
            no_operator = false;
            cout << "\nIt will going to divide numbers";
        }
        else
        {
            cout << "\tInvalid Input: You must enter A/S/M/D only\n";
            //home();
        }
    }

    return 0;
}

My problem is it will run the '''home()''' in else statement even if the input is correct on the second loop.

I want to stop the '''home()''' to be called when the input is correct

CodePudding user response:

You program is working perfectly fine as the input is correct it does not show the home rather print the message it will going to divide etc.

CodePudding user response:

Your code works perfectly fine. Make sure you're inputting the correct letters. Also for this code, a "do while()" loop would be better.

  • Related