Well, I just started programming in C++, this is my 3rd day and I finally built my first basic program. Just a basic calculator that takes two numbers and either multiplies, divides, adds, or subtract them. I do not know how to make a menu in C++ and I'm just getting into concepts. I would like to know how do I make If else statements that display an error message for an input that isn't 1, 2 , 3, or 4 for the integer select. This is for my 11th grade computer science class that started a week ago.

#include <iostream>
int x;
int y;
int z;
void addFunc();
void subFunc();
void divFunc();
void multFunc();
using namespace std;

int main()
{
    int select;
    cout << "Welcome to calculator!" << endl << "You have four options" << endl << "Type 1 for add." << endl << "Type 2 for subtract." << endl << "Type 3 for divide." << endl << "Type 4 for multiply." << endl;
    cin >> select;
    if(select == 1)
    {
        addFunc();
        return 0;
    }
    else if(select == 2)
    {
        subFunc();
        return 0;
    }
     else if (select == 3)
    {
        divFunc();
        return 0;
    }
    else(select == 4);
    {
        multFunc();
        return 0;
    }
    return 0;
}

void addFunc()
{
    cout << "Choose a number to add." << endl;
    cin >> x;
    cout << "Choose another number to add" << endl;
    cin >> y;
    z = x + y;
    cout << "Your answer is " << z << endl;
}

void divFunc()
{
    cout << "Choose a number to divide." << endl;
    cin >> x;
    cout << "Choose another number to divide" << endl;
    cin >> y;
    z = x / y;
    cout << "Your answer is " << z << endl;
}

void subFunc()
{
    cout << "Choose a number to subtract." << endl;
    cin >> x;
    cout << "Choose another number to subtract." << endl;
    cin >> y;
    z = x - y;
    cout << "Your answer is " << z << endl;
}

void  multFunc()
{
    cout << "Choose a number to multiply." << endl;
    cin >> x;
    cout << "Choose another number to multiply." << endl;
    cin >> y;
    z = x * y;
    cout << "Your answer is " << z << endl;
}

Just a suggestion but because you're using a menue, why not use a switch statement instead?

For example:

#include <iostream>
int x;
int y;
int z;
void addFunc();
void subFunc();
void divFunc();
void multFunc();
using namespace std;

int main()
{
    int select;
    cout << "Welcome to calculator!" << endl << "You have four options" << endl << "Type 1 for add." << endl << "Type 2 			  for subtract." << endl << "Type 3 for divide." << endl << "Type 4 for multiply." << endl;
    cin >> select;
    
	switch (select)
	{
		case 1:
			addFunc();
		break;
		
		case 2:
			subFunc();
		break;
		
		case 3:
			divFunc();
		break;
		
		case 4:
			multFunc();
		break;
		
		default:
			cout << "Your number was not between 1-4";
		break;
		
	}
    return 0;
}

void addFunc()
{
    cout << "Choose a number to add." << endl;
    cin >> x;
    cout << "Choose another number to add" << endl;
    cin >> y;
    z = x + y;
    cout << "Your answer is " << z << endl;
}

void divFunc()
{
    cout << "Choose a number to divide." << endl;
    cin >> x;
    cout << "Choose another number to divide" << endl;
    cin >> y;
    z = x / y;
    cout << "Your answer is " << z << endl;
}

void subFunc()
{
    cout << "Choose a number to subtract." << endl;
    cin >> x;
    cout << "Choose another number to subtract." << endl;
    cin >> y;
    z = x - y;
    cout << "Your answer is " << z << endl;
}

void  multFunc()
{
    cout << "Choose a number to multiply." << endl;
    cin >> x;
    cout << "Choose another number to multiply." << endl;
    cin >> y;
    z = x * y;
    cout << "Your answer is " << z << endl;
}

I was always taught that it wasn't wise to use if statements if you have a lot of options =)!

Alternatively, if you wanted to your use if statements, just try this:

#include <iostream>
int x;
int y;
int z;
void addFunc();
void subFunc();
void divFunc();
void multFunc();
using namespace std;

int main()
{
    int select;
    cout << "Welcome to calculator!" << endl << "You have four options" << endl << "Type 1 for add." << endl << "Type 2 for subtract." << endl << "Type 3 for divide." << endl << "Type 4 for multiply." << endl;
    cin >> select;
    if(select == 1)
    {
        addFunc();
        return 0;
    }
    else if(select == 2)
    {
        subFunc();
        return 0;
    }
     else if (select == 3)
    {
        divFunc();
        return 0;
    }
    else if(select == 4)
    {
        multFunc();
    }
	else{
	  cout << "Number not between 1-4";
	}
    return 0;
}

void addFunc()
{
    cout << "Choose a number to add." << endl;
    cin >> x;
    cout << "Choose another number to add" << endl;
    cin >> y;
    z = x + y;
    cout << "Your answer is " << z << endl;
}

void divFunc()
{
    cout << "Choose a number to divide." << endl;
    cin >> x;
    cout << "Choose another number to divide" << endl;
    cin >> y;
    z = x / y;
    cout << "Your answer is " << z << endl;
}

void subFunc()
{
    cout << "Choose a number to subtract." << endl;
    cin >> x;
    cout << "Choose another number to subtract." << endl;
    cin >> y;
    z = x - y;
    cout << "Your answer is " << z << endl;
}

void  multFunc()
{
    cout << "Choose a number to multiply." << endl;
    cin >> x;
    cout << "Choose another number to multiply." << endl;
    cin >> y;
    z = x * y;
    cout << "Your answer is " << z << endl;
}

Edited 4 Years Ago by phorce: n/a

Comments
Nice one....

Oh my god. You are a life saver. Time to read up on switch and case. Thanks a lot.

They are really easy to get your head around!

Best of luck with your project =) If this thread is solved, please mark it that way and add rep points if you think someone has helped you!

:)

What you have done so far is right, but the next step is refactoring it to find better ways of doing things, alternatives you can use like Switch statements and better practices to reduce the amount of code.

For example you used a criteria statement on the last 'else', generally you do not do that as that as it is the last one executed if none of the above are met. So it is best to use it for outputing an error, if the user input an option you did not list.

Way I did a calculator instead of having more functions for seperate calculations, I have just changed the assignment based on the operator in the switch statement and given the result at the end.

#include <iostream>

void calculator(), error();
double revision = 0.1;
double num_1, num_2, result;
char selection, operation;

int main()
{
	do {
	system("cls");
	std::cout << "Simple Calculator" << '\n' << "Revision: " << revision << '\n' << '\n';
	std::cout << "Input Option:" << '\n' << "(1) - Calculator" << '\t' << "(2) - Exit Program" << '\n';
	std::cin >> selection;

	switch (selection) {
	case '1' : calculator(); break;
	case '2': break;
	}
	} while (selection != '2');

	system("PAUSE");

}

void calculator()
{
	std::cout << "Format Applies As Such: (number operation number) eg. 2 + 2" << '\n';
	std::cin >> num_1 >> operation >> num_2;

	switch(operation) {
	case '+' : result = num_1 + num_2; break;
	case '-' : result = num_1 - num_2; break;
	case '*' : result = num_1 * num_2; break;
	case '/' : result = num_1 / num_2; break;
         default : error(); break;
	}

	std::cout << result;
}

void error()
{
	std::cout << "Invalid Command Aborting Program" << '\n';
         return 0;
}
This question has already been answered. Start a new discussion instead.