Thread: scanning in character string

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    scanning in character string

    I have a question in regards to scanning in a character array with spaces for a project im working on for an introductory class in C programming.

    I know that I cant use the scanf function becasue it will stop scanning in the characters once it sees a space. Our teacher told us to use the "gets" function.

    The string can have a maxium length of 50 characters. So I defined the string variable as
    char str[51];
    to leave an extra space for the the \0

    I can get the gets function to work in an easy case

    Code:
    char str[51];
    printf("Enter the string:");
    gets(str);

    it works fine for this. This is probally a dumb question, but will the gets function be skipped, if it is inside a loop. My program just seems to skip right over the gets function, but if I replace it with a scanf function everything works fine(except for the fact that I cant scan in more than one word).

    Here is the basic structure of the part of the program that I am having trouble with.

    Code:
    #include<stdio.h>
    #define SIZE 50
    
    int selection(void);  //prototype of function that chooses encryption method
    
    int main()
    {
      int choice=0;
      char str[SIZE];
    
      while(choice!=5)
      {
        choice=selection();
        printf("Enter the string now: ");
        gets(str);
      }
    return(0);
    }
    
    
    int selection(void)
    {
      int choice;
      int lcv=1;
    
      printf("Chose method 1-5 by entering integer, ect.")
      while(lcv==1)
      {
        scanf("%d",&choice);
        if(//input validation)
        {
           correct while loop while quit
        }
        else
        {
          while loop will continue
        }
      }
    return(choice);
    }

    In this case, the program doesnt stop and give the user a chance to input a string like it did in the first example.

    any help would be appreciated
    thanks,
    Zack

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Also, the reason that gets is being skipped is because it is picking up the newline character left on the screen by scanf(), Don't use gets, but if you use scanf(), keep in mind that it leaves the enter on the buffer, waiting for some input function to grab it.
    Last edited by chrismiceli; 11-23-2004 at 12:46 AM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    so I should use fgets?

    would this be better, replace the

    Code:
    gets(str);
    with

    Code:
    fgets(str, SIZE, stdin);
    I am also not sure what stdin is for. Thanks for the help

    Zack

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    FILE *fp = fopen( "file.txt", "r" );
    fgets( buff, sizeof buff, fp );
    stdin is just the predefined variable of type FILE* which is attached to the standard input stream of your program.

    Similarly,
    Code:
    scanf( "%d", &myint );
    fscanf( stdin, "%d", &myint );
    are the same thing.

    To avoid all the problems which scanf has with leaving newlines (and generally poor error recovery in general), just use fgets() to read a line, then use sscanf() to parse the line.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zackboll
    would this be better, replace the

    Code:
    gets(str);
    with

    Code:
    fgets(str, SIZE, stdin);
    Sure, it'd be better. Unless you're Mister C.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Salem
    To avoid all the problems which scanf has with leaving newlines
    Code:
    scanf("%d\n");
    Solved


    To flush the input buffer you may use.
    Code:
     
    void FlushInput(){
        char c;
        do{
            c=getchar();
        }while( c!= '\n' && c!= EOF);
    }
    Disadvantage: if there's nothing in the input buffer you'll have to write something to it. But there's an alternative, although not very popular.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%d\n");
    Nope - that just creates a different set of problems

    Your FlushInput() also declares c as char, whereas it should be int if you want to meaningfully compare with EOF

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by xErath
    Code:
    scanf("%d\n");
    Solved
    www.eskimo.com/~scs/C-faq/q12.17.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    So stdin is referring to the input buffer. I am not reading the string from a file, but rather the user types it in.

    will I have to flush the buffer after the user goes through the "scanf" for a single integer input, but before going through the "fgets". I know that fflush(stdin) is bad from various members of the board, as well as the faq, but for some reason, that is what our teacher tells us to do. I was having a problem before with the "gets" function being skipped becasue it would read in a newline from the "scanf" function. If I somehow cleared the buffer after my "scanf" statement, and used "fgets" instead of "gets", would my problem be solved?

    thanks for the help,
    Zack


    Quote Originally Posted by Salem
    Code:
    FILE *fp = fopen( "file.txt", "r" );
    fgets( buff, sizeof buff, fp );
    stdin is just the predefined variable of type FILE* which is attached to the standard input stream of your program.

    Similarly,
    Code:
    scanf( "%d", &myint );
    fscanf( stdin, "%d", &myint );
    are the same thing.

    To avoid all the problems which scanf has with leaving newlines (and generally poor error recovery in general), just use fgets() to read a line, then use sscanf() to parse the line.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Dave_Sinkula
    Hum... still it did work many times with me... Ok then, no newline!

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    thanks

    thanks for link

    Quote Originally Posted by Dave_Sinkula
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Hum... still it did work many times with me
    Yeah, "works for me" is a continual hazard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM