0

I have been given this question as an assignment but I haven't got very far, I don't know how to properly use a switch function for this, and I'm not sure how to complete it. Can anyone help?

enter image description here

struct car
{
   char model[50];
   int manufacture_year;
   float price;
};

int main()
{

int i;
int function;
struct car array[2];


for(i=0; i<2; i++) {

   printf("what is the cars model? ");
   scanf(" %s", &array[i].model);

   printf("What year was the car manufactured? ");
   scanf(" %d", &array[i].manufacture_year);

   printf("How much does it cost? ");
   scanf(" %f", &array[i].price);

   printf("\n");

}
  printf("press 1 to show model, 2 to show price and 3 to terminate");
  scanf("%d", &function);


}

that's what I have so far... a switch is meant to come after I think.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
u Ahsin
  • 1
  • 1
  • 2
    Please avoid including links to external pictures. For [homework](https://stackoverflow.com/help/on-topic) you should first read about `switch` and then ask if you still have problems. – milbrandt Feb 04 '18 at 13:30
  • 1
    [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Aditi Rawat Feb 04 '18 at 13:32
  • 1
    What exactly is the problem? – J...S Feb 04 '18 at 13:56
  • When calling any of the `scanf()` family of functions: 1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the '%s' and/or '%[...]' input format specifiers, always include a MAX CHARACTERS modifier that is one less than the length of the input buffer. This is because those input format specifiers append a NUL byte to the input and to avoid any possibility of overflowing the input buffer (which would be undefined behavior and can lead to a seg fault event. – user3629249 Feb 05 '18 at 05:55
  • regarding: `scanf(" %s", &array[i].model);` In C, referencing the name of an array degrades to the address of the first byte of the array. Adding that '&' is not correct when the rest is already an address. – user3629249 Feb 05 '18 at 06:00
  • this: `printf("press 1 to show model, 2 to show price and 3 to terminate"); scanf("%d", &function);` would be better written as: `int ch; while( (ch = getchar()) != EOF && '\n' != ch ); do { printf( "%s", "menu: \n1: select Model to show\n2: enter minimum price\n3: quit\n"); int ch = getchar(); } while( 1 > ch && 3 < ch );` the `while()` cleans out stdin the `do` assures a valid input given by the user. – user3629249 Feb 05 '18 at 06:08

1 Answers1

0

You put the switch after the function that inputs the value of the switch variable (in this case, scanf), like this:

/* preceding code */
printf("press 1 to show model, 2 to show price and 3 to terminate");
scanf("%d", &function);
switch (function) {
    case 1:
        show_model(array, 2); /* placeholder */
        break;
    case 2:
        show_price(array, 2); /* placeholder */
        break;
    case 3:
        break;
}

The syntax for the switch is shown above. The values you want to test for come after the case keywords. And, after the statement(s) for each case, there usually is a break statement to exit the switch. If there weren't a break statement under the first printf for example, execution would continue to the next statement, which may or may not be desirable.

user351579
  • 129
  • 6