Thread: Inputting a sentence into a variable

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    25

    Inputting a sentence into a variable

    Right I need to be able to input a sentence into a c program that can be stored and then later sent over to another program using sockets.

    The problem I`m having at the mom is readin in the sentence.

    I`ve tried setting up a string but when I read into it only the last word is stored.

    Whats the best way to do this ?

    cheers

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Show what you are doing. If your reading from stdin you could use getline(). If you doing it all inside the program you may need the string library functions.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you could always read the FAQ.

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

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    Trying to Make an sms program using sockets.


    At the moment I am usign scanf but it didnt work.

    I ahve to be able read in a string of less than 160 characters./

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    fgets()
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    so the code would be

    Code:
    fgets("%C", stringname);
    ?

  7. #7
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    No.
    Code:
    fgets(char* buffer, int length, FILE* file);
    Which is used like this...
    Code:
    char buff[256];
    
    fgets(buff, sizeof(buff), stdin); /* read from stdin */
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    I want to be able to read the input from the keyboard but fgets is to read from a file isnt it?

  9. #9
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    If you read the comment after the fgets() you would see that the use of stdin is used to read from standard input which is the keyboard.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    Heres the code I have so far

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    main()
    {
    int x=1;
    int selection;
    char username[10];
    char buf[160];
    char *p;
    printf("Please Enter you Username \n");
    scanf("%s", username);
    printf("your username is %s \n", username);
    while(x!=0)
       {
       printf("please choose an option from the following menu:\n\n");
       printf("1. Send a message\n");
       printf("2. Check userlist\n");
       printf("3. Log off Program\n");
       scanf("%d", &selection);
       switch(selection)
          {
          case 1 : printf("whats the message?\n");
                   if (fgets(buf, sizeof(buf), stdin) !=NULL)
    	          {
    	           printf("the message is as follows : %s\n", buf);
    		   if ((p = strchr(buf, '\n')) != NULL)
                       *p = '\0';
                       printf ("And now it's >%s<\n", buf);
    		   }
    	       break;
          case 2 : printf("the userlist is as follows \n");
                   break;
          case 3 : printf("now logging you off \n");
                   x=0;
    	       break;
          }
    
    
    
       }
    
    }

    The thing is the fgets wont allow me to enter anything. I have tried it without the if loop and with it but neither works.

    any ideas?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't mix scanf and fgets. scanf leaves newline characters in the input stream. fgets reads until a newline, which since there is one already in the stream, concludes its work.

    Also, if you had actually read the link I gave you in my first reply, your second and third posts wouldn't have been necessary.

    But hey, why bother reading when people are trying to help, right?

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

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    chill out man.

    I read the link you posted but I coudlnt get my head around it. At the moment I am totally stressed out and all rational though has gone out the window.

    so if I remove all the scanf`s and replace them with fgets it should work?

    Thanks for any help.

  13. #13
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    I don't see anything wrong with the code you have written.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  14. #14
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    Ive changed the first scanf to fgets so now the username is read ina nd taht works. THe problem now is to read in the integer for the menu. When I compile using gcc I get
    Code:
    warning: passing arg 1 of `fgets' makes pointer from integer without a cast
    I`m not sure what to make of this now

    the code I`m using for it is as follows

    Code:
     fgets(selection, 1, stdin);

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by velius
    I don't see anything wrong with the code you have written.
    It is valid code, it just doesn't do what he expects it to. As I've stated, his problem is that he's mixing scanf with fgets. Again as stated, scanf leaves the newline caracter in the input stream. Then, he calls fgets, which reads until it reaches the size limit, or a newline. Since there is a newline there already, it doesn't read anything except for the newline.

    Let me illustrate clearer
    Code:
    "please choose an option from the following menu:"
    
    "1. Send a message"
    "2. Check userlist"
    "3. Log off Program"
    At which points, it sits waiting for you to type...
    Code:
    1enter
    Scanf strips out the 1, leaving the newline.
    Code:
    fgets( ... )
    Since there is a newline already in the buffer, it terminates the fgets call, because that is what fgets is supposed to do. It reads until it reaches the newline. There is one there, so it does its job and only reads that.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 08:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM