Thread: Best Way to Read Input from User (error for whitespace)

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    6

    Best Way to Read Input from User (error for whitespace)

    I'm writing a program that takes in single words from a user. The goal is for the program to take in ONLY a single word (i.e. if there is space in the user's input, it will trigger an error).

    I've read a lot of things about the pros and cons of the different input methods (scanf, gets, fgets, etc), and think I want to avoid gets.

    Currently I'm working with:

    Code:
        gettimeofday(&start, NULL);
        for (int i = 0; i < n; ++i){
            do{
                printf("Word number %d: %s\n>", i+1, arr[i]);
                scanf("%10s", word);
                scanf("%*[^\n]"); scanf("%*c");
          
                if(strcmp(arr[i], word) != 0){
                    printf("Incorrect.\n");
                    correct = 0;
                }
    
                else{
                    correct = 1;
                }
            } while (correct == 0);
        }
        gettimeofday(&stop, NULL);
    
    
    



    This works, but counts for example "word word" as just "word" (by scanf stopping at whitespace).

    Does anyone have any recommendations or advice about how to go about accomplishing what I'm trying to do?

    Or suggestions on the scanf/fgets debate?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    think I want to avoid gets
    gets doesn't even exist in the current language spec.

    You should definitely use fgets since your input is basically "line based" where you only want one word on the line.

    One possibility:
    Code:
    #include <stdio.h>
    
    
    int main() {
        char line[1000], word[100], extra[2];
     
        while (1) {
            printf("Enter one word: ");
            fgets(line, sizeof line, stdin);
            extra[0] = '\0';
            sscanf(line, "%99s%1s", word, extra);
            if (extra[0] == '\0') break;
            printf("I said ONE WORD!!! Try again...\n");
        }
     
        printf("You entered: %s\n", word);
     
        return 0;
    }
    Another possibility:
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main() {
        char line[1000], word[100];
     
        while (1) {
            printf("Enter one word: ");
            fgets(line, sizeof line, stdin);
            int pos = 0;
            sscanf(line, "%99s%n", word, &pos);
            if (line[strspn(line + pos, " \t\n") + pos] == '\0') break;
            printf("I said ONE WORD!!! Try again...\n");
        }
     
        printf("You entered: %s\n", word);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    6
    Awesome, I will give that a try. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you read input from the user without waiting?
    By Isaiah in forum C Programming
    Replies: 5
    Last Post: 01-23-2012, 03:53 PM
  2. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  3. User Input Error
    By peckitt99 in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2006, 11:12 AM
  4. Read a 4x4 matrix from user input
    By JHaney in forum C++ Programming
    Replies: 9
    Last Post: 11-29-2005, 07:56 PM
  5. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM

Tags for this Thread