to take input till enter is pressed

This is a discussion on to take input till enter is pressed within the C Programming forums, part of the General Programming Boards category; Hi Everyone! I am trying to store string input with spaces and the program should stop taking the input while ...

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    to take input till enter is pressed

    Hi Everyone!
    I am trying to store string input with spaces and the program should stop taking the input while the enter key is pressed.
    Code:
    #include<stdio.h>
    int main()
    {
         char c[25];
          int i=0;
         printf("Enter your fullname\n");
          while(c[i]!=    )  // not sure how to check for enter key pressed
          {
               scanf("%c",&c[i]);
               i++;
           }
            c[i]='\0';
          printf("the given name\n");
          i=0;
           while(c[i]!='\0')
           {
              printf("%c",c[i];
              i++;
             }
          printf("\n");
         return 0;
    }

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    287
    '\n'

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    287
    but you should also have a test to make sure you dont input past your array bounds while( i < 25 - 1 && c[ i ] != '\n' )


    25 - 1 because you need room for null byte

  4. #4
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by jwroblewski44 View Post
    '\n'

    It not terminating when I use '\n'

  5. #5
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    with slight changes made to the program got the output,it wasn't the fault of '\n'.
    Code:
    while(i<25-1)
    {
        scanf("%c",&c[i]);
         if(c[i]=='\n')
            break;
    
        i++;
    }

  6. #6
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    thanks a lot.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by shruthi View Post
    thanks a lot.
    The better way to take string input is with fgets(). One of it's best features is that it prevents over-running the string array.

    scanf("%[^'\n']s",mycharBuffer);

    is one way of getting a string with scanf(), which includes white space, up to the newline (enter key).

    scanf() is quite primitive compared to fgets(), and especially known for it's format specifiers being stubborn, and the entire function being "fragile". fgets() is definitely preferred for user input in any serious program.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by Adak View Post
    The better way to take string input is with fgets(). One of it's best features is that it prevents over-running the string array.

    scanf("%[^'\n']s",mycharBuffer);

    is one way of getting a string with scanf(), which includes white space, up to the newline (enter key).

    scanf() is quite primitive compared to fgets(), and especially known for it's format specifiers being stubborn, and the entire function being "fragile". fgets() is definitely preferred for user input in any serious program.
    scanf has "stubborn" format specifiers only in the same sense that printf has stubborn format specifiers: they don't do what you want if you use the wrong specifiers.

    Take your example, for example:
    Code:
    scanf("%[^'\n']s",mycharBuffer);
    This reads all characters up to the first newline or apostrophe (since you put apostrophes in the exclusion list), then it expects to read a literal "s" in the input. The "[" specifier acts on its own without "s"; it's a different specifier.

    There are also ways to limit the size of input with scanf, just like there are ways to limit the size of output with printf:
    Code:
    scanf("%20[^\n]", buffer);
    That will read up to but not including the first newline, but no more than 20 characters.

    Of course, reading only a single length-limited string like this is better achieved with fgets, like you said, but the real power of scanf is when you want to input or parse more than just a simple string (like several numbers in a line, or numbers separated by non-whitespace), perhaps by reading a line with fgets and then parsing it with sscanf. There are, of course, ways to do this parsing manually (eg, strtol/strtof) but scanf/sscanf lets you do it in a single function call.

    Edit: stupid auto-correct messed up my grammar

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks, Christop. Yes, no s, and no ' is needed in the format specifier:
    Code:
    scanf("%[^\n]",mycharBuffer);
    will do it.

    Clearly, it confirms that the scanf() format specifiers are indeed *stubborn*.

  10. #10
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    287
    there is also a function getline(). Im sure its in the standard library, but i enjoy using a version a made. This is a snippet from K&R.

    Code:
    int gotline( char s[] ){
        char * start = s;
        while( ( *start = getchar() ) == ' ' || *start == '\t' ) // skips blanks
            {    }
        start++;
        while( start - s < MAXLINE - 1 && ( *start++ = getchar() ) != '\n' && *( start - 1 ) != EOF )
            {    }
        *start = '\0';
        return start - s;
    }
    With a #define MAXLINE [maximum number of char's in line]

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by jwroblewski44 View Post
    there is also a function getline(). Im sure its in the standard library,
    It's not in the standard library, only in the GNU C library

    Bye. Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How could I make my program switch Sleep() while enter is pressed?
    By cplusplusnoob in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2012, 05:32 PM
  2. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  3. How to find if enter was pressed in an edit control?
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2007, 07:25 PM
  4. ok know it will exacut till pay input
    By fmchrist in forum C Programming
    Replies: 2
    Last Post: 01-02-2006, 06:33 AM
  5. how do you know when enter is pressed?
    By pinkcheese in forum Windows Programming
    Replies: 6
    Last Post: 07-26-2002, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21