home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 3107
Discussions: 188,375, Posts: 2,243,439, Members: 232,603
Old October 26th, 2002, 03:28 PM   Digg it!   #1 (permalink)
Member
 
techteen143's Avatar
 
Join Date: Oct 2002
Location: Rochester, NY
Posts: 478
Stupid C++ question

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.
techteen143 is offline   Reply With Quote
Old October 26th, 2002, 03:49 PM     #2 (permalink)
Member
 
techteen143's Avatar
 
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.
techteen143 is offline   Reply With Quote
Old October 26th, 2002, 03:56 PM     #3 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.

jkrohn is offline   Reply With Quote
Old October 26th, 2002, 04:01 PM     #4 (permalink)
Member
 
techteen143's Avatar
 
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.
techteen143 is offline   Reply With Quote
Old October 26th, 2002, 05:42 PM     #5 (permalink)
vb6
Junior Member
 
Join Date: Jun 2002
Posts: 20
Send a message via ICQ to vb6
Hi

#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.
vb6 is offline   Reply With Quote
Old October 26th, 2002, 06:25 PM     #6 (permalink)
Member
 
techteen143's Avatar
 
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.
techteen143 is offline   Reply With Quote
Old October 26th, 2002, 06:39 PM     #7 (permalink)
vb6
Junior Member
 
Join Date: Jun 2002
Posts: 20
Send a message via ICQ to vb6
isn't that what you wanted ?
vb6 is offline   Reply With Quote
Old October 26th, 2002, 06:43 PM     #8 (permalink)
Member
 
techteen143's Avatar
 
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.
techteen143 is offline   Reply With Quote
Old October 26th, 2002, 07:11 PM     #9 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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.
originel is offline   Reply With Quote
Old October 26th, 2002, 07:12 PM     #10 (permalink)
vb6
Junior Member
 
Join Date: Jun 2002
Posts: 20
Send a message via ICQ to vb6
#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 ?
vb6 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2881)
The United States Debt (20)
Looks like Burris will get his Sena.. (7)
Upgrading RAM (5)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (436)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
Help with an Ati Radeon HD 4850.. (23)
Laptop waking up itself (0)
CPU wont boot (3)
Best digital camera for under 2.. (13)
Building first computer, will t.. (2)
Folderchat Weekday thread (436)
get this error, "res://C:\.. (73)
Problem with boot and motherboa.. (0)
MS to offer free Windows 7 upgr.. (1)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 07:41 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28