재귀함수 scanf 질문

kimhyuns55의 이미지

#include <stdio.h>
int mystrlen(char arr[]) {
        int i;
        for (i=0;arr[i];++i);
        return i;
}
int recur(char answer[],char blind[],int chance) {
        int i, correctCount = 0;
        char input;
        if(chance > 0) {
                printf("chances left [%d] // %s //\n", chance, blind);
                printf("Guess one character : ");
                scanf("%c", &input);
                for(i=0;i<mystrlen(answer);++i) {
                        if(answer[i] == input) {
                                blind[i] = answer[i];
                                correctCount++;
                        }
                }
        }
        if(correctCount == 0) chance--;
        if(chance == -1) return 0;
        else recur(answer, blind, chance);
}
int main () {
        int i, chance = 3;
        char answer[] = "answer", blind[100] = "*****************";
        blind[mystrlen(answer)] = 0;
        recur(answer, blind, chance);
}

문자열의 문자를 맞춰나가는 행맨 게임을 작성 중입니다만, int recur 함수에서 두번째로 recur 함수를 다시 부를때, input을 스캔하려 하지 않습니다.

위의 코드를 실행했을 때, 아래와 같은 결과가 나옵니다.

chances left [3] // ****** //
Guess one character : a
chances left [3] // a***** //
Guess one character : chances left [2] // a***** //
Guess one character : n
chances left [2] // an**** //
Guess one character : chances left [1] // an**** //
Guess one character : s
chances left [1] // ans*** //

첫번째 입력은 'a'로 했으며, 글자를 맞추어 chance가 줄어들지 않아야 함에도 불구하고, 다음 recur에서 input을 읽지 않아, chance가 줄어들게 되는것으로 보입니다.
두번째, 세번째 입력또한 각각 정답인 n과 s로 입력하였습니다.

어째서 input을 scan하지 않게 되는지에 대한 답변을 부탁드립니다.

raymundo의 이미지

처음에 a 를 입력할 때 엔터를 친 것까지 입력 버퍼에 들어가서 입력 버퍼에 a, \n 순으로 들어가 있는데,
처음 scanf 는 'a'를 읽고 아직 버퍼에 \n 이 남아 있는 상태에서
다음 번 scanf 가 그 \n을 읽어 버려서 그렇습니다.

간단히는 scanf 포맷 문자열을

scanf(" %c", &input);   // %c 앞에 스페이스

위와 같이 하면 버퍼의 앞부분의 공백문자들을 저 스페이스가 매칭되며 읽어버리고 %c 에 맞는 입력을 기다리게 됩니다.

아니면 흔한 방법으로 scanf 바로 뒤에 getchar()를 한 번 불러도 되고요.

좋은 하루 되세요!

kimhyuns55의 이미지

말씀해 주신대로 수정하니 문제없이 실행됬습니다
답변 정말 감사합니다!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.