简体   繁体   中英

Bug with quantity of float numbers on C

Well, first of all, I'm nothing close to versed on C, so this might have a simple answer, but I can't figure out what it could be. Moreover, my English is not what you'd call perfect, so I apologize in advance if I make myself hard to understand.

I'm trying to do this little program that would let me do simple calculations for travel time with fixed speeds, where all I'd have to input is the distance to travel. I started to code it, and soon enough I got a semi-functional console program, but then it started to grow and grow, and then came a point where I seemingly can't add any more float numbers to the code. Here's the code so far:

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

/* TENGO QUE ARREGLAR ESTO, ME CAUSA BRONCA CON EL NÚMERO TOTAL DE FLOAT NUMBERS*/
main()
{
float x1, km, mn, mn1, mn1a, mn1b, mn1c, mn2, mn2a, mn2b, mn2c, mn3, mn3a, mn3b,      mn3c, mn4;
  int c=1;
  printf("\n\n\tTiempo de recorrido\n\n");
  if (x1 >= 0)
  {
  do
  {
  printf("\tEscribe distancia en mapa: ");
  scanf("%f", &x1);
  c = x1;
  km = x1*50;   // Distancia real
  mn = km;
  mn1 = mn/5;   // Tiempo de viaje
  mn1a = mn1/18; // Jornadas de marcha
  mn1b = mn1 + (floorf(mn1a)*6); // Tiempo total del viaje
  mn1c = mn1b/24; // Días totales de viaje
  mn2 = mn/8;// Tiempo de viaje
  mn2a = mn2/18; // Jornadas de marcha
  mn2b = mn2 + (floorf(mn2a)*6); // Tiempo total del viaje
  mn2c = mn2b/24; // Dias totales de viaje
  mn3 = mn/25;// Tiempo de viaje
  mn3a = mn3/18;



  //Instrucciones PRINT
  printf("\n\n\tNumero de kilometros = %3.2f km", km);
  if (mn1 < 24){
  printf("\n\n\tTiempo a 5 km/h (caminata) = %3.2f horas", mn1);
  }
  else{
  printf("\n\n\tViaje en caminata (5 km/h)\n");
  printf("\n\tTiempo de recorrido = %3.2f horas", mn1);
  printf("\n\tJornadas de marcha = %3.2f jornadas", mn1a);
  printf("\n\tTiempo total = %3.2f horas", mn1b);
  printf("\n\tDias totales = %3.2f dias\n", mn1c);}
  if (mn2 < 24){
  printf("\n\tTiempo a 8 km/h (a caballo) = %3.2f horas", mn2);}
  else{
  printf("\n\n\tViaje a caballo (8 km/h)\n");
  printf("\n\tTiempo de viaje = %3.2f horas", mn2);
  printf("\n\tJornadas de marcha = %3.2f dias", mn2a);
  printf("\n\tTiempo total = %3.2f horas", mn2b);
  printf("\n\tDias totales = %3.2f dias\n", mn2c);
  }
  if (mn3 < 24){
  printf("\n\tTiempo a 25 km/h = %3.2f horas\n", mn3);
  }
  else{
  printf("\n\n\tTrote de caballo (25 km/h)\n");
  printf("\n\tTiempo de viaje = %3.2f horas", mn3);
  printf("\n\tJornadas de marcha = %&.2f días", mn3a);
  printf("\n\tJornadas de marcha = %3.2f horas", mn3);     
  }
  }
  while (c >= 0);
  }
  else{
  getch();
  }
 }

Now, when I try to run the code as it is above, there's no problem with the output. But, when I try to add another variable declaration below (with mn4 which is already added on the float declaration), the program bugs out: I only get the text "Escribe la distancia en el mapa" (without the : symbol"), and when I type any key, it sends the message "Process exited with return value 49/50", and it closes after the next keystroke.

I've tried thinkering with the code in any way I can think of, but nothing worked, except that it changed the return values between 49 and 50, for some reason. I hope someone could tell me how to fix this, or, if that's not possible, to tell me why it's so, so I can find a way around that.

Thanks in advance.

This happens because you wrapped everything in a great big if (x1 >= 0) . It seems like you do a branch on an uninitialised variable at the very beginning for...no discernable reason.

I think you meant to read x1 and then condition your computations on its value instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM