Thread: how to accept a carriage return ?

  1. #1
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121

    how to accept a carriage return ?

    Here is my problem...

    I want the user to be able to press enter and then the line of text to repeate
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
         char lol[20];
         while(1)
         {
              printf("command: ");
              scanf("%s", lol);
              if(strcmp(lol, "\n") == 0)
              {
                // reprint "command"
              }
          }
    }
    But... this does not work, that is my code basically. Or that is what i tried rather, I also tried "\r" but that didnt work and neither did EOF. N E ideas?
    -- Will you show me how to c++?

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    because you are using a while loop and you have set the condition to "while(1)," it will always be true, therefore, it is unneccassary to manuelly reset it. after it prints the char "lol," it will automaticly start the while again. remember, while is a loop for a reason.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
         char lol[20];
         while(1)
         {
              printf("command: ");
              scanf("%s", lol);
              printf("&s\n", lol);
          }
    }
    theoreticly this should work, correct me if im wrong.

    EDIT: Sorry, didnt realize what u meant at first. this doesnt really fix your problem. im on a mac, although i believe if
    your on windows you can do...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
         char lol[20];
         while(1)
         {
              printf("command: ");
              scanf("%s", lol);
              printf("&s\n", lol);
              system("PAUSE");
          }
    }
    although that may be wrong. macs pwn over windows. lol.
    Last edited by PSPJunkie; 01-08-2007 at 09:52 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf ignores \n - it reads upto the \n and leaves \n in the input stream
    to read whole line including \n use fgets (see the FAQ about details how to read from the stdin using fgets)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    soo fgets will accept the carriage return, right and now wait for a stream or character or something like that?

    And my example was not really the best looking back, the whole point of the example was to make so i can accept <enter> as i would a character and it would go to the next line. Like in the dos shell.
    -- Will you show me how to c++?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Maybe you meant to do something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
       char line[BUFSIZ];
    
       for(;;)
       {
          fputs("Please enter a string of characters.\n: ", stdout);
          fflush(stdout);
          if( fgets(line, sizeof line, stdin) != NULL  && line[0] != '\n' )
          {
             puts(line);
          }
          else if( line[0] == '\n' )
          {
             continue;
          }
          else
          {
             break;
          }
       }
    
       return 0;
    }
    Of course, now this means that the only way to stop the loop is to send EOF.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    May be this will help you

    Code:
    #include<stdio.h>
    
    int main()
    {
        char str[80];
        
        printf("Enter Command :\\>");
        
        while(fgets(str, sizeof str, stdin) != NULL && *str != '\n' && *str != EOF)
        {
            printf("Your Command > %s", str);
            printf("Enter Command :\\>");
        }    
        
        printf("You are out\n");
        
        getchar();
        return 0;
    }
    
    /* my output
    Enter Command :\>copy con hello
    Your Command > copy con hello
    Enter Command :\>dir
    Your Command > dir
    Enter Command :\>ipconfig
    Your Command > ipconfig
    Enter Command :\>
    */
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM