hi guys.i want to show you my best calculator i made so far. any tips for improvement.and also can u tell me how to let the user quit at the beginning by pressing q
ill post the code next cause i don't have enough room. i would like feed back whether it be bad or good. also can any of you who knows graphics and made a calculator with it can you pm me not the source code but the actual program...ill have to do the code in 2 replys cuz it is to big
#include <iostream>
usingnamespace std;
class setup{
public:
void title(){
system("TITLE calculator");
system("color 2");
cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
}
};
int main()
{
setup setup1;
setup1.title();
int howmany;
int choice;
double a;
double b;
double c;
double d;
double e;
double average;
char cchar;
char cagain;
cout << "type 1 to use a regular calculator.";
cout << "type 2 to use a average calculator";
cout << endl;
cin >> choice;
switch (choice){
case 1:
do{
cout << "please enter the amount of numbers you want to use: ";
cin >> howmany;
switch (howmany){
case 1:
cout << "invalid number" << endl;
break;
case 2:
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << " = "
<< (a + b) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << " = "
<< (a - b) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " = "
<< (a * b) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " = "
<< (a / b) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
break;
case 3:
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << " = "
<< (a + b + c) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << " - " << c << " = "
<< (a - b - c) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " = "
<< (a * b * c) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " = "
<< (a / b / c) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
case 4:
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
cout << "please enter the forth number you would like to use" << endl;
cin >> d;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << " + " << d << " = "
<< (a + b + c + d) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << " - " << c << " - " << d << " = "
<< (a - b - c - d) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " = "
<< (a * b * c * d) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " = "
<< (a / b / c / d) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
case 5:
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
cout << "please enter the forth number you would like to use" << endl;
cin >> d;
cout << "please enter the fifth number you would like to use" << endl;
cin >> e;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << " + " << c << " + " << d << " + " << e << " = "
<< (a + b + c + d + e) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << " - " << c << " - " << d << " - " << e << " = "
<< (a - b - c - d - e) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " * " << e << " = "
<< (a * b * c * d * e) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " / " << e << " = "
<< (a / b / c / d / e) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
default:
cout << "invlaid number.you can only use up to 5" << endl;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
break;
case 2:
do{
cout << "enter how many numbers you want to find the average of (up to five)" << endl;
cin >> howmany;
switch (howmany){
case 1:
cout << "you cant use that number it is too low" << endl;
break;
case 2:
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the second number you want to use" << endl;
cin >> b;
average = (a + b) /2;
cout << "the average of those numbers is: " << average << endl;
break;
case 3:
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the second number you want to use" << endl;
cin >> b;
cout << "please enter the third number you want to use" << endl;
cin >> c;
average = (a + b + c) /3;
cout << "the average of those numbers is:" << average << endl;
break;
case 4:
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the second number you want to use" << endl;
cin >> b;
cout << "please enter the third number you want to use" << endl;
cin >> c;
cout << "please enter the forth number you want to use" << endl;
cin >> d;
average = (a + b + c + d) /4;
cout << "the average of those four numbers is: " << average << endl;
break;
case 5:
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the second number you want to use" << endl;
cin >> b;
cout << "please enter the third number you want to use" << endl;
cin >> c;
cout << "please enter the forth number you wamt to use" << endl;
cin >> d;
cout << "please enter the fifth number you want to use" << endl;
cin >> e;
average = (a + b + c + d + e) /5;
cout << "the average of those five numbers is: " << average << endl;
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
}
system("pause");
return 0;
}
You may want to try using vectors. Also I tend to stay away from the dreaded system () function.
Also as for your first question I am guessing you should try using the getline function and search the string for the +-*/% characters.
@moot1, It seems you are relatively new to C++. Your program is very inefficient, and relies on the fact that the user knows how to use your program specifically. While coding, you're supposed to assume that the user is really dumb, and that a 6yr old can use your program easily.
Also, who wants to simply add, multiply, subtract or divide all numbers!, also, for multiple input at once, haven't you heard of cascading?
ascii has a nice idea, but that involves strings. If you know how to use them, you can accept an expression from the user, and calculate the result, much like an actual calculator. And by functions, he means program functions. For repetitive statements, so that the overall length of your program is less. For a simple program like this, yours is damn long!!!
In your next post, just express your extent of knowledge in the C++ language, and I'll be able to help you out! :)
[This message may seem a little sharp, but it isn't meant to be! :)]
i couldnt really think of any way to shorten it or make it more user freinlly. can you please tell me a way to shorten it by like showing me a snippet of code because i didnt get fully what you meant above about functions.thanks
Firstly, in your switch cases, you use something like this:
1 2 3 4 5 6 7
case 3:
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the second number you want to use" << endl;
cin >> b;
cout << "please enter the third number you want to use" << endl;
cin >> c;
Instead, what you could do is:
1 2 3 4 5
case 3:
{
cout<<"\nPlease enter the three numbers: ";
cin>>a>>b>>c;
}
This will shorten your code by almost 40-50 lines easily.
Nextly, you could write functions for the operations. This would be outside main, preferably above... Say:
1 2 3 4
float add(float a, float b)
{
return (a+b);
}
So in any switch case, take the example of 5 numbers, you could do this:
cout<<"\nThe answer is: "<<((add(a, b))+(add(c, d))+e);
Or for that matter:
cout<<"\nThe answer is: "<<add(add(add((add(a, b), c), d), e);
This will again, make the program simpler, because now, you have four functions (one for each operation) doing all the work for you, and you no longer need to evaluate the result for each case. Just make a call to the function, and it'll do it for you. This makes the program readable, as well as memory efficient.
cin >> dnum1 >> dnum2;
average = (dnum1 + dnum2) /2;
cout << "the average of those numbers is " << average << endl;
break;
case 3:
cout << "Please enter three numbers you want to use: ";
cin >> dnum1 >> dnum2 >> dnum3;
average = (dnum1 + dnum2 + dnum3) /3;
cout << "the average of those numbers is " << average << endl;
break;
case 4:
cout << "Please enter the four numbers you want to use: ";
cin >> dnum1 >> dnum2 >> dnum3 >> dnum4;
average = (dnum1 + dnum2 + dnum3 + dnum4) /4;
cout << "the average of those four numbers is " << average << endl;
break;
case 5:
cout << "Please enter the five numbers you want to use: ";
cin >> dnum1 >> dnum2 >> dnum3 >> dnum4 >> dnum5;
average = (dnum1 + dnum2 + dnum3 + dnum4 + dnum5) /5;
cout << "the average of those five numbers is " << average << endl;
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
}
system("pause");
return 0;
Well, kinda. You haven't added any new functions, and for tasks like adding you could even just define a macro to do it like:
#define add(a, b) a + b
So put some functions in there! And you still haven't added some kind of capability so that the user can enter as many numbers as they want, which can easily be done with a loop.
Also, I linked www.pastebin.com in my first message. Put your code there so you don't have to spew it out over a few posts.
See, just a simple cascading in your input has made your code 60 lines smaller. Imagine what effect functions will have!!!
@ ascii, yeah, macros are cool too, but they aren't very memory efficient, in spite of being speedy coz they are expanded everywhere! Whereas functions may be a fraction of a second slower, but very very memory efficient.. I have nothing against macros, but I guess to each his own, I'd rather use functions... :)
You have a class and a function already in your code. Just write some more.
@caprico, yeah it's a fair argument. I just like macros since they're shorter, and most people have enough memory nowadays that I don't find to be a problem.
DevC++ is outdated as far as the debugger and linker are concerned. Also, quite a few errors are over-looked, if I've read up correctly. I use simple TurboC++...