2011-08-29 2 views
1

저는 개인적인 여가와 학습을 위해 프로젝트를 진행하고 있습니다. 그것의 일부는 다음과 같습니다첫 번째 가져 오기 요청을 건너 뛰는 함수 (문자열 #)를 가져옵니다.

#include<stdio.h> 
#include<string.h> 
wgame() 
{ 
char string3[12], string2[12], string1[12], string4[12], string5[12]; 
memset (string1, 0, 11); 
memset (string2, 0, 11); 
memset (string3, 0, 11); 
memset (string4, 0, 11); 
memset (string5, 0, 11); 
printf("reference C correct\n"); 
printf("Okay, so you want a game. Here's one for you\n\n\n"); 
printf("This is a word game.\n\n A noun is a person place or thing.\n A verb is 
something that you can get up and do.\n A subject is what the conversation is about.\n"); 
printf("Go ahead, type a subject:\n"); 
gets(string3); 
printf("That's a good one. Now, type a verb:\n"); 
gets(string2); 
printf("How about another:\n"); 
gets(string4); 
printf("Really? Okay. Now, type in a noun:\n"); 
gets(string1); 
printf("Cool. How about typing another noun:\n"); 
gets(string5); 
printf("Allright, here's how your words fit into this game:\n\n\n\n\n"); 
printf("When the %s was %s the %s %s all the other %s", string1, 
string2, string3, string4, string5); 
return 4; 

} 

내 문제는 출력이 "얻는다 (문자열 #)"처음 스킵하고 다음 "의 printf()"로 진행되고 있다는 점이다. 누군가 왜 이것이라고 말할 수 있습니까?

+1

'wgame()'은 (는) int wgame (void)이어야합니다. * 절대 사용하지 말라'gets()'; 안전하게 사용할 수 없으며 언어에서 제거되고 있습니다. 의미있는 변수 이름을 사용하십시오. "마술 숫자"('11','12')를 피하십시오. 들여 쓰기 코드. –

답변

4

wgame 전에 scanf의 일부가 stdio 버퍼에 \n으로 표시 될 수 있습니다.

  • gets를 사용하지 마십시오 scanfgets
  • 를 혼합하지 마십시오 : 여기

    당신이해야 할 몇 가지 있습니다. 사용 fgets
  • fflush(stdin)을 제안하는 사람의 말을 경청하지 마십시오. 잘못된입니다. 그러나

    /* Right before `wgame` begins. */ 
    while((c = getchar()) != '\n' && c != EOF) 
        ; 
    

    을 사용자 입력이 위험 폐기, 그것은 아껴서 사용해야합니다주의 : 큰 관심과 절제와

, 당신은 사용할 수 있습니다.

이 주제에 대한 내용은 C FAQ이고 플러시 표준 시간은 explanation입니다.

0
#include<stdio.h> 
#include<stdlib.h> 
#define size 5 

void main() 
    { 
    char *str,*name[size]; 
    int i,n; 
    scanf("%d",&n); 
    printf("%d",n); 
    fflush(stdin); // using fflush here gets() isn't skipping else i have to use scanf() 
    for(i = 0; i < n; i++) 
    { 
     str = (char*)malloc(20*sizeof(char)); 
     printf("enter a name :\n"); 
     //scanf("%s",str); 
     gets(str); 
     name[i]=str; 
    } 
    printf("the entered names :\n"); 
    for(i = 0; i < n; i++) 
    puts(name[i]); 
    } 
+1

앞으로 비슷한 문제에 직면 한 사람들을 위해 여기서 무엇을하고 있는지 설명하고 싶을 수도 있습니다. :) – Neograph734

관련 문제