c - Code crashing in codeblocks -
so i'm newbie programming c, have relatively easy equation i'm messing , can't seem fit why keeps crashing.
is syntax or have random equation crashing it?
#include <stdio.h> #define newyork 1077 #define paris 4487 #define london 4336 #define rome 5113 #define frankfurt 4732 #define sanfrancisco 2888 #define tokyo 7252 #define havana 380 // google maps search #define b747 614 #define b777 590 #define a330 567 #define a380 634 #define c 1354 // google maps search int main (void) { int distance, flight_hours, velocity, speed; char city; printf ("please enter city fly \n"); printf ("enter first character of name of city in lower case letter \n"); scanf ("%c", city); switch (city) { case 'n': { distance = newyork; break; } case 'p': { distance = paris; break; } case 'l': { distance = london; break; } case 'r': { distance = rome; break; } case 'f': { distance = frankfurt; break; } case 's': { distance = sanfrancisco; break; } case 't': { distance = tokyo; break; } case 'h': { distance = havana; break; } default : printf("you've made error.. \n"); return 0; } printf("enter following values type of aircraft flown: \n\n"); printf("enter 1 if boeing 747 \n"); printf("enter 2 if boeing 777 \n"); printf("enter 3 if airbus 330 \n"); printf("enter 4 if airbus 380 \n"); printf("enter 5 if concorde \n"); scanf("%f", &velocity); if (velocity == 1) speed = b747; else if (velocity == 2) speed = b777; else if (velocity == 3) speed = a330; else if (velocity == 4) speed = a380; else if (velocity == 5) speed = c; else { printf ("your entry invalid \n"); return 0; } flight_hours = distance/speed; ((float) (distance%speed)/(float)speed)*60; return 0; }
change scanf ("%c", city); scanf ("%c", &city);
also, make program work every time, do:
scanf (" %c", &city); also, check errors, checking return values of scanf(). if returns eof, there error.
also, check this question return values of scanf().
Comments
Post a Comment