-
My First Program
I've finally been able to write my first half usefull C++ program there are a few thing I would like to add but at the moment I don't know enough to be able to add them :) .
I have some problems with the code below, when you choose to add and enter the first number etc. then when the answer is displayed the else statement which I added to the program is executed even though I don't want it to be, any ideas?
Code:
#include <iostream>
int main()
{
int unsigned short DoAddOrMinus;
int long FirstNumber;
int long SecondNumber;
std::cout << "What would you like to do?\n";
//This is the code for the menu
std::cout << "1 - Add\n2 - Subtract\n\n";
std::cout << "Please make your selection: ";
std::cin >> DoAddOrMinus;
//This is the if statement which performs addition
if (DoAddOrMinus == 1)
{
std::cout << "\n\nWhat is the first number you want to add: ";
std::cin >> FirstNumber;
std::cout << "What is the second number you want to add: ";
std::cin >> SecondNumber;
std::cin.ignore(80,'\n');
std::cout << "\n\n";
std::cout << "The answer is: " << FirstNumber + SecondNumber << "\n";
}
//This is the code which preforms subtraction
if (DoAddOrMinus == 2)
{
std::cout << "\n\nWhat is the first number you want to subtract: ";
std::cin >> FirstNumber;
std::cout << "What is the second number you want to subtract: ";
std::cin >> SecondNumber;
std::cin.ignore(80,'\n');
std::cout << "\n\n";
std::cout << "The answer is: " << FirstNumber - SecondNumber << "\n";
}
//This is the code which is activated when someone does not enter a number from the menu
else
{
std::cout << "You should have entered a number from the menu.\n";
}
std::cin.get();
return 0;
}
-
Read the tutorial about switches they allow you to make long if statements.
Here is an example of my code:
Code:
#include <iostream>
int multiply(int x,int y)
{
return x*y;
}
int divide(int x, int y)
{
return x/y;
}
int add(int x, int y)
{
return x+y;
}
int subtract(int x, int y)
{
return x-y;
}
using namespace std;
int main()
{
char op='c';
int x,y;
while (op!='e')
{
cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
cin>> op;
switch(op)
{
case '+':
cout<<"Enter the numbers you would like to add."<<endl;
cin>>x;
cin>>y;
cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
break;
case'-':
cout<<"Enter the numbers you would like to subtract."<<endl;
cin>>x;
cin>>y;
cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
break;
case'/':
cout<<"Enter the numbers you would like to divide."<<endl;
cin>>x;
cin>>y;
cout<<x<<"/"<<y<<divide(x,y)<<endl;
break;
case'*':
cout<<"Enter the numbers you would like to multiply."<<endl;
cin>>x;
cin>>y;
cout<<x<<"*"<<y<<multiply(x,y)<<endl;
break;
case'e':
return 0;
default:
cout<<"sorry, try again."<<endl;
}
}
return 0;
}
case replaces if which makes everything a lot easier.
As for the else loop I don't know why it won't work.
-
Your else statement will only operate properly if DoAddOrMinus == 2 because that is the only if statement it is attached to. If DoAddOrMinus == 1 the applicable if statement will execute it's code. But the next if statement only evaluates DoAddOrMinus == 2, and anything else will trigger the else statement.
Hint...lookup the else if evaluation statement.
Code:
if (DoAddOrMinus == 1)
{
// some code
}
.
.
if (DoAddOrMinus == 2) // See Hint above
{
// some code
}
//This is the code which is activated when someone does not enter a number from the menu
else
{
// This else is a logic error
}