How to enter a discount value based on a year variation? Example: >2010=7% and <2010=12%

Asked

Viewed 22 times

0

I developed a program where the user will inform the year of the vehicle and then the program will display on the screen the discount for the year. I still need to add a way to ask if he wants to calculate again, if not, finish. I can do it in Python if necessary.

The code I wrote:

int
main ()
{
  int anocarro;
  printf ("Digite o ano de fabricacao do veiculo: \n");
  scanf ("%i", &anocarro);

  if (anocarro <= 2010);
  printf ("%i", "\nO desconto sera 7%");
  else (anocarro >= 2010);
  printf ("%i", "\nO desconto sera 12%");
}

The mistakes were:

main.c:19:13: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 > has type ‘char *’ [-Wformat=]
  printf ("%i", "\nO desconto sera 7%");
            ^
main.c:20:3: error: ‘else’ without a previous ‘if’
  else (anocarro >= 2010);
  ^~~~
main.c:21:13: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
  printf ("%i", "\nO desconto sera 12%");
  • Doesn’t have this ; at the end of the if line (this ; is closing the if command). It makes no sense to specify a %i format if you are not printing a number. Use: printf ("\nO desconto sera 7%");. You could even use printf ("%s", "\nO desconto sera 7%"); but I don’t see much point.

1 answer

0


The problem is you’re saying you want to print one inteiro(in fact the %i is to inform that the base can be either 8, as 10 or 16) and is passing a string.

printf ("%i", "\nO desconto sera 7%");

To learn more about the % access this link

A way to solve the problem of printf() would replace %i for %s or put the string directly(printf("Sua string aqui"))

Now the other problem is if and else, is that as you are not putting the {} there is one ambiguity

To solve this, you can place the blocks({})

Making the changes the code would look like this:

int main ()
{
  int anocarro;
  printf ("Digite o ano de fabricacao do veiculo: \n");
  scanf ("%i", &anocarro);

  if (anocarro <= 2010){
  printf ("\nO desconto sera 7%");
  }else (anocarro >= 2010){
  printf ("\nO desconto sera 12%");
  }
}
  • One ; I’ve already fixed. Otherwise, apparently I can’t use %, at least with the online compiler I’m using. I replaced it with the word and it worked. Anyway, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.