-3

My problem is that when I ask the user to give me a number, the user can write more than one number at a time(16548,5484,etc), so my program saves those inputs and when the next time it needs to use scanf it automaticaly reads the next one the user wrote(in my first example it would be '6'). I don't want that, i want the user to only be able to write only one character, or if there is a way to delete those extra characters so it will only read the first character, that would also solve the problem for me.

  • "The system" is not going to ignore characters for you. Your program is going to have to read every character the user types. If the user types something he's not supposed to, it's your program's job to decide that. (But with that said, dealing with unexpected, incorrect, or unwanted user input can be tricky. It is, unfortunately, *especially* tricky if you're trying to use `scanf` for most of your input.) – Steve Summit Sep 20 '18 at 16:46
  • 1
    Use `fgets` or `getchar` to read unformatted characters instead of depending on scanf formats. – stark Sep 20 '18 at 16:48
  • 1
    Don't use `scanf()`, almost ever, for anything. If you cannot guarantee the format of your input, you have no business using that function. – torstenvl Sep 20 '18 at 16:48
  • You have an imaginary problem. The behaviour you have is expected and natural. The behaviour you think you want is counter-intuitive and frustrating. You should do exactly nothing and let the user type ahead, just like every other console-oriented program does. – n. m. could be an AI Sep 20 '18 at 16:55
  • You can try something like `"%c%*[^\n]"` – jxh Sep 20 '18 at 16:56
  • Unix/curses: https://stackoverflow.com/q/45413808/315052 – jxh Sep 20 '18 at 17:00
  • 1
    If you want users to enter *one number per line*, you should not use `scanf` at all, you should read *lines* and parse them (and give *an error message* if there is extra input, instead of ignoring said input). – n. m. could be an AI Sep 20 '18 at 17:01
  • Thanks for all the answers, i will try out all and see whats works the best! – GPFlopi Sep 20 '18 at 17:04
  • I can't stop wonder why all newbies end up using `scanf` It's one of the most difficult functions to master and therefore the cause of most mistakes. Use `fgets` …. (and repeat until you see a newline) – Support Ukraine Sep 20 '18 at 17:11
  • 1
    @4386427 It's because virtually all introductory C textbooks and teachers teach them to. `scanf` *seems* like an easy and straightforward way of reading user input, without getting involved in the additional complexities (whatever those are) of `fgets` and the better methods. And it's true: if all you want to do is read, say, one int, `scanf("%d", &i)` is a pretty easy way to do it. But at some point, of course, `scanf` falls completely apart -- except that by then, the learner has become just dependent enough on it that it *seems* easier to augment it (somehow) rather than abandon it... – Steve Summit Sep 20 '18 at 17:51
  • "to make scanf to read only one character at a time" --> `char ch; if (scanf("%c", &ch) ...`, but using `scanf()` is **not** the recommended here. You need to post your code. – chux - Reinstate Monica Sep 20 '18 at 20:58
  • @SteveSummit Informative and correct [summary](https://stackoverflow.com/questions/52429656/in-c-how-to-make-scanf-to-read-only-one-character-at-a-time#comment91804891_52429656) about `scanf()`. – chux - Reinstate Monica Sep 20 '18 at 21:13

1 Answers1

-1

You can try int or float value , result will be same, you should do rounding

    #include<stdio.h>
    #include<math.h>
    void main()
    {
        float v;
        printf("Enter a number : "); 
        scanf("%f",&v);
        while(1)
        {
            if(v<10.0)
            {
                printf("%.f",floor(v));
                break;
            }
            else
            {
                v = v / 10.0;
            }
        }


}