2016-10-31 4 views
2

À l'origine, j'utilisais scanf, mais je courais dans le caractère newline coincé dans le stdin. Tout ce que j'ai lu disait de passer aux fgets et d'utiliser plutôt sscanf. Avec ça, j'ai décidé de passer à ça ... mais ça ne marche toujours pas. Ci-dessous vous trouverez mon code. Ma question est, qu'est-ce que je fais mal avec mes fgets et sscanf qui l'oblige à ne pas attendre l'entrée de l'utilisateur?Fgets et sscanf n'attendant pas l'entrée C

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct f_in{ 
    char outline; 
    int lines; 
    int rows; 
    int num_args; 
} F_IN; 

typedef struct args_in { 
    char in_string[20]; 
    int t_format; 
} ARGS_IN; 

void printInterface(char argQs[5][50], char argChar); 

int main(int argv, char** argc){ 
    char defaultQuestions[5][50] = { { "1) What char for border?" } 
      , { "2) Add question" } 
      , { "3) Remove Question" } 
      , { "4) Print last answers" } 
    , { "5) Exit" } }; 
    int commandEntry, exitFlag; 
    char borderChar = '*', addQ[50],userInp[1]; 

    exitFlag = 1; 

    while (exitFlag){ 
      printInterface(defaultQuestions, borderChar); 

      printf("Enter the integer value for the command you wish to select: "); 
      fgets(userInp, sizeof(userInp),stdin); 
      sscanf(userInp,"%d", &commandEntry); 
      printf("\nYou selected: %s\n", defaultQuestions[commandEntry - 1]); 

      userInp[0] = 0; 

      if (commandEntry == 1){ 
        printf("Please enter the character you wish to be the border: "); 
        fgets(userInp,sizeof(userInp),stdin); 
        sscanf(userInp,"%c",&borderChar); 
      } 
      else if (commandEntry == 2){ 
        printf("What question would you like to add? (only enter 50 char max)\n"); 
        fgets(addQ, 50, stdin); 
        printf("This was your question: %s", addQ); 
      } 
      else if (commandEntry == 5){ 
        printf("Goodbye!\n"); 
        exitFlag = 0; 
      } 
    } 
    return 0; 
} 

void printInterface(char argQs[5][50], char argChar){ 
    int i, j; 
    int lineCnt = 13; 
    int borderLen = 75; 

    for (i = 0; i<100; i++){ 
      printf("\n"); 
    } 

    for (i = 0; i<lineCnt; i++){ 

      if (i == 0 || i == lineCnt - 1){ 
      for (j = 0; j<borderLen; j++){ 
          printf("%c", argChar); 
        } 
        printf("\n"); 
      } 

      else if (i >= 3 && i <= 10){ 
        printf("%c %s", argChar, argQs[i - 3]); 
        for (j = 0; j < ((borderLen - strlen(argQs[i - 3]))-6); j++){ 
          printf(" "); 
        } 
        printf("%c\n", argChar); 
      } 

      else{ 
        for (j = 0; j<borderLen; j++){ 
          if (j == 0){ 
            printf("%c", argChar); 
          } 
          else if (j == (borderLen - 1)){ 
            printf("%c\n", argChar); 
          } 
          else{ 

        for (j = 0; j<borderLen; j++){ 
          if (j == 0){ 
            printf("%c", argChar); 
          } 
          else if (j == (borderLen - 1)){ 
            printf("%c\n", argChar); 
          } 
          else{     
            printf(" "); 
          } 
        } 
      }   
    }     

    for (i = 0; i<10; i++){ 
      printf("\n"); 
    } 

} 
+1

Qu'est-ce qui ne fonctionne pas exactement? Quelle est l'entrée? Quelle est l'attente de production ? Modifiez votre question et clarifiez cela. –

+0

Qu'est-ce qui se passe, c'est qu'il n'attend pas l'entrée de l'utilisateur sur la première question posée, et immédiatement en boucle à nouveau et répéter, se terminant par une boucle infinie. Ce qui est attendu est qu'il attendra une entrée d'utilisateur pour faire l'une des 5 actions par défaut données. – Dpry12

+0

Cela a fonctionné @ user3121023, mais qu'est-ce que cela a fonctionné par rapport à userInp [1]? – Dpry12

Répondre

2

"userInp [1] ne permet de suffisamment de mémoire pour stocker la terminaison '\ 0'" - user312023