My coding errors

Hi there,
i'm having hard time fixing my code.
It gives me scanf C4996 warning, but i don't know what i did wrong!
The program should calculate the miles driven using main function and then calculate the miles per gallon using user defined function (fuel).

many thanks in advance.

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

#include <stdio.h>
#include <conio.h>
#include <math.h>

#define value1 gallon_used
#define value2 starting_miles
#define value3 ending_miles

double Fuel(int value1, int value2, int value3);

void main()
{


	int value1, value2, value3;
	int miles_driven = value3 - value1;


	printf("Enter the gallon used:");
	scanf("%d", &value1);
	printf("Enter the starting miles:");
	scanf("%d", &value2);
	printf("Enter the ending miles:");
	scanf("%d", &value3);

	print("Results:");

	printf("The miles driven are %Lf\n", miles_driven);
	printf("The miles per gallon was: %Lf\n", Fuel(value1, value2, value3));
	printf("Press any key to continue");
getch();
	
}

double Fuel(int value1, int value2, int value3)
{
	return ((value3 - value2)/(value1));

}
Last edited on
Other than the void main(), you didn't do anything wrong except not defining the Macro to disable this bogus warning. See this link:
https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx

Last edited on
Thanks for the help.

But the main error was missing f after print for result.
I still don't get the correct output out of this.

It gives me zero for miles driven and zero for miles per gallon.

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

#include <stdio.h>
#include <conio.h>
#include <math.h>

#define value1 gallon_used
#define value2 starting_miles
#define value3 ending_miles

double Fuel(int value1, int value2, int value3);

void main()
{

	
	int value1, value2, value3;
	double milesdriven = value3 - value2;


	printf("Enter the gallon used:");
	scanf_s("%f", &value1);
	printf("Enter the starting miles:");
	scanf_s("%d", &value2);
	printf("Enter the ending miles:");
	scanf_s("%d", &value3);
	printf("Results:\n");
	printf("The miles driven are %Lf\n", milesdriven);

	printf("The miles per gallon was: %.4Lf\n", Fuel(value1, value2, value3));
	printf("Press any key to continue");
	
	getch();
  
}

double Fuel(int value1, int value2, int value3)
{
	return ((value3-value2)/(value1));

}



Any Help?
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
#include <stdio.h>
#include <conio.h>
#include <math.h>
using namespace std;

#define value1 gallon_used
#define value2 starting_miles
#define value3 ending_miles

double Fuel(double value1, double value2, double value3);

double Fuel(double value1, double value2, double value3)
{
	return ((value3 - value2) / (value1));
}

int main()
{
	double value1 = 0, value2 = 0, value3 = 0;
	double milesdriven = 0;

	printf("Enter the gallon used:");
	scanf_s("%lf", &value1);
	printf("Enter the starting miles:");
	scanf_s("%lf", &value2);
	printf("Enter the ending miles:");
	scanf_s("%lf", &value3);
	printf("Results:\n");

	milesdriven = value3 - value2;

	printf("The miles driven are %lf\n", milesdriven);
	printf("The miles per gallon was: %.4lf\n", Fuel(value1, value2, value3));
	printf("Press any key to continue");

	getch();
	return 0;
}


You tell it milesDriven = value3 - value2; before you even input values to value3 and value2.
Last edited on
Why'd you all write it in C ? This is a website about C++ .
Last edited on
Topic archived. No new replies allowed.