Plzz help problem with strings

This is a discussion on Plzz help problem with strings within the C Programming forums, part of the General Programming Boards category; Note MAX is #define MAX 100 What im trying to do is reading the user's input and placing them into ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Unhappy Plzz help problem with strings

    Note MAX is #define MAX 100
    What im trying to do is reading the user's input and placing them into the string( the same string).
    The input is ended by doing the End of File(EOF) Which DOS is ctrl+z.

    Yet when i run the program and print the string.
    It doesnt print the first input i typed.
    Also is this the proper way to read input into string?
    Example:
    Blah
    power
    X
    ^z
    power
    x

    Code:
    int main()
    {
    
    int i = 1,
    	valid;
    char c,
    	input[MAX];
    	
    	c = getchar();
    	input[MAX] = c;
    	
    	for (  ; (c = getchar()) != EOF; i++){
    		input[i] = c;
    }
    
    	input[i] = '\0';
    
    	printf("%s", input);
    return 0;
    }
    And hints info whatever will help

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    10
    are you inputting the words from a file or the keyboard?

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    Keyboard (i think )
    Yeah i just type the words in and press enter
    I dont know the difference : /

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    int main(void)
    {
    
        int i = 1, /* in C, arrays are indexed from 0 */
        valid;
        char c, 
        input[MAX];
    
        c = getchar(); /* getchar returns an int */
        input[MAX] = c; /* off the end of the array */
    
        for ( ; (c = getchar()) != EOF; i++ ) /* EOF is not a char */
        {
            input[i] = c;
        }
    
        input[i] = '\0';
    
        printf("%s", input);
        return 0;
    }
    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.*

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    What other way can i end this loop with EOF
    I have to read the number of words until i end it with that command : /

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    There was an example in the EOF FAQ.
    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    int main(void)
    {
       int i;
       char input[MAX];
       for (i = 0; i < MAX - 1 /* save room for the '\0' */; i++ )
       {
          int c = getchar();
          if(c == EOF)
          {
             break;
          }
          input[i] = (char)c;
       }
       input[i] = '\0';
       printf("You entered:\n\"%s\"\n", input);
       return 0;
    }
    
    /* my input/output
    C:\test>test
    Blah
    power
    X
    ^Z
    You entered:
    "Blah
    power
    X
    "
    */
    Last edited by Dave_Sinkula; 04-15-2003 at 03:13 PM.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM

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