Converter program
Feb 23, 2009 at 1:04pm UTC
Hi there,
The following program is a kW to horsepower converter. The problem I am having is that after choosing option 1, inputting 8 values and obtaining the table, the program automatically jumps to option 2 before ending. How do I fix this - what is wrong with the
if
statement?
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
using namespace std;
int main()
{
double number [8];
int userChoice;
cout << "Program to convert kW/hp to hp/kW. Please make your choice:" << endl << endl;
cout << "1 - kW to hp converter" << endl;
cout << "2 - hp to kW converter" << endl;
cin >> userChoice;
if (userChoice == 1)
{
cout << endl << "Please input 8 kW values for conversion to hp" << endl;
for ( int x = 0; x<8; x++ )
{
cin >> number[x];
}
cout << endl << "The corresponding table values are:" << endl << endl;
cout << "kW\thp" << endl;
for ( int x = 0; x < 8; x++ )
{
cout << number[x] << "\t" << number[x]*1.314 << endl;
}
}
else (userChoice == 2);
{
cout << endl << "Please input 8 hp values for conversion to kW" << endl;
for ( int x = 0; x<8; x++ )
{
cin >> number[x];
}
cout << endl << "The corresponding table values are:" << endl << endl;
cout << "hp\tkW" << endl;
for ( int x = 0; x < 8; x++ )
{
cout << number[x] << "\t" << number[x]/1.314 << endl;
}
}
return 0;
}
Thanks! :)
Feb 23, 2009 at 1:11pm UTC
remove semicolon at end of line 32
and it should either be "else" with no condition
or "else if" with a condition.
Last edited on Feb 23, 2009 at 1:11pm UTC
Feb 23, 2009 at 1:13pm UTC
line 32:
else (userChoice == 2);
remove
(userChoice == 2);
or ad an
if :
else if (userChoice == 2)
(without semicolon)
The way you have was like this:
1 2 3 4 5 6 7 8 9 10
if (userChoice == 1)
{
//some code
}
else
userChioce == 2;
{
//some other code (not in the else block)
}
EDIT: I was slow in typing...
Last edited on Feb 23, 2009 at 1:14pm UTC
Feb 23, 2009 at 3:11pm UTC
Thanks! :)
Last edited on Feb 23, 2009 at 3:11pm UTC
Topic archived. No new replies allowed.