#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int Operater;
int answer;
cout << "Hello, This Application is a calculator first you must enter a number." << endl;
cout << "Next The app will ask of you want to add, subtract, Multiply or divide. "<< endl;
cout << "Next you will enter the next number." << endl;
cout << "Once you hit ENTER the application will calculate an answer. "<< endl;
cout << "Lets Begin, Please enter a number"<< endl;
cin >> a;
cout << "Now type your operator 1=add 2=subtract 3=multiply 4=divide"<< endl;
cin.get();
return 0;
}
But once you type the number and hit enter the console closes Im not sure what to do to fix this and keep the console open.
It's best to just run a console program from the console. On windows, navigate to the executable directory, shift+rightclick into the directory, click open cmd here (or something like that).
Or WIN+R -> cmd
> cd >>your directory<<
Then just type the name of the executable (you may drop the .exe extension). On Mac - I have no idea, but there should be something like bash available. On any linux distro, you should already know how to operate a shell.
The problem is, when a console program is done its done. It shouldn't block (because console programs are meant to run from the console). There are ways to keep the console open, but they all go against the very idea of a console program.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int Operater;
int answer;
cout << "Hello, this application is a calculator first you mount enter a number." << endl;
cout << "Next you will enter the next number." << endl;
cout << "Next The app will ask of you want to add, subtract, Multiply or devide. "<< endl;
cout << "Once you hit ENTER the application will calculate an answer. "<< endl;
cout << "Lets begin, please enter a number"<< endl;
cin >> a;
cout << "Now chose your second number to use."<< endl;
cin >> b;
cout << "Now chose your operator 1=add 2=subtract 3=multiply 4=divide"<< endl;
cin >> Operater;
if (Operater = 1)
answer = a + b;
if (Operater = 2)
answer = a - b;
if (Operater = 3)
answer = a * b;
if (Operater = 4)
answer = a / b;
cout << "Your answer is ";
cout << answer;
cin.get();
getchar();
return 0;
}
I'm still kinda new to this kinda thing so I was wondering if someone could explain to me in the noobiest way possible how switch statements work and how it differs from an else statement.
Switch statements can be faster than if statements, for example:
1 2 3 4 5
switch (a + b){
case 1: cout << "1"; break;
case 2: cout << "2"; break;
deafult: cout << "Thats too high!";
}
is a little bit faster than
1 2 3
if (a + b == 1) cout << "1";
elseif (a + b == 2) cout << "2";
else cout << "Thats too high!";
In that case you only make the computation of what a + b is once. You might do something like:
1 2 3
c = a + b;
if (c == 1) ...
...
But now you have a variable c that you might not need again.
The if statements starting at line 27 are not the "fastest" though (keep in mind iHutch's correction). Here you run through 4 conditions no matter what. If the middle two started "else" and the last one was just "else" then the program wouldn't keep looking if it found a matching case.