PDA

View Full Version : trying out c++


mrloco
02-27-2005, 02:52 PM
I have run into some problems while trying to write a Pounds to kilos conversion program in c++. no matter what number i press, the program always goes to the kilos to pounds part...
here's the code.

#include <iostream.h>
void PoundsToKilos() {
cout << "Enter the value in Pounds\n";
float x;
cin >> x;
cout << "The value in kilos is=\n "<< x/2.2;
}
void KilosToPounds() {
cout <<"enter the value in Kilos\n";
float x;
cin>>x;
cout <<"the value in Pounds is= "<<x*2.2;
}
int main() {
int choice;
cout <<"Weight conversion Program\n";
cout <<"=========================\n";
cout <<"please choose\n";
cout <<"1.) Kilos to pounds\n";
cout <<"2.) Pounds to Kilos\n";

cin>>choice;
if (choice=1)
KilosToPounds ();
else if (choice=2)
PoundsToKilos ();
cin.get();
}

Kgen
02-27-2005, 02:57 PM
not too sure about this, since i work mainly in java/c, but perhaps you need to atoi() the input before the conditional statement?

mrloco
02-27-2005, 03:00 PM
sorry, that dint work, but thx anyways.

rakmaya
02-27-2005, 03:19 PM
Hah Hah:)

change the if (choice=1)

to

if (choice == 1)



In C++ comparision is "==" and "=" is assignment. In your code you are doing Assignment first and the comparing choice.

The code if(choice=1) is the same as saying

choice = 1;
if(choice)
..........

since now choice is greater than Zero and hence "not false" it will go into the first "if" condition.

horst hrubesch
02-27-2005, 03:19 PM
Hi Mr. Loco, try

if (choice == 1)


Hope it helps,
F.


edit: rakmaya was faster I guess :)

mrloco
02-27-2005, 03:37 PM
I forgot about that...how stupid of me...ty

Loudboy
02-27-2005, 07:35 PM
man, mr. loco. the obvious mistakes can really screw u over can't they? i dont know how many times ive not realized something because i forgot a semi colon or forgot to close a bracket somewhere.

mrloco
03-01-2005, 04:03 PM
true...the worst thing about that is that you are looking for something completely different and you spend hours looking for it.

CGTalk Moderation
03-01-2006, 05:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.