Hello,
I am making a console calculator app in Visual Studio 2012 RC (Beta). I want the user to select if they want addition, subtraction, multiplication or division. I am using the if statement, but when I debug it and choose, say, division, it adds it. Then I do, say, multiplication, it does subtraction. The user should press 1 to plus, 2 to minus, 3 to multiply and 4 to divide. This is the order:
Addition
Subtraction
Multiplication then
Division
My theory is that it is obeying sequence, not choice. Here is the source code:
------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main ()
{
int addition = 1;
int subtraction = 2;
int multiplication = 3;
int division = 4;
int a;
int b;
cout << "Welcome to Shahmeer's Calculator.\n";
cout << "Press 1 for addition, 2 for subtraction, 3 for multiplication & 4 for division: ";
if (cin >> addition)
{
cout << "Please enter a value\n";
cin >> a;
cout << "Please enter another value\n";
cin >> b;
cout << "The sum of these two values is " << a+b;
}
cout << "\nPress 1 for addition, 2 for subtraction, 3 for multiplication & 4 for division: ";
if (cin >> subtraction)
{
cout << "Please enter a value\n";
cin >> a;
cout << "Please enter another value\n";
cin >> b;
cout << "The difference between these two values is " << a-b;
}
cout << "\nPress 1 for addition, 2 for subtraction, 3 for multiplication & 4 for division: ";
if (cin >> multiplication)
{
cout << "Please enter a value\n";
cin >> a;
cout << "Please enter another value\n";
cin >> b;
cout << "The product of these two values is " << a*b;
}
cout << "\nPress 1 for addition, 2 for subtraction, 3 for multiplication & 4 for division: ";
if (cin >> division)
{
cout << "Please enter a value\n";
cin >> a;
cout << "Please enter another value\n";
cin >> b;
cout << a << " divided by " << b << " is " << a/b << "\n";
}
system ("pause");
return 0;
}
when you go: if (cin >> addition) it inputs a value into int addition, then returns the cin object. The cin object is not zero, so it evaluates as true.
Try this instead:
1 2 3 4 5 6 7 8 9 10 11
int choice;
cin >> choice;
if (choice == addition)
{
do addition stuff
}
elseif (choice == subtraction)
{
do subtraction stuff
}
elseif ...
#include <iostream>
usingnamespace std;
int main ()
{
int addition = 1;
int subtraction = 2;
int multiplication = 3;
int division = 4;
int a;
int b;
int choice;
cout << "Welcome to Shahmeer's Calculator.\n";
cout << "Press 1 for addition, 2 for subtraction, 3 for multiplication & 4 for division: ";
cin >> choice;
cout << "Please enter a value\n";
cin >> a;
cout << "Please enter another value\n";
cin >> b;
if (choice == addition)
{
cout << "The sum of these two values is " << a+b;
}
if (choice == subtraction)
{
cout << "The difference between these two values is " << a-b;
}
if (choice == multiplication)
{
cout << "The product of these two values is " << a*b;
}
if (choice == division)
{
cout << a << " divided by " << b << " is " << a/b << "\n";
}
system ("pause");
return 0;
}
A few improvements:
1. Make addition/subtraction/multiplication/division an enum type or a constint type instead of int. That'll prevent you from accidently writing into a container which is going to be a constant that you'll use for comparisons.
2. Use elseif instead of if when checking for subtraction, multiplication or division. Even better... use a switch statement!
Thanks for this! I am just a beginner (started only 2 days ago!). Well anyway it worked! Yeah, another thing I wanted to ask is is it possible to go to "Welcome to Shahmeer's Calculator" after each calculation and if so could you show me how to. It would be much appreciated. I also want to give you the source code of some apps (very simple ones) that I made!
This is an app that calculates the circumference of a circle after you give it the radius.
-----------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
#define PI 3.14159
int main ()
{
int r;
cout << "Welcome to Shahmeer's Circle Circumference Calculator\n"; cout << "Please enter the radius (r) of a circle: \n";
cin >> r;
cout << "The circumference of the circle is " << 2*PI*r << "\n";
system ("pause");
return 0;
}
This one calculates the average speed after you give it the distance and total time.
---------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystring;
float distance = 0;
int time = 0;
And this one is a sort of talking app.
----------------------------------------------
// Has a sort of conversation with the user
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string talk;
cout << "Hello, what is your name. My name is Alex Talk App. ";
getline (cin, talk);
cout << "I like the name " << talk << ".\n";
cout << "\n" << "How old are you? I guess applications don't really have ages. ";
getline (cin, talk);
cout << "\n" "Where do you live? I live in the HP Compaq 6720s laptop. ";
getline (cin, talk);
cout << "\n" << "That's a cool place! But you should see my country, Windows 7. It nearly got taken over by Windows 8! Phew! What are your hobbies? ";
getline (cin, talk);
cout << "\n" << "Interesting hobbies. Mine are , well I don't have hobbies, really. I talk to the user and then go to sleep! What kinds of food and drink do you like? ";
getline (cin, talk);
cout << "\n" << "Hmmm," << talk << " sounds delicious! I only consume electricity! It was very nice meeting you! Bye!\n ";
getline (cin, talk);
cout << "We will meet again next time!\n";
system ("pause");
return 0;
}