Program skipping my if statement

My program is skipping my if statement, it is suppose to run a menu which after choosing your gas, a user inputs 0-30 seconds and it will spit out a distance traveled in the time through said gas. The only issue is the code jumps past my if statement (located at the very end of the code posted) and ends up spitting out my else (Invalid Time) which is meant for time<0 || time>30.

I left out the other subroutines because they are a mirror to the CO2 subroutine with different co-eff as seen at the beginning. Any help at all would be greatly appreciated, first time taking real programming other than simple HTML / Assembly.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Directories */
#include <stdio.h>

/* Constants */
#define CO2_mps 258
#define Air_mps 331.5
#define He_mps 972
#define H_mps 1270

/* Main Function */
int main()
{
	/* Subroutines */
	void CO2();
	void Air();
	void He();
	void H();

	/* Gas Selection Menu */
	int gas;
	printf("Choose Gas\n");
	printf("1) Carbon Dioxide\n");
	printf("2) Air\n");
	printf("3) Helium\n");
	printf("4) Hydrogen\n");
	printf("\nGas #");
	scanf_s("%d", &gas);

	/* Selection Options */
	switch (gas)
	{
	case 1:
		printf("Carbon Dioxide Chosen\n");
		CO2();
		break;
	case 2:
		printf("Air Chosen\n");
		Air();
		break;
	case 3:
		printf("Helium Chosen\n");
		He();
		break;
	case 4:
		printf("Hydrogen Chosen\n");
		H();
		break;
	default:
		printf("Invalid Selection\n");
		break;
	}

}

/* CO2 Subroutine */
void CO2()
{
	/* Defining Variables */
	double CO2_sec;
	double CO2_distance;

	/* User Input */
	printf("\nTime To Reach Detection Location From Source (sec):");
	scanf_s("%1f", &CO2_sec);

	/* Calculation */
	CO2_distance = CO2_mps * CO2_sec;

	/* Data Output */
	if (CO2_sec > 0 && CO2_sec < 30)
		printf("Source Is %1f From Detection Location\n", CO2_distance);
	else
		printf("Invalid Time\n");
	
	return;
}
Last edited on
This is why we prefer iostream over stdio. The scanf at line 64 is scanning a float, but you've passed the address of a double. Change line 59 to float CO2_sec;
This appears to be written in C, rather than C++. This would be a lot easier using C++.

scanf_s("%1f", &CO2_sec);

%1f doesn't look right. I suspect it's meant to be %lf ; that's not a "1"; it's a lower case "L".

ahh I see! And apologies I don't know the difference between C and C++ I guess this is the wrong forum, you managed to fix my error anyways thank you!
Topic archived. No new replies allowed.