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++
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:
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.
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.)
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
constdouble MaxLateness = 8.3;
double Hor = 0.0; // always initialise variables to something
// ...
} while (Hor < MaxLateness) ;
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)
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?
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.
#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");
}