I'm new to c++ ,ish.
I made a simple calculator and want to know if theres a way to either clean or shorten my code. Or if its better to declare and write EVERYTHING out like i like to do.
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
int main()
{
usingnamespace std;
int x=1;
cout<<"Widget360 Simple Calculator";
cout<<"V 1.0 Beta";
while (x <= 10)
{
string calc;
float numone = 0;
float numtwo = 0;
float num = 0;
cout<<"\n";
cout<<"\n";
cout<<"Please Enter A Value: ";
getline (cin, calc);
stringstream(calc) >> numone;
cout<<"\n";
cout<<"\n";
cout<<"Would you like to: add (1), subtract (2), divide (3), or multiply (4)?" << endl;
cout<<"\n";
getline (cin, calc);
stringstream(calc) >> num;
if (num == 1){
cout<<"\n";
cout<<"\n";
cout<<"What would you like to add: ";
getline (cin, calc);
stringstream(calc) >> numtwo;
cout<<"\n";
cout<<"\n";
cout<< "= " << numone + numtwo << endl;
}
if (num == 2){
cout<<"\n";
cout<<"\n";
cout<<"What would you like to subtract: ";
getline (cin, calc);
stringstream(calc)>>numtwo;
cout<<"\n";
cout<<"\n";
cout<<"= " << numone - numtwo << endl;
}
if (num == 3){
cout<<"\n";
cout<<"\n";
cout<<"What would you like to divide: ";
getline (cin, calc);
stringstream(calc)>>numtwo;
cout<<"\n";
cout<<"\n";
cout<<"= " << numone / numtwo << endl;
}
if (num == 4){
cout<<"\n";
cout<<"\n";
cout<<"What would you like to Multiply: ";
getline(cin, calc);
stringstream(calc)>>numtwo;
cout<<"\n";
cout<<"\n";
cout<<"= " << numone * numtwo << endl;
}
if (( num == 0) || (num > 4)) {
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"Error, please enter a number between 1-4....\n";
cout<<" 1 = add\n";
cout<<" 2 = subtract\n";
cout<<" 3 = divide\n";
cout<<" 4 = multiply\n";
cout<<"\n";
cout<<"\n";
}
x = x + 1;
}
cin.get();
return 0;
}
PS::::i use the cout's a lot so when i test the program i can see exactly what happens clearly. Ill take those out later. and i use DEV::C++ and learning Visual Studio 2010
is a waste. you could cin >> num; and then cin.ignore(numeric_limits<streamsize>::max(), '\n') if you're worried about any garbage entered.
1 2 3
cout<<"\n";
cout<<"\n";
cout<<"Please Enter A Value: ";
is also unnecessary. you could cout<<"\n\nPlease Enter A Value: "; or
1 2 3
cout<<"\n""\n""Please Enter A Value: ";
if you want to see your strings how they are printed.
input of numtwo is repeated four times. this could be avoided with some slight changes in structure.
another thing, is that your calculator is a bit uncomfortable to use. instead of numbers 1-4 you could ask user to enter strings "add", "subtract", etc. and "quit" so that the user can perform a number of calculations other than 10. you're already using strings, so I don't see the problem.
Here is a heavily commented example I wrote that should help you out.
#include <iostream>//cmath is not needed until you start using exponents and tangents and stuff
using namespace std;
//one funtion of many that can be writen for
//other types of arithmatic operations
//try making simple functions, this will
//clean up a lot of code in main()
float mult(float a, float b)//you can also put these in a separate .h file, and declare and define them there to clean up main()
{
//this of course only works with two numbers unless you start overloading operators and stuff
//or make endless amounts of overloaded versions of the same same functions lol
//there's always an easier way so just use this to help figure out what you want to do
//and then do some more research
return a * b;
}
float add(float a, float b)
{
return a + b;
}
float sub(float a, float b)
{
return a - b;
}
float div(float a, float b)
{
if(b == 0)
{
cout << "Divide by 0\n";
return 0.0;
}
else
return a / b;
}
int main()
{
float one;
float two;
char op;
//you can use a while or do:while loop to keep the program up and going
int i = 1;
while(i > 0)//just to keep you in the loop, you can just press the exit button on the window
{
cout << "Enter an arithmatic statement: ";//Enter 55.1*9 or something to test it out
cin >> one/*float*/ >> op/*char*/ >> two/*float*/;//matches the oder of the input types expected by the input stream
//to show you that cin can work like this I output the individual variables
cout << one << "\n";
cout << op << "\n";
cout << two << "\n\n"; //stack your "\n"'s for multiple new lines like that to shorten code
//use switch to analyze your arithmatic operator, or a lot of "if" statements
//you will need to add all of them
switch(op)//you could write a function for this too to get the code out fo main
{
case '*': cout << mult(one, two) << "\n";//you could also just "cout << one * two << endl;" but functions
break;
case '+': cout << add(one, two) << "\n";
break;
case '-': cout << sub(one, two) << "\n";
break;
case '/': cout << div(one, two) << "\n";
break;
default:
cout << "no definition for operator " << op << "\n\n";
}
}
i had actually though of using add, subtract, after i posted, and as for the couts i had planned on removing them , they were just for show to keep separate
but i do like your way better, " Now i Know".
im not fully understanding how numtwo can be used less and return the same results.
but first things first. I will work on adding the add, subtract, blah blah.
ex something something:
thnx for the example as for thats how i learn best. However it will take me a day to straighten out your code. Putting it in code brackets would have been nice, but thnaks none the less. Ive never seen