October 26th, 2002, 03:28 PM
|
#1 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
I am making a calculator and am having some problems.
It is supposed to take a selection of either addition subtraction multiplication division or power and print the answer. The problem is in my while loop, just started these so I need some help. here is part of the program:
#include <iostream.h>
#include <math.h>
int main(void)
{
char choice;
double num , sum;
cout << "\t\t Simple Math Calculator \n\n" << endl;
cout << "select 'A' for addition, 'S' for subtraction, 'D' for division, \n";
cout << "'M' for multiplication, and 'P' for power";
cin >> choice;
if((choice == 'a') || (choice == 'A'))
{
cout << "Input numbers to be added and '.' to finish" << endl;
while(choice != '.')
{
cin >> num;
num ++;
sum = num + num;
cout << "Answer = " << sum << endl;
}
cin >> choice;
}
return 0;
}
The problem is when I input for example: 3 2 1
I get:
Answer = 8
Answer = 6
Answer = 4
and when I add the period it is infinite, what did I do wrong?
If you actually took the time to read this thanks.
Last edited by techteen143 : October 26th, 2002 at 03:55 PM.
|
| |
October 26th, 2002, 03:49 PM
|
#2 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
This part:
while(choice != '.')
{
cin >> num;
num ++;
sum = num + num;
cout << "Answer = " << sum << endl;
}
cin >> choice;
}
Thanx
Last edited by techteen143 : October 26th, 2002 at 03:56 PM.
|
| |
October 26th, 2002, 03:56 PM
|
#3 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
cin >> choice;
need to be INSDE the while loop
Otherwise the code never even bothers to touch it.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
October 26th, 2002, 04:01 PM
|
#4 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
Thanx BUT
If I do that when I input 3 2 1
It prints:
Answer = 8
answer = 4
*edit*
& I still get an infinite loop when running it multiple times without restarting
Last edited by techteen143 : October 26th, 2002 at 04:04 PM.
|
| |
October 26th, 2002, 05:42 PM
|
#5 (permalink)
| | Junior Member
Join Date: Jun 2002
Posts: 20
|
#include <iostream.h>
#include <math.h>
int main(void)
{
char choice;
int lastsum=0;
double num , sum;
cout << "\t\t Simple Math Calculator \n\n" << endl;
cout << "select 'A' for addition, 'S' for subtraction, 'D' for division, \n";
cout << "'M' for multiplication, and 'P' for power";
cin >> choice;
if((choice == 'a') || (choice == 'A'))
{
cout << "Input numbers to be added and '.' to finish" << endl;
while(choice != '.')
{
cin >> num;
sum = lastsum + num;
cout << "Answer = " << sum << endl;
lastsum = sum;
}
cin >> choice;
}
return 0;
}
Ok try this. You should be fine.
Erorr
"sum = num + num "
Last edited by vb6 : October 26th, 2002 at 05:48 PM.
|
| |
October 26th, 2002, 06:25 PM
|
#6 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
OK I am still getting multiple answers but the last answer is correct. When I input '.' the loop is infinite. How can I fix this?
Last edited by techteen143 : October 26th, 2002 at 06:42 PM.
|
| |
October 26th, 2002, 06:39 PM
|
#7 (permalink)
| | Junior Member
Join Date: Jun 2002
Posts: 20
|
isn't that what you wanted ? |
| |
October 26th, 2002, 06:43 PM
|
#8 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
| Quote: Originally posted by techteen143
The problem is when I input for example: 3 2 1
I get:
Answer = 8
Answer = 6
Answer = 4
and when I add the period it is infinite, what did I do wrong? | I want one answer that is correct and not knowing how many #'s will be entered I am using a while loop. And when I input a period it is supposed to stop the program instead it is in an infinite loop.
Any ideas?
Thanx
Last edited by techteen143 : October 26th, 2002 at 06:53 PM.
|
| |
October 26th, 2002, 07:11 PM
|
#9 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
here's the problem, when you cin the number, when you type '.', it returns it as 46.0000 because you declared a double, not a char, and whenever you enter a character, it automatically returns the ascii value ('.'=46). so you need to use some other way of ending the loop. |
| |
October 26th, 2002, 07:12 PM
|
#10 (permalink)
| | Junior Member
Join Date: Jun 2002
Posts: 20
|
#include <iostream.h>
#include <math.h>
int main(void)
{
char choice;
double num , sum=0;
cout << "\t\t Simple Math Calculator \n\n" << endl;
cout << "select 'A' for addition, 'S' for subtraction, 'D' for division, \n";
cout << "'M' for multiplication, and 'P' for power";
cin >> choice;
if((choice == 'a') || (choice == 'A'))
{
cout << "Input numbers to be added and -1 to finish" << endl;
while(num != -1)
{
cin >> num;
if(num != -1 )
{
sum = sum + num;
}
}
}
cout << "Answer = " << sum << endl;
return 0;
}
Somthing like this ? |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |