-3

discrepency

Hi everyone,

Currently working on some Harvard's CS50 problems, I've noticed that substituting get_int by scanf makes my program behave erratically.

The problem at hand here consists in creating a plurality election mockup. And the simple fact that I replace get_int by scanf in order to register the number of votes during the election ends up signalling the first vote as 'invalid' (a function later in the code is charged of checking vote validity).

I've checked multiple times, used the debugger, but to no avail.

Any hints as to why that happens would be deeply appreciated.

  • 1
    `scanf()` has very idiosyncratic behaviour. It's worth devoting at least an hour to a close study of the man pages involved. It does not even behave the same with different format specifiers. Don't mix any input methods: use just one in any program. Function `get_int()` is non-standard. – Weather Vane Jan 01 '21 at 18:17
  • 1
    Edit the question to provide a [mre]. – Eric Postpischil Jan 01 '21 at 18:23

1 Answers1

1

scanf("%i", &vote_count) does not consume anything after the number. What comes after the number is most likely a newline character, which is what will be seen by get_string. That probably causes the first name to be an empty string.

You should see all that if you use a debugger.

rici
  • 234,347
  • 28
  • 237
  • 341