Estonia_girl
Estonia_girl

Reputation: 83

End while loop with ctrl+d, scanf?

I want the user to be asked "how many circles" they wanna write until the user decides to end it with (Ctrl+d) which is EOF?

extra question: if I write a letter for example "k" it will spam out circles. How do I change that?

#include <stdio.h>
int main ()
{
    int i;
    int x;

    printf("\nHow many circles do you want to write?\n");
    scanf("%d", &x);

    while(x != EOF)
    {
        for (i = 1; i <= x; i = i + 1)
        {
            putchar('o');
        }
        printf("\nHow many circles do you want to write?"
               "(to end program click ctrl+d at the same time!))\n");
        scanf("%d", &x);
    }
    printf("\n\n Bye! \n\n");
    return 0;
}  

Upvotes: 1

Views: 17661

Answers (4)

&#212;rel
&#212;rel

Reputation: 7622

You can read char by char input :

#include <stdio.h>                                                             
int main ()                                                                    
{                                                                              
    int i;                                                                     
    int x = 0;                                                                 
    int nb = 0;                                                                


    while(x != EOF)                                                            
    {                                                                          
        printf("\nHow many circles do you want to write?\n");                  
        nb = 0;                                                                
        for (x = getchar(); x != '\n'; x = getchar()) {                        
            if (x == EOF)                                                      
                goto end;                                                      
            if (x >= '0' && x <= '9') {                                        
                nb = nb * 10 + x - '0';                                        
            }                                                                  
        }                                                                      
        for (i = 0; i < nb; i++)                                               
        {                                                                      
            putchar('o');                                                      
        }                                                                      
    }                                                                          
end:                                                                           
    printf("\n\n Bye! \n\n");                                                  
    return 0;                                                                  
}     

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726519

The biggest problem with your program is that scanf will not read an EOF into a variable. However, fixing just this problem is not going to make your program entirely correct, because there are other issues in your code:

  • Your code repeats itself - when possible, you should unify the code that deals with the first iteration vs. subsequent iterations.
  • Your code will not handle invalid input - when an end-user enters non-numeric data, your program goes into an infinite loop.
  • Your code follows the old style of C - declaring all variables at the top has not been required for more than fifteen years. You should declare your loop variable inside the loop.

Here is how you fix all these shortcomings:

int x;
for (;;) {
    printf("\nHow many circles do you want to write? (to end the program click Ctrl+D at the same time!)\n");
    int res = scanf("%d", &x);
    if (res == EOF) break;
    if (res == 1) {
         ... // Draw x circles here
    } else {
        printf("Invalid input is ignored.\n");
        scanf("%*[^\n]");
    }
}
printf("\n\n Bye! \n\n");
return 0;

Upvotes: 4

Simon Augustin
Simon Augustin

Reputation: 1

if you're allowed to #include , there are two convenient functions bool kbhit() and char getch(). So you can write

char c=0;
if(kbhit()) c = getch();
if(c== whatever code ctrl+d returns) x=EOF;

Hint: Take a look at what scanf(%d,&x) returns when you enter a letter instead of a number.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134306

As per the man page, scanf() will return EOF, not scan EOF to x as a value.

Return Value

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs......

Also,

if I write a letter for example "k" it will spam out circles, how do I change that?

In case of input of one char value, it causes matching failure, in your case, scanf() returns 0, instead of 1.

So, altogether, you've to collect the return value of scanf() and check check that value for the required condition. You can change your code as

int retval = 0;
while ((retval = scanf("%d", &x))!= EOF && (retval == 1))

Upvotes: 1

Related Questions