basic-computer-games/train/c/main.c
Michael 919e5bb1f5 removed debugging code
Signed-off-by: Michael <michael.lindman@gmail.com>
2022-02-17 03:45:34 +00:00

41 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
int main() {
while (true) {
srand(time(0));
printf("Train\n");
printf("Creative Computing Morristown, New Jersey\n");
printf("Time - Speed Distance Exercise:");
int car = (rand() %(65-40+1))+40;
int distance = (rand() %(20-5+1))+5;
int train = (rand() %(39-20+1))+20;
printf(" A car traveling %d MPH can make a certain trip in %d hours less than a train traveling at %d MPH.\n", car, distance, train);
while (true) {
char input[5];
printf("\nHow long does a trip take by car ");
fgets(input, 5, stdin);
printf(input);
double answer = atof(input);
int car_time = distance * train / (car - train);
int percent = abs(((car_time)-answer)*100/answer)+.5;
if (percent > 5) {
printf("Sorry, you were off by %d percent\n", percent);
printf("Correct answer is %d hours\n", car_time);
} else {
printf("Good!, Answer within %d percent\n", percent);
printf("Another Problem? (yes or no)");
char newgame[5];
fgets(newgame, 5,stdin);
if (strcmp("yes", newgame)) {
break;
}
return 1;
}
continue;
}
}
}