K
K
komy_ccilky_kidaesh_pidr2018-02-22 11:30:21
C++ / C#
komy_ccilky_kidaesh_pidr, 2018-02-22 11:30:21

Why doesn't code (c) work?

Always prints the word vorona, even if the number is not in the range. Please help me find the error. I can't figure out what's wrong

#include <stdio.h>
void main()
{
  printf("Enter the number in range of 1-99. \n");
  int a, v;
  scanf_s("%i", &a);
  int m = a % 10;
  if (((a>=11) && (a <= 14)) || (m=0) || ((m>=5) && (m<=9)))
  {
    v = 0;
  }
  else if ((m >= 2) && (m <= 4))
  {
    v = 1;
  }
  else if (m = 1)
  {
    v = 2;
  }
  else v=3;
  switch (v)
  {
  case 0:printf("%i voron", a); break;
  case 1:printf("%1 voroni", a); break;
  case 2:printf("%i vorona", a); break;
  case 3:printf("uncorrected"); break;
  }
  _getch();
  return 0;
}

assignment: A non-negative number k(1-99) is given. Type the phrase "k crows". (Example: If K = 23, then "23 crows" should be printed, if K = 51 - then "51 crows", pay attention to the case K = 11,12,13,14).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2018-02-22
@Mercury13

https://ru.wikipedia.org/wiki/Yoda_Terms
Already in two places.
Moreover, in C you don’t need to put brackets in difficult conditions, as in Pascal, it’s quite a ride ...
But no, you added unnecessary parentheses and drowned out the error.
Also what I don't like.
Instead of 0,1,2,3 it is better to use the MANY/FEW/ONE/UNKNOWN constants.
In case 1, the wildcard is badly ripped off the board.
If the condition on MANY is the most complex and is fulfilled according to the residual principle, why do we do it first? Better something like…
• 10…20 → MANY
• 1 → ONE
• 2…4 → FEW
• Else MANY

R
Rsa97, 2018-02-22
@Rsa97

Because you don't have comparisons (==) in your conditions, but assignments (=).
Use the Yoda style .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question