If/Else if statement help

Hey guys, I'm just now learning c++ in college (have used MATLAB quite a bit already) and I'm having a problem with a lab code that is supposed to convert from either Fahrenheit to Celsius or vise versa depending on which the user wants to do. I'm getting a ([Error] ld returned 1 exit status) when I try to run it. If I put return 0 at the very end of the code (e.g. after the last set of brackets, I don't currently have it included), it tells me ([Error] expected unqualified-id before 'return'), so I can't get it to work either way. Can someone look it over and tell me what I'm doing wrong? Thanks.


//Conversions between temperatures
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int main ()
{
double df, dc;
int type;
cout<<"Enter 1 to convert to celsius and 2 to convert to farhenheit."<<endl;
cin>>type;

if (type==1)
{cout<<"Enter temperature in fahrenheit."<<endl;
cin>>df;
dc=5/9*(df-32);
cout<<"The temperature in celsius is "<<dc<<endl;}

else if (type==2)
{cout<<"Enter temperature in celsius."<<endl;
cin>>dc;
df=(9/5)*dc+32;
cout<<"The temperature in fahrenheit is "<<df<<"."<<endl;}

else (type);
{cout<<"Please enter a valid integer.";}
return 0;
}
Last edited on
 
else (type);

That statement does nothing.
There are two problems:
1) (type); is a the same as (expr); i.e. The compiler evaluates type as an expression, but does nothing with it.
2) The ; terminates the statement. The following cout is going to be executed unconditionally. What you want is simply:
1
2
else 
  cout<<"Please enter a valid integer."; 


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
([Error] ld returned 1 exit status)
is a linker error, for example if you try to call a function which cannot be found, that would be a typical cause. The full error message should give more detail of what exactly is not found.

else (type);
I'm not sure what you are trying to do here. (type); is a valid statement, but it doesn't do anything. What is needed is simply else

By the way, your code has integer division, 5/9 and 9/5, those won't give the required result, you will need to put 5.0/9.0 to make the values type double rather than int.

Alright guys, thanks so much. I re-saved the file under a different name and finally got the program to run. I'll make sure to use code tags next time. Thanks again!
Last edited on
Why would this be happening?

probably a stray semicolon after the else. (As in your original code).
Did you fix the:
 
else (type);

Topic archived. No new replies allowed.