![]() |
|
#1
|
|||
|
|||
C++ Calculator Problem. Any help much appreciated.Right then here is the code for it.
CPP / C++ / C Code:
As you can probably tell from things I've done wrong and things I should have done first, such as making a lib file for all the values as the top, I am an utter beginner. The problem with this calculator is that it almost works, however, it doesn't work after the first few numbers are inputted, to run it you would get something like this. Please enter a number 4 1. Add 2. Divide 3. Multiply 4. Subract 5. Quit Choose an operation 1 Please enter a number 4 The answer is 8 1. Add 2. Divide 3. Multiply 4. Subract 5. Quit Choose an operation 3 Please enter a number 3 The answer is 32 And of cour 8x3 is not 32, and I realise that it's storing only the first number ir get's into the value input2 and so I thought that I must need to flush or erase it's memory for it somehow, but I have no command that does that does it. Can anyone help? I know the program layout is a little confusing, planning to add some pauses for each part. Help please? Last edited by LuciWiz : 29-Jun-2005 at 05:22.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
|||
|
|||
|
What I mean is you can flush buffers and strings, but is it possible to erare a float value?
|
|
#3
|
|||
|
|||
|
Your code declares variables globally:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#4
|
||||
|
||||
|
Quote:
Then, print your inputs as soon as they are entered to prove they are correct. Next, move the display of the answer after the switch. Pass the answer and input into each function. This way the function has a single purpose -- execute the operation. And why is there a do/while loop in divide? __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#5
|
|||
|
|||
|
Quote:
I can get rid of float ans from inside the brackets of the divide, subtract, add and multiply fuctions because as you say I declared them globally. I cannot get rid of float ans from inside the brackets at the start though, or else the global lines at the beginning aren't defined. WaltP: The DO IF statement in the divide section is because of the anything divided by zero crashing the program, creating an infinite loop or something. |
|
#6
|
||||
|
||||
|
Quote:
Quote:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#7
|
|||
|
|||
|
CPP / C++ / C Code:
You should reorganize your code into something like this I think. Theres no need for so many functions when they all do simple things. The references/pointers needs fixing, though. You'll probably want to loop it too. |
|
#8
|
|||
|
|||
|
Quote:
Simpler organization is a Good Thing. (But I think the Original Poster had questions that mainly were due to problems with his loop, which you didn't cover.) Here are a few Bad Things about your code. 1. Won't compile. (Did you try it?) 2. The following is wrong: CPP / C++ / C Code:
3.The following is wrong, wrong (even if choice were defined here): CPP / C++ / C Code:
4. The following is wrong, since some return paths don't return a value. Bad style even if you had checked for valid operation values in PrintMenu(). CPP / C++ / C Code:
5. Many people read these threads to pick up programming techniques even though they never post any requests. Since your post was about style, I would offer the comment that I think it is good style to have main() return a value. Now, I know that this is a special case that is covered in the C standard (main() returns zero if there is no explicit return or exit()), so it's not strictly illegal, but I think if we are attempting to help those with less experience, that we should be very careful about every detail, and make every effort to set a good example. (And at the very least make sure the program compiles and gives some kind of expected output.) Regards, Dave |
|
#9
|
|||
|
|||
|
Quote:
Ooops. This is where I slap myself. My first posting of code. I just hope its my worst. Okay I'll admit it. I'm a slacker. I went through about three edits. I rearranged the code to make it look prettier and that was all I thought about at the time. I made one compilable version, but the results were undesirable. I edited again but I assumed it would compile because I only made few changes. Rearranging does not solve the problem, apparently. I know. I know. Flawed logic. Well to quote our Original Poster "As you can probably tell from things I've done wrong and things I should have done first,..,I am an utter beginner." --SpinDizzy I'll fix it later. Or give up. |
|
#10
|
|||
|
|||
|
I fixed it, atlast. It works much better now(as it actually compiles). This one only handles integers however(scanf is bugging me). You get the general idea though.
CPP / C++ / C Code:
Three cheers for recursion!!! |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| probably a stupid mistake....lil help appreciated | wbsquared03 | C++ Forum | 9 | 06-Dec-2004 15:43 |
| Need help with a program if anyone can help it would be appreciated | Krc784 | C++ Forum | 1 | 03-Nov-2004 20:55 |
| Minor Problem with my program. Any help greatly appreciated. | agentxx04 | C Programming Language | 6 | 24-Oct-2004 15:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The