what is wrong with my code

console application in c++ have error with that code it says illigle else without matching if please check and post the correct code
note: it is a tempreture convertor

// convertor.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int c,t;
cout<<"please write c to convert from (c-f) or f to convert from(f-c)";
cin>>c;
if(c=='c')
cout<<"pls enter the tempreture in celcius";
cin>>t;
cout<<5/9+(t-32);
else if(c=='f')
cout<<"pls enter the tempreture in fahrenheit";
cin>>t;
cout<<9/5+t+32;
return 0;
}


Last edited on
I have corrected some of the syntax errors. It is important to use the correct types. So I have made your variable c a char because it expects to be set to a character, either 'c' or 'f'.

Your variable t I have made a float because that stores decimal numbers of the form 98.4

Your formulae are still wrong, you need to research those.

Your statement after the if and else if clauses need to be in braces {}.

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
#include <iostream>

using namespace std;

int main(int argc, char* argv[])  // This is the correct signature for the main function
{
	char c; // this is a character 'c' or 'f'
	float t; // this has a decimal point

	cout<<"please write c to convert from (c-f) or f to convert from(f-c)";
	cin>>c;

	if(c=='c')
	{ // You need to put braces round the next few instructions
		cout<<"pls enter the tempreture in celcius";
		cin>>t;
		cout<<5/9+(t-32);  //this is wrong
	}
	else if(c=='f')
	{ // Here too you need braces
		cout<<"pls enter the tempreture in fahrenheit";
		cin>>t;
		cout<<9/5+t+32; // this is wrong
	}

	return 0;
}
Last edited on
Use code tags and post the exact error message you're getting the next time.
Your problem is that you forgot the curly braces around the if block.
Topic archived. No new replies allowed.