char in if statments

Im not sure how to right it to so that if i input c for car then it will use the carcharges an so on. type is a char

1
2
3
4
5
6
if (type == 'c') 
		carcharges (hourin, minin, hourout, minout, charge, minutes, hours);
	else if (type == 't')
		truckcharges (hourin, minin, hourout, minout, charge, minutes, hours);
	else if (type == 'b')
		buscharges (hourin, minin, hourout, minout, charge, minutes, hours);
closed account (S6k9GNh0)
What's wrong with that code?
You could make it look nicer via a switch statement.
when i run my program it just passes over this if statement


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void getdata (int* hourin, int* minin, int* hourout, int* minout);
void carcharges (int hourin, int minin, int hourout, int minout, float charge, int minutes, int hours);
void truckcharges (int hourin, int minin, int hourout, int minout, float charge, int minutes, int hours);
void buscharges (int hourin, int minin, int hourout, int minout, float charge, int minutes, int hours);
void output (int hourin, int minin, int hourout, int minout,int* roundedtime, int* minutes, int* hours, float* charge);

int main (void)
{

	char type;
	int hourin;
	int minin;
	int hourout;
	int minout;
	int hours;
	float charge;
	int minutes;
	int roundedtime;


	getdata (&hourin, &minin, &hourout, &minout);

	printf("Type of vehicle (c - t - b)\t\t");
	scanf("%c\n", &type);

	if (type == 'c') 
		carcharges (hourin, minin, hourout, minout, charge, minutes, hours);
	else if (type == 't')
		truckcharges (hourin, minin, hourout, minout, charge, minutes, hours);
	else if (type == 'b')
		buscharges (hourin, minin, hourout, minout, charge, minutes, hours);

	system ("pause");
	output (hourin, minin, hourout, minout, &roundedtime, &minutes, &hours, &charge);

	system ("pause");
	return 0;
}



void getdata (int* hourin, int* minin, int* hourout, int*minout)
{

	printf("Hour vehicle entered lot(0 - 24)\t");
	scanf("%d", &hourin);

	printf("Minute vehicle enterd lot(0 - 60)\t");
	scanf("%d", &minin);

	printf("hour vehicle left lot  (0 - 24)\t\t");
	scanf("%d", &hourout);

	printf("Minute vehicle left lot  (0 - 60)\t");
	scanf("%d", &minout);

	return;
}

void carcharges (int hourin, int minin, int hourout, int minout, float charge, int minutes, int hours)
{

	hours = hourout - hourin;

	if (minout < minin)
		hours -= 1;
	else if (minout > minin)
		hours += 1;
	else
		hours = hours;

	minutes = abs(minin - minout);

	if (hours < 3 && minutes < 60)
		charge = 0;
	else
		charge = (hours - 2) * 1.5;

	

return;
}

void truckcharges (int hourin, int minin, int hourout, int minout, float charge, int minutes, int hours)
{
	hours = hourout - hourin;

	if (minout < minin)
		hours -= 1;
	else if (minout > minin)
		hours += 1;
	else
		hours = hours;

	minutes = abs(minin - minout);

	if (hours < 2 && minutes < 60)
		charge = hours * 1;
	else
		charge = 2 + (2.30 * (hours - 2));

	return;

}

void buscharges (int hourin, int minin, int hourout, int minout, float charge, int minutes, int hours)
{
	hours = hourout - hourin;

	if (minout < minin)
		hours -= 1;
	else if (minout > minin)
		hours += 1;
	else
		hours = hours;

	minutes = abs(minin - minout);

	if (hours < 1 && minutes < 60)
		charge = hours * 2;
	else
		charge = 2 + (3.70 * (hours - 1));

}
void output (int hourin, int minin, int hourout, int minout, int* roundedtime, int* minutes, int* hours, float* charges)
{
	roundedtime = hours +1;

	printf("\t\t\t\t PARKING LOT CHARGE \n");
	printf("Type of vehicle: \tfix this part\n");
	printf("TIME-IN\t\t\t %d : %d\n", &hourin, &minin);
	printf("TIME-OUT\t\t\t %d : %d\n", &hourout, &minout);
	printf("\t\t\t\t --------\n");
	printf("PARKING TIME\t\t %d : %d\n", &hours, &minutes);
	printf("ROUNDED TOTAL\t\t %d\n", &roundedtime);
	printf("\t\t\t\t ---------\n");
	printf("TOTAL CHARGe\t\t\t $%f", charges); 

return;
}


thats my whole code if that helps.
I'm not very good at C so I'm not sure what the correct solution is but if you change line 28 to scanf("\n%c", &type); it appears to read the char correctly.

In other parts of your program you are passing the wrong type to printf and scanf. Increase your compiler's warning level and he will tell you more.
Last edited on
closed account (zb0S216C)
If the proposition(s) of an if statement evaluates to false (depending on the comparison operator you've used, of course), the corresponding if block is ignored. They aren't just skipped for no reason.

It's a strange occurrence, so my suggestion is this: Remove the new-line character from the format string in scanf(). Whether this suggestion actually works, I don't know, but it's worth a try. If it does in fact work, then the question would be: Why does it work?

Wazzak
Last edited on
i tried both suggestions and when i do it doesn't let me enter the type of vehicle
I don't see how getdata works.

You are passing in pointers to ints and then taking the address of those pointers in the scanf statements

shouldn't it be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void getdata (int* hourin, int* minin, int* hourout, int*minout)
{

	printf("Hour vehicle entered lot(0 - 24)\t");
	scanf("%d", hourin);

	printf("Minute vehicle enterd lot(0 - 60)\t");
	scanf("%d", minin);

	printf("hour vehicle left lot  (0 - 24)\t\t");
	scanf("%d", hourout);

	printf("Minute vehicle left lot  (0 - 60)\t");
	scanf("%d", minout);

	return;
}

?
Topic archived. No new replies allowed.