3

Sorry for the probably dumb question, but i wanted to practice loops a bit and came out with this idea.Basically it ask you to enter or not in a loop and when you're in, it ask you for something to do.The problem is that just after i enter the loop it prints two times the printf string before passing to the scanf one and waiting for an input. I can't figure it out. All help is welcome! Here's the code:

#include <stdio.h>

int main() 
{
    char check = 'a';
    char int_check = 'a';
    int b = 0;
    printf("want to go in? [y or n]\n");
    scanf("%c",&check);
    if ( check == 'y') {
        while (1){
            printf("Waiting: \n");
            scanf("%c",&int_check);
            if ( int_check == 'q'){
                printf("You're out, bye!\n");
                break;
            };
        };
    } else if ( check == 'n'){
        printf("You're not in, see ya!\n");
    }else {
        printf("Please, type 'y' or 'n'\n");
    };
    return 0;
}
5
  • I'm not sure if it's an error-error, but you shouldn't have all those semicolons after your curly braces. Commented Feb 23, 2012 at 21:52
  • 1
    @DanFego It's silly (or at least I find it so), but it doesn't matter. It's treated as a NOP statement IIRC. Commented Feb 23, 2012 at 21:53
  • 3
    You press "y" and then hit return. That's two characters, "y" & "\n". "y" is read in by the pre-loop scanf, and the first scanf in the loop finds the newline character still in the buffer. Commented Feb 23, 2012 at 21:54
  • @pst: Look in the scanf line in the while loop. The destination has a space in it. This is a good solid case for teaching good style when someone is learning a language (e.g., having a space after commas in an argument list, etc.). Commented Feb 23, 2012 at 21:54
  • 1
    yeah that space is an edit error, it isn't int the original programm, sorry :( EDIT: fixed now Commented Feb 23, 2012 at 22:04

2 Answers 2

4

If you input onto a terminal the following:

x

The first loop will see an x

The second loop will see a newline character.

The easiest way to work around this is to use sscanf and getline.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Sharth! i'll try that :) ... sorry if i wasted your time with silly questions :)
@WhiteEyeTree: No worries. It wasn't a silly question. It's one that nearly everyone deals with when learning C. One thing to keep in mind though, is that the extra semicolons are unnecessary and can change the meaning of your program. You should do if (condition) { ... }, and skip the final semicolon. It's not needed, and it adds a null statement to be executed after the conditional.
2

You can change the program to respond to keyboard immediately, i.e. without waiting for the user to press enter. It requires changing the properties of the input terminal, and is generally messier and less portable-seeming than line-oriented input. This page describes how to do it, and here is the code modified to work that way:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

struct termios saved_settings;

void
reset_term_mode(void)
{
  tcsetattr (STDIN_FILENO, TCSANOW, &saved_settings);
}

int main() 
{
    tcgetattr(STDIN_FILENO, &saved_settings);
    atexit(reset_term_mode);

    struct termios term_settings;

    tcgetattr(STDIN_FILENO, &term_settings);
    term_settings.c_lflag &= ~(ICANON|ECHO);
    term_settings.c_cc[VMIN] = 1;
    term_settings.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_settings);

    char check = 'a';
    char int_check = 'a';
    int b = 0;
    printf("want to go in? [y or n]\n");
    scanf("%c",&check);
    if ( check == 'y') {
        while (1){
            printf("Waiting: \n");
            scanf("%c", &int_check);
            if ( int_check == 'q'){
                printf("You're out, bye!\n");
                break;
            };
        };
    } else if ( check == 'n'){
        printf("You're not in, see ya!\n");
    }else {
        printf("Please, type 'y' or 'n'\n");
    };
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.