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;
}