My program keeps crashing

Hello!
so this is supossed to print out the name of the worker, the time they check in, and if they are late they get paid less. The code runs fine until you input the time thats when it crashes. I´m guessing the problem is because of the Do/While command i´m using.

More about the program...
I know that the program is in spanish so i´ll give a short description of the program.
There are 3 work positions and there are 4 possible time marks you can be on time, on the tolerance range, tardy or you can have non attendance. The program is supossed to be working as long as the time doesnt go past 8:30 AM cause thats when you get marked the non attendance (Thats where i used the do/while) and i think thats where the problem is.

More info.
Currently working on an Hp Pavilon 14 with Windows 8, i tested the program on an Acer with windows 10. In both it crashes.
I´m working with Dev C++

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
 #include<stdio.h>
#include<math.h>
#include<conio.h>
#include<windows.h>

int main()
{
	printf ("Daniela Abigail Zapata Rodriguez 1838226 \n");
	printf ("Francisco Joseth Rangel Romo 1628173 ")	;
	
	char Nom;
	int Dep ;
	float S,Hor;
	
	printf ("\n Nombre De Empleado:\n");
	scanf ("%s",&Nom);

	printf ("\n Menu\n");
	printf ("\n Selecciona Un Departamento\n");
	printf ("\n1:Tecnico\n");
	printf ("\n2:Mantenimiento\n");
	printf ("\n3:Ingeniero\n");
	
	printf ("\n Elige Un Departamento \n");
	scanf ("\n%d", &Dep);
	printf ("\n Hora De Entrada (Formato de hora 00.00) \n");
	scanf ("\n%d", &Hor);
	
	do
	{
		switch (Dep)
		{
			case 1: /*Tecnico*/
			if (Hor==08.01||Hor<=08.15) /*Tolerancia */
			{
					S= (700 - (700*.01));
			}
			else
			{
				if (Hor==08.16|| Hor<08.30) /*Tarde*/
				{
					S= (700 - (700*.02) );
				}
				else
				{
					if (Hor<=08.00); /* A Tiempo */
					{
						S= 700;
					}
				}
			}
				break; /* Fin Caso 1 */
				
			case 2: /*Mantenimiento*/
			if (Hor==08.01||Hor<=08.15) /*Tolerancia */
			{
					S= (1000 - (1000*.025));
			}
			else
			{
				if (Hor==08.16|| Hor<08.30) /*Tarde*/
				{
					S= (1000 - (1000*.03) );
				}
				else
				{
					if (Hor<=08.00) /* A Tiempo */
					{
						S= 1000;
					}
				}
			}	
				break; /* Fin Caso 2 */
				
			case 3: /*Ingeniero*/
			if (Hor==08.01||Hor<=08.15) /*Tolerancia */
			{
					S= (1500 - (1500*.03));
			}
			else
			{
				if (Hor==08.16|| Hor<08.30) /*Tarde*/
				{
					S= (1500 - (1500*.04) );
				}
				else
				{
					if (Hor<=08.00) /* A Tiempo */
					{
						S= 1500;
					}
				}
			}
				break; /* Fin Caso 3*/
		}
		
	printf ("\n Empleado %s  \n ",Nom);
	printf ("\n Su Salario Es. %.2f \n", S);
	printf ("\n Su Hora De Entrada Es. %d", Hor);
	
	}

	while(Hor<08.30);
	
	printf ("Empleado. %c ", Nom);
	printf("\n Usted Tiene Falta \n");
	printf ("\n Su Hora De Entrada Es. %d", Hor);
	printf ("Debido A Retardo No Se Efectuara Pago \n");
	system ("Pause");
				
}
Last edited on
First step compile with high level of warnings, I used cpp.sh (the gear icon top right of the code) with all 3 warnings on:

In function 'int main()':
27:21: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'float*' [-Wformat=]
46:21: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
97:35: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'int' [-Wformat=]
99:45: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
107:45: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]


So you need to get the format arguments right, look here:

http://en.cppreference.com/w/cpp/io/c/fscanf
http://en.cppreference.com/w/cpp/io/c/fprintf

By the way, putting 0 before your numbers has no effect.

Line 46: remove the semicolon.

Equality with floating point numbers does not work, they are not stored exactly. Either stick with relational operators <, > <=, >= or write your own function for "equality" : See if the absolute difference between the 2 numbers is less than some precision value.

If you use scanf, check the return value to see if it worked.
Last edited on
By the way, putting 0 before your numbers has no effect.

You're right! But I suggest changing it to "putting 0 before your floating point literals has no effect".

There are floating-point hex literals in the language, but as I just learned, no floating-point octal literals. (As a warning to OP, integer literals with leading 0s are octal. Maybe the inconsistency is best avoided by omitting the leading zero unless you need an octal number.)
Program is working! thanks!!
i just have to find a way that it shows the full name and not a single letter but thank you!!!
Last edited on
if (Hor==08.01||Hor<=08.15)

As I said before about equality: Hor==08.01 will almost always be false,change it to

if (Hor >= 8.01 || Hor<= 8.15)

Nom is of char type it only hods a single char, not an entire Name.

If you use scanf, check the return value to see if it worked. You are asking for trouble otherwise. One has to be careful when C programming.

Always put a default case in a switch, one can use it to catch errors, like an else clause in an if statement.

Each of the cases has similar code for the lateness, can you figure out how to put that into 1 function?

S is not a good variable name; what does it mean? I shouldn't have to guess.
Last edited on
while(Hor<830/100);

That does integer division before assigning to float, so it will be 8.0 . Always put digits before and after the decimal point when dealing with floating point. Always put the while part of a do loop with the closing brace of the body, so it doesn't look like a stand alone while loop with a null statement:

} while(Hor < 830.0 / 100.0);

Even better, 830.0 / 100.0 is a constant value, make it so with a const variable, and prefer double throughout the progam:

1
2
3
4
5
6
const double MaxLateness = 8.3;
double Hor = 0.0; // always initialise variables to something

// ...

} while (Hor < MaxLateness) ;
Program is working!


That doesn't mean it is correct, or cannot be improved :+) Just to let you know, hopefully you will take all the advice on board.
okaaaay so i try to implement some of the things you suggested but since its for a school subject the teacher asked us to keep it only on the stuff he taught during the semester that's why i cant add the conts double thing.
By the way i googled how to check the return value for a scan i just didn't understand it can you explain it to me??

here is the new code i changed the "S" variable to "Pay" and added 2 more variables for when you are on time or early (HorTempra) and the one you suggested for when you are late (HorFalta)

here is the code...
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
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<windows.h>

int main()
{
	printf ("\n Daniela Abigail Zapata Rodriguez 1838226 ");
	printf ("\n Francisco Joseth Rangel Romo 1628173 \n ")	;
	
	char* Nom;
	int Dep ;
	float Pay,Hor, HorFalta, HorTempra;
	/* Variables */
		Pay=0;
		Hor=0;
		HorFalta=8.3;
		HorTempra=8;
	
	do /*inicio de ciclo*/
	{
		
		printf ("\n Bienvenido a la plataforma \n");
		printf ("\n Nombre De Empleado:\n");
		scanf ("%s",&Nom);

		printf ("\n Menu\n");
		printf ("\n Selecciona Un Departamento\n");
		printf ("\n1:Tecnico\n");
		printf ("\n2:Mantenimiento\n");
		printf ("\n3:Ingeniero\n");
	
		printf ("\n Elige Un Departamento \n");
		scanf ("\n%d", &Dep);

		printf ("\n Hora De Entrada (Formato de hora 00.00) \n");
		scanf ("\n%f", &Hor);
		
		
		switch (Dep)
		{
			case 1: /*Tecnico*/
			if (Hor>=08.01&&Hor<=08.15) /*Tolerancia */
			{
					Pay= (700 - (700*.01));
			}
			else
			{
				if (Hor>=08.16&&Hor<08.30) /*Tarde*/
				{
					Pay= (700 - (700*.02) );
				}
				else
				{
					if (Hor<=HorTempra) /* A Tiempo */
					{
						Pay= 700;
					}
				}
			}
				break; /* Fin Caso 1 */
				
			case 2: /*Mantenimiento*/
			if (Hor>=08.01||Hor<=08.15) /*Tolerancia */
			{
					Pay= (1000 - (1000*.025));
			}
			else
			{
				if (Hor>=08.16|| Hor<08.30) /*Tarde*/
				{
					Pay= (1000 - (1000*.03) );
				}
				else
				{
					if (Hor<=HorTempra) /* A Tiempo */
					{
						Pay= 1000;
					}
				}
			}	
				break; /* Fin Caso 2 */
				
			case 3: /*Ingeniero*/
			if (Hor>=08.01&&Hor<=08.15) /*Tolerancia */
			{
					Pay= (1500 - (1500*.03));
			}
			else
			{
				if (Hor>=08.16&&Hor<08.30) /*Tarde*/
				{
					Pay= (1500 - (1500*.04) );
				}
				else
				{
					if (Hor<=HorTempra) /* A Tiempo */
					{
						Pay= 1500;
					}
				}
			}
				break; /* Fin Caso 3*/
			default
		}
		
	printf ("\n Empleado %s   ",Nom);
	printf ("\n Su Salario Es. %.2f ", Pay);
	printf ("\n Su Hora De Entrada Es. %.2f \n", Hor);
	
	} while (Hor<HorFalta);
	
	printf ("\n Empleado. %s ", Nom);
	printf("\n Usted Tiene Falta ");

	printf ("\n Debido A Retardo No Se Efectuara Pago \n");
	system ("Pause");
				
}

Hi,

Edited quite a bit:
scanf returns the numbers of successfully assigned values, so in this case it should be 1. Do an if statement to check that.

It's a good idea to declare and assign a value all in 1 line:

1
2
3
4
5
6
float Pay,Hor, HorFalta, HorTempra;
	/* Variables */
		float Pay = 0.0 ;
		float Hor = 0.0 ;
		float HorFalta = 8.3;
		float HorTempra = 8.0;


Code is easier to read when there are spaces around the operators. I always put digits either side of the decimal point, it reinforces the idea that the number is a float, and saves the compiler an implicit cast.

Please remove the leading zero from your values, as I said earlier they are pointless. As in 8.30 not 08.30

You have a default case, but it doesn't do anything. It should at least report an error with printf. Ideally offer another case so that the user can Quit. I prefer a while loop instead of a do loop.

What happens if someone arrives at 8.00 or before?
Last edited on
took the advice on the 1 value per line
and before 8.00 they just get fully paid
I tried adding the scan verification but the program gets stuck after you input the hour so I left it as a comment.

here the code
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
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<windows.h>

int main()
{
	printf ("\n Daniela Abigail Zapata Rodriguez 1838226 ");
	printf ("\n Francisco Joseth Rangel Romo 1628173 \n ")	;

	/* Variables */
	char Nom[35];
	int Dep ;
	float Pay= 0.0;
	float Hor;
	float HorFalta= 8.3;
	float HorTempra= 8;
	
	do /*inicio de ciclo*/
	{
		Pay=0.0;
		
		
		printf ("\n Bienvenido a la plataforma \n");
		printf ("\n Nombre De Empleado:\n");
		scanf ("%[^\n]",&Nom);

		printf ("\n Menu\n");
		printf ("\n Selecciona Un Departamento\n");
		printf ("\n1:Tecnico\n");
		printf ("\n2:Mantenimiento\n");
		printf ("\n3:Ingeniero\n");
	
		printf ("\n Elige Un Departamento \n");
		scanf ("\n%d", &Dep);

		printf ("\n Hora De Entrada (Formato de hora 00.00) \n");
		scanf ("\n%f", &Hor);
		
			/*scanf verificacion 
			if (scanf("%f", &Hor) == 1)
			{
				printf ("\n Se ha verificado la informacion \n");		
			} 
		
			else
			{		
				printf ("\n Informacion corrompida \n");
			}
			fin de la verificacion */
				
				
		switch (Dep)
		{
			case 1: /*Tecnico*/
			if (Hor>=8.01&&Hor<=8.15) /*Tolerancia */
			{
					Pay= (700 - (700*.01));
			}
			else
			{
				if (Hor>=8.16&&Hor<8.30) /*Tarde*/
				{
					Pay=(700 - (700*.02) );
				}
				else
				{
					if (Hor<=HorTempra) /* A Tiempo */
					{
						Pay=700;
					}
				}
			}
				break; /* Fin Caso 1 */
				
			case 2: /*Mantenimiento*/
			if (Hor>=8.01&&Hor<=8.15) /*Tolerancia */
			{
					Pay=(1000 - (1000*.025));
			}
			else
			{
				if (Hor>=8.16&&Hor<8.30) /*Tarde*/
				{
					Pay=(1000 - (1000*.03) );
				}
				else
				{
					if (Hor<=HorTempra) /* A Tiempo */
					{
						Pay=1000;
					}
				}
			}	
				break; /* Fin Caso 2 */
				
			case 3: /*Ingeniero*/
			if (Hor>=8.01&&Hor<=8.15) /*Tolerancia */
			{
					Pay=(1500 - (1500*.03));
			}
			else
			{
				if (Hor>=8.16&&Hor<8.30) /*Tarde*/
				{
					Pay=(1500 - (1500*.04) );
				}
				else
				{
					if (Hor<=HorTempra) /* A Tiempo */
					{
						Pay=1500;
					}
				}
			}
				break; /* Fin Caso 3*/
			
			default:
			printf ("\n Su departamento no existe \n");
			Pay=0.0;
			
		}
		
	printf ("\n Empleado %s   ",Nom);
	printf ("\n Su Salario Es. %.2f ", Pay);
	printf ("\n Su Hora De Entrada Es. %.2f \n", Hor);
	
	} while (Hor<HorFalta);
	
	printf ("\n Empleado. %s ", Nom);
	printf("\n Usted Tiene Falta ");

	printf ("\n Debido A Retardo No Se Efectuara Pago \n");
	system ("Pause");

}

[

1
2
3
4
5
6

int ArgsRead = scanf("%f", &Hor);

if (ArgsRead !=1) {
   printf("Failed to read input\n");
}


Put that into a while loop, and do it for every scanf, ideally have a function which does it.
Topic archived. No new replies allowed.