-
January 11th, 2011, 12:10 AM
#1
Running total all messed up
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
system ("TITLE Calculator");
system ("COLOR 0A");
while (1)
{
int menu_choice, nums, total;
char answer, esc;
cout << " ::Menu::\n"
<< "Type the number of the choice you want and press \"Enter\".\n\n"
<< "1. Addition\n"
<< "2. Subtraction\n"
<< "3. Multiplication\n"
<< "4. Division\n"
<< "5. Temperature Converter\n"
<< "6. Exit\n\n"
<< "Menu Choice: ";
cin >> menu_choice;
if (menu_choice < 1 || menu_choice > 6 )
{
system ("CLS");
cout << " Error\n" << menu_choice << " is not an available choice.\n\n";
system ("PAUSE");
system ("CLS");
}
else if (menu_choice == 1)
{
while (1)
{
total = 0;
system ("CLS");
cout << "Enter the numbers (separated by a space) you want added together,\n"
<< "when done type press \"Enter\".\n\n"
<< "Numbers: ";
cin >> nums;
total += nums; // this glitches it up
if (total > 2147483647 || total < -2147483647) // Not reading the negative
{
system ("CLS");
cout << " Error\n"
<< "The total of the number(s) you entered are too\n"
<< "large for this program to handle.\n\n";
system ("PAUSE");
system ("CLS");
continue;
}
cout << "The total is: " << total << ".\n";
cout << "\n\nWould you like to add more numbers? (Y/N)\n"
<< "Answer: ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
{
system ("CLS");
continue;
}
else if (answer == 'n' || answer == 'N')
{
system ("CLS");
break;
}
}
}
____________________________________________________________________________
Ok, i got bored today, so i decided to make a calculator, this is the first section of a soon to be ongoing project outta boredom. BUT, so far i have two problems it, 1st it wont read the neg. when i try to limit the total, and 2nd the running total doesnt work right, it just glitches up and doesnt work, any help??
Please note: i know im using system commands, i know they make the program run slower, but its all i know how to use at the moment and i dont think itll matter for a calculator, so please dont bring it up unless that is the cause of the problems, thx.
Last edited by Marc G; January 11th, 2011 at 03:44 AM.
Reason: Added code tags
-
January 11th, 2011, 01:22 AM
#2
Re: Running total all messed up
You might want to elaborate on what exactly is the problem, e.g., explain how you know "this glitches it up".
Oh, and please post code in [code][/code] bbcode tags.
-
January 11th, 2011, 07:54 AM
#3
Re: Running total all messed up
Originally Posted by iiSoMeGuY 7x
so far i have two problems it, 1st it wont read the neg. when i try to limit the total, and 2nd the running total doesnt work right, it just glitches up and doesnt work, any help??
Did you step through your code with the debugger?
One error in your code is that you don't check for valid input. You check the range of inputted values, but if a user enters some text, your program has undefined behavior.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
January 11th, 2011, 08:16 AM
#4
Re: Running total all messed up
You're telling the user to enter a series of numbers separated by a space, and trying to put all that into a single int.
-
January 11th, 2011, 01:40 PM
#5
Re: Running total all messed up
Originally Posted by iiSoMeGuY 7x
Ok, i got bored today, so i decided to make a calculator, this is the first section of a soon to be ongoing project outta boredom.
Just to warn you, if you plan to make this more elaborate, the way you've started the program is not the way to program such a calculator.
You'll get tripped up if you ever decide to use precedence rules, i.e
Code:
5 + 2 * 4 = 13 (not 28).
Or parentheses, or...
There are formal ways of writing such a program, but that's for later.
Regards,
Paul McKenzie
-
January 11th, 2011, 08:31 PM
#6
Re: Running total all messed up
never mind, i just changed it so im not using any += or -= (i was trying something new), and added some more loops, but thanks for trying to help anyways
-
January 11th, 2011, 09:36 PM
#7
Re: Running total all messed up
Originally Posted by iiSoMeGuY 7x
i knew about that, i just started on it, im gonna add more stuff later
Since you are bored (as your first post suggested), I think you would learn a lot more if you allow the user to input any mathematical expression and then you attempt to evaluate it.
Instead of limiting the user to only add, or only subtract, multiply, etc. have them do anything they want, as long as the expression is legal. Then return the correct answer back to them.
Something like this:
Code:
Enter expression: 2 * 8 + 5
Your answer is: 21
Enter expression: 3 + 4 * 8
Your answer is 35
Enter expression: 1 + 1 + 4 * 2 - 3
Your answer is 7
That is much closer to a calculator than what you're coding now, and should keep you busy for a while.
Regards,
Paul McKenzie
-
January 12th, 2011, 12:00 AM
#8
Re: Running total all messed up
Originally Posted by D_Drmmr
Did you step through your code with the debugger?
One error in your code is that you don't check for valid input. You check the range of inputted values, but if a user enters some text, your program has undefined behavior.
i knew about that, i just started on it, im gonna add more stuff later
-
January 12th, 2011, 12:07 AM
#9
Re: Running total all messed up
Originally Posted by laserlight
You might want to elaborate on what exactly is the problem, e.g., explain how you know "this glitches it up".
it wont show you did anything, skip to "Would you like to add more numbers...", or just says that the total is 5 no matter what.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|