| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am taking a class in C++ Programing and our teacher is not explaining anything so the class will understand.
Our homework is to write a program for a simple calculator. Shouldn't be that big of a deal however he has not explained what the codes mean or how to go about using them. We should understand from the book but to me it is greek. Can anyone send me to a link or explain to me what the meanings are? I want to figure this out on my own but without knowing what the codes mean there is no way I can do it. |
|
#2
|
|||
|
|||
|
There's a post in the same page as yours with a calculator code written. I'm not suggesting stealing the code. Just, read it to understand how it works (as for the functions, just grab the book..functions are functions and shouldn't have any problem with them).
|
|
#3
|
|||
|
|||
|
Refer to Yashwant Kanitkar. The book explains all concepts with vividity.
|
|
#4
|
|||
|
|||
|
Ok
ok i got it to run however when i do the invert it will not do it and then when i start the * 100 like it is supposed to it just keeps on going an going... this is what i got so far..
#include<iostream> #include <math.h> using namespace std; int main() { char Operator = ' '; float Num1 = 0, Num2 = 0; cout << "\t\t\t Calculator Functions : \n\n"; cout << " \t\t + (add) , * (mult) , ^ (expo) , \n "; cout << " \t\t (I)nvert , (S)et value , (Q)uit, \n "; cout << " \t\t show this (M)enu again. \n "; do { cin >> Operator; Operator = toupper (Operator); switch (Operator) { case 'S': cin >> Num1; break; case '+': cin >> Num2; Num1 = Num1 + Num2; break; case '^': cin >> Num2; Num1 = pow (Num1, Num2); break; case '-': cin >> Num2; Num1 = Num1 - Num2; break; case '*': cin >> Num2; Num1 = Num1 * Num2; break; case 'I': cin >> Num2; Num1 = Num1 / Num2; break; case 'Q': cout << " You have chosen to exit.\n"; break; case 'M': system ("cls"); cout << "\t\t\t Calculator Functions : \n\n"; cout << " \t\t + (add) , * (mult) , ^ (expo) , \n "; cout << " \t\t (I)nvert , (S)et value , (Q)uit, \n "; cout << " \t\t show this (M)enu again. \n "; break; } if (Operator != 'Q') cout << "\n\t\t\t\t" << Num1 << endl ; }while (Operator != 'Q'); return 0; } |
|
#5
|
|||
|
|||
|
What do you mean by "Invert" Operation? Do you want to inverse a number?
The "*100" example is not clear.Please elaborate. |
|
#6
|
|||
|
|||
|
From what I can infer from your example of "*100" , is that you must be getting "0".
Am I right? |
|
#7
|
|||
|
|||
|
I get an amount 3 times and then 0 until I stop the loop.
|
|
#8
|
|||
|
|||
|
i
Quote:
You mean if you 3*100 , you get 300 and 0 ? |
|
#9
|
|||
|
|||
|
Where are you supplying value to variable Num1?
It is initialized to zero => result will be zero for "*" operator, irrespective of Num2's value. What keyboard button sequence are you using? Meaning: After 1st loop, when your prog. prompts for second key press for operation,which button do you press? I may not be able to help you unless you are specific. (From my analysis of your prog, I disagree with you for printing an amount 3 times the orifginal value supplied) |
|
#10
|
|||
|
|||
|
Hey!
In the name of God...what the hell are you talking about? I've read all the posts and not one did I understand. What is that thing about 3 * 100 returns 0 ? Honestly, I don't follow the idea. Please, define each operation and what it does so you can make a function for each one... The concept is simple: Code:
1) Enter a number. 2) Enter an operand 2.1) If the operand if final (= or enter for example), go to(3). 2.2) If not, prompt for a second number. 3) Show result. The program should be a simple loop with an algorithm like the one above. When an operand is entered, you make a select case (for example). If the operand requires another number, prompt for it. If not, call the function with that only number and show the result. Since you're working with C++....you, and that's only my opinion, could create a class called Calculator. you would give it all the methods (operations) and a couple of variables (parameters for those operations) and simply invoque for example: - Calculator.Add(N1, N2) - Calculator.Substract(N1, N2) - Calculator.Multiply(N1, Ntimes) - Calculator.ToPower(N1, Nexp) - Calculator.Exponential(N) //there's an example of a single number operand e**x - Calculator.ShowResult That way, you can test the object aside, and once you see everything working, you can be sure the problem (should you find any) is not there!!. Plus, that would make the code realy nice and short (the class definition should be inside the .h and .cpp files). Good Luck... Anibal. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Right Direction Please |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|