Hello I am about 3 days into my effort to learn C++. I am learning on my own relying on web based resources like cplusplus.com and my local library.
I recently "finished" a simple calculator program. I am satisfied with the way it works but I feel there are a couple of lines I could clean up or optimize.
I have 2 questions.
1. Line 36 is working the way I want it to but it took several iterations of the same idea to get there and I am not sure it is done in the best way.
2.In order to get my program to run after getting a solution I used a while statement in the
main(). I also considered calling
main()from
printsolution(). Neither of these feel satisfactory to me, I believe my
main() will never complete which doesn't seem correct.
My next task will be learning how to enable to user to exit the program at any time, so please don't give that part away.
code below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// simple calculator.cpp : Defines the entry point for the console application.
// simple calculator takes 3 inputs from the user 1.First number 2.Operation symbol 3.Second number
// it then preforms the desired math and returns a solution.
#include <iostream>
#include <string>
#include <sstream>
int domath(float& a,char& b, float& c, float d);
int printsolution(float d);
int getuserinput ( float a, char b, float c)
{
using namespace std;
string input="";
//Gets user input 1 and verifys it is a float number
while(true)
{
cout<<"Enter the first number you want to work with."<<endl;
getline( cin, input);
stringstream myStream(input);
if (myStream >> a)
break;
cout<<"That is not a valid number try again."<<endl;
}
//Gets user input 2 and verfiys it is a valid operator
cin.sync();
while(true)
{
cout<<"Now enter a +, -, * or / sign"<<endl;
getline( cin, input);
stringstream myStream(input);
if (myStream >> b, b== '+' || b == '-' || b == '*' || b == '/' )
break;
cout<<"That is not a valid operator try again."<<endl;
}
//Gets user input 3 and verifys it is a float number
cin.sync();
while(true)
{
cout<<"Thank you now enter the second number you want to work with."<<endl;
getline( cin, input);
stringstream myStream(input);
if (myStream >> c)
break;
cout<<"That is not a valid number try again."<<endl;
}
cout<<"You entered "<<a<<b<<c<<endl;
cout<<"Starting calculation."<<endl;
domath( a, b, c, 0.0);
return (0);
}
int domath (float& a, char& b, float& c, float d)
{
if (b == '+')
d = a + c;
if (b == '-')
d = a - c;
if (b == '*')
d = a * c;
if (b == '/')
d = a /c;
printsolution(d);
return 0;
}
int printsolution (float d)
{
using namespace std;
cout<<"Your Soulution is "<<d<<"\n"<<endl;
return 0;
}
int main()
{
using namespace std;
while(true)
{
cout<<"Welcome to Eric's Simple Calculator."<<endl;
cout<<"------------------------------------"<<endl;
getuserinput(0.0,0,0.0);
}
return 0;
}
|
I have 2 questions.
1. Line 36 is working the way I want it to but it took several iterations of the same idea to get there and I am not sure it is done in the best way.
2.In order to get my program to run after getting a solution I used a while statement in the
main(). I also considered calling
main()from
printsolution(). Neither of these feel satisfactory to me, I believe my
main() will never complete which doesn't seem correct.
My next task will be learning how to enable to user to exit the program at any time, so please don't give that part away.