-1

So I'm a newbie to programming with C, but I have a relatively easy equation I'm messing with and can't seem to fit why it keeps crashing.

Is it something with the syntax or do I have a 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 the city to which you would like to fly \n");
printf ("Enter the first character of the name of the 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 an error.. \n");
    return 0;

}





printf("Enter the following values for type of aircraft to be 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 is invalid \n");
    return 0;

}

flight_hours = distance/speed;
((float) (distance%speed)/(float)speed)*60;
return 0;
}
Jclee
  • 39
  • 9
  • 1
    What does your debugger tell you? It should tell you exactly which line of code is crashing. And much more. So do use it to help you find your problems. But in your case it looks pretty obvious. `scanf ("%c", city);` should be scanf ("%c", &city); – kaylum Feb 07 '16 at 05:41
  • Ahh I didn't catch that, I've changed the corresponding syntax, I get everything working except my equation, it just returns 0, isn't that how I get my time in hours and minutes in decimals? – Jclee Feb 07 '16 at 06:18
  • 1
    Again, use your debugger rather than just asking for the answer. It's an essential dev skill to know how to use a debugger. Execute the code line by line and inspect the variable values at each point to see whether they are the expected values. That should tell you where things start going wrong at which point you can dig further. But as an obvious thing, what do you think this line does: `((float) (distance%speed)/(float)speed)*60;`? Looks like it does nothing to me. The value calculated is discarded. – kaylum Feb 07 '16 at 07:01

3 Answers3

0

Remove return 0; from your switch statement.

It should look like

default :
        printf("You've made an error.. \n");

Also your scanf() should be

scanf("%c", &city);
nalzok
  • 14,965
  • 21
  • 72
  • 139
Rguarascia
  • 193
  • 3
  • 20
0

Change scanf ("%c", city); to scanf ("%c", &city);

Also, to make the program work properly every time, do:

scanf (" %c", &city);

Also, check for errors, by the checking the return values of scanf(). If it returns EOF, there is an error.

Also, check this question for the return values of scanf().

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
0

I do not know what compiler you are using, but hopefully it has warnings you can turn on, because they should tell you your problems. For me, it found the ones other people mentioned, plus one more:

% cc -o char char.c
char.c:26:14: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
scanf ("%c", city);
        ~~   ^~~~
char.c:78:17: warning: format specifies type 'float *' but the argument has type 'int *' [-Wformat]
    scanf("%f", &velocity);
           ~~   ^~~~~~~~~
           %d
char.c:98:44: warning: expression result unused [-Wunused-value]
    ((float) (distance%speed)/(float)speed)*60;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
3 warnings generated.
J. V. A.
  • 529
  • 3
  • 11