IF/ELSE not working

Write your question here.
I am new at C++ and having problems with this code, it always takes the first option on the IF even if I hardcode the option to a different number, any ideas?


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
  int main()
{
	void calcfar(int inputtemp); /* subroutine to calculate f degrees to c degrees */
	void calccel(int inputtemp); /* subroutine to calculate c degrees to f degrees */
	wchar_t selection='1';
	int temperature;
	printf( "Temperature conversion \n");
	printf( "Please choose one of the following \n");
	printf( "1 = Farhenheit to Celsius conversion \n");
	printf( "2 = Celsius to Farhenheit conversion \n");
	printf( "Enter any other key to exit: ");
	scanf("%c", &selection);
	printf( "Enter the temperature that you would like to convert: ");
	scanf_s("%i", &temperature);
	if (selection = '2')
		calccel(temperature); /* call celsius conversion */
	else if(selection = '1')
			calcfar(temperature); /* call farhenheit conversion */
	else 
		printf("Exiting program\n");
	return(0);
 } /* end main */
void calcfar(int inputtemp)
{
	int converttemp;
	converttemp = (inputtemp -32) * (5.0/9.0);
	printf("The Fahrenheit/Celsius conversion is %3f %06.3f\n", converttemp);
	return;
} /* end function */
void calccel(float inputtemp)
{
	float converttemp;
	converttemp = (9.0/5.0) * (inputtemp + 32);
	/* printf( "The Celsius/Fahrenheit conversion is %3f %6.3f\n", converttemp); */
	printf("The Celsius conversion is %f\n", converttemp);
	return;
} /* end function */
You are doing assignment in your if statement checks. Use == for comparison.
closed account (jyU4izwU)
You Must Have x2 "="Try This:
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
  int main()
{
	void calcfar(int inputtemp); /* subroutine to calculate f degrees to c degrees */
	void calccel(int inputtemp); /* subroutine to calculate c degrees to f degrees */
	wchar_t selection='1';
	int temperature;
	printf( "Temperature conversion \n");
	printf( "Please choose one of the following \n");
	printf( "1 = Farhenheit to Celsius conversion \n");
	printf( "2 = Celsius to Farhenheit conversion \n");
	printf( "Enter any other key to exit: ");
	scanf("%c", &selection);
	printf( "Enter the temperature that you would like to convert: ");
	scanf_s("%i", &temperature);
	if (selection == '2')
		calccel(temperature); /* call celsius conversion */
	else if(selection == '1')
			calcfar(temperature); /* call farhenheit conversion */
	else 
		printf("Exiting program\n");
	return(0);
 } /* end main */
void calcfar(int inputtemp)
{
	int converttemp;
	converttemp = (inputtemp -32) * (5.0/9.0);
	printf("The Fahrenheit/Celsius conversion is %3f %06.3f\n", converttemp);
	return;
} /* end function */
void calccel(float inputtemp)
{
	float converttemp;
	converttemp = (9.0/5.0) * (inputtemp + 32);
	/* printf( "The Celsius/Fahrenheit conversion is %3f %6.3f\n", converttemp); */
	printf("The Celsius conversion is %f\n", converttemp);
	return;
} /* end function */
Shows you how new I am, I thought == meant not =. Thank you for helping me out.
closed account (jyU4izwU)
No Problem, One Day You will Be The One Showing People.
Last edited on
For future reference, if you want 'not equal' use !=
Topic archived. No new replies allowed.