-1

I am a coding-beginner and would like to hear your advice relating to following solution of this exercise:

Write a program that loops prompting for positive or zero integers of data type long. Then the number of digits the integer consists of (in decimal representation) should be printed to stdout. Entering a negative number immediately stops the program. Output examples: 0 has 1 digit. 999 has 3 digits. etc.

I've written the code below and according to the tests I did, the program fulfills all given tasks. But what do you think about it? How can I improve it?

(And I also think that I am not allowed to use any finished helpful function in any c-library. It is just 'plain' C coding or so. Idk how to describe it.)

(The programming language is C)

#include <stdio.h>

int main(void) 
{
  long number;
  int n=0;

  do
  {
      printf("Enter a number: ");
      scanf_s("%ld", &number);

      if (number > 0)
      {
          while (number != 0)
          {
              number /= 10;
              n++;
          }
      }
      else if(number == 0){
          n = 1;
      }
      else {
          exit();
      }

      printf("The number you've entered has %d digits.\n\n",n);
      n = 0;

  } while (getchar() != 'EOF');

  return 0;
}
  • 1
    What is `'EOF'`? Doesn't the compiler yell warning at you? – alk Apr 01 '17 at 15:37
  • Sorry but SO is not the place to ask for opinion, only facts. http://stackoverflow.com/help/dont-ask – Rob Apr 01 '17 at 15:37
  • 3
    IMO, This question belongs to https://codereview.stackexchange.com – ayushgp Apr 01 '17 at 15:37
  • 1
    `exit()` requires an argument. – alk Apr 01 '17 at 15:38
  • 2
    `exit` requires `#include ` – BLUEPIXY Apr 01 '17 at 15:41
  • exit(EXIT_FAILURE) i mean, exit(EXIT_SUCCESS) in other way – user 7362383 Apr 01 '17 at 15:41
  • 2
    You should check the outcome of the `scanf_s()` call. As it stands if it fails `number` will be used uninitialised, which might very well provoke undefined behaviour, anything may happen. – alk Apr 01 '17 at 15:43
  • And looking at the code from the aesthetic point-of-view: The indention is inconsistent. – alk Apr 01 '17 at 15:45
  • Also to make sure the prompt appears *before* the console asks for input call `fflush(stdout)` immediately after `printf("Enter a number: ");`. – alk Apr 01 '17 at 15:47
  • Thanks for all the answers! I did add the fflush and the EXIT_FAILURE including stdlib to my code. Questions: 1. Actually I wanted to use scanf(), but somehow my VS Studio 2017 tells me I should use scanf_s(). I looked after it, but I can only find http://www.cplusplus.com/reference/cstdio/scanf/ . 2. Do you mean with "incosistent code" that I didn't always put my curly braces on the same 'place'? – physics Apr 01 '17 at 15:56
  • I wasn't writing about "*inconsistent *code**". But yes, I was referring to the way the braces are placed. – alk Apr 01 '17 at 16:09
  • `scanf_s()` as provided by VC is a MS extension to C. Ìf used correctly `scanf()` would do as well. MSDN docs for `scanf()` are here: https://msdn.microsoft.com/en-us/library/9y6s16x1.aspx – alk Apr 01 '17 at 16:10
  • Which compiler are you using?? You should definitely enable -Wall -Wextra -Werror options – Erik W Apr 01 '17 at 17:18
  • Ok thanks, now I know the difference between scanf_s and scanf and sorry, I will use google a bit more longer to find such commands instead of asking here. I think Erik meant that ^^. – physics Apr 01 '17 at 18:30
  • And the way I've solved this task is okay? I mean the if-else-statements and the do-while with that EOF 'query'. – physics Apr 01 '17 at 18:35
  • 1
    I'm voting to close this question as off-topic because this question should be migrated to https://codereview.stackexchange.com/ – Bob Jarvis - Слава Україні Apr 01 '17 at 23:16
  • okay, sorry. I'll open a new thread on this site then, when this is rlly off-topic here. Thanks everyone for their time and help. – physics Apr 02 '17 at 05:49
  • `'EOF'` and `EOF` aren't the same. – alk Apr 02 '17 at 06:13
  • I've redirected the discussion to http://codereview.stackexchange.com/questions/159583/c-counting-digits-of-numbers btw. But no, the compiler does not warn me about 'EOF'. And yes according to http://www.cplusplus.com/reference/cstdio/EOF/ and http://www.cplusplus.com/reference/cstdio/getchar/?kw=getchar I have to use EOF and not 'EOF'. – physics Apr 02 '17 at 06:40
  • According to http://stackoverflow.com/questions/4358728/end-of-file-eof-in-c you only get EOF, when you press ctrl+z to let the program now when you're done --> EOF. – physics Apr 02 '17 at 06:47

1 Answers1

0

That getchar it's useless because EOF it's to say that a file it's over and you're not reading an file. Change that to while(number >=0).