Thread: input in C?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    input in C?

    Is there something wrong with my code?
    I am quite confused about inputing a string and a character...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
    	int c[21];
    	
    	while (scanf("%c\n",c) != EOF) {
    
    		printf("%s\n",c);
    	}
            
           return 0;
    
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, you are scanf-ing a character in variable of type character array.

    You need to do something like scanf("%c",c[i]); where i is some valid index.

    try this:

    Code:
    int i = 0;
    
    while(scanf("%c\n",c[i++] != EOF){
       printf("%s\n",c);
    }

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    You definitely don't need to be EOF checking when doing stdin.

    If you're trying to populate an integer array, then you'll be using %d, not %c.

    Code:
    for ( i = 0; i < 21; ++i )
    {
          scanf ( "%d", &c[i] );
    }

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    sorry, the code I posted was wrong...
    It should be

    Code:
    int main () {
    	char c[21];
    	
    	while (scanf("%c\n",c) != EOF) {
    
    		printf("%c\n",c);
    	}
    
    
    }
    i got a error message..
    warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Quote Originally Posted by Dr Saucie View Post
    You definitely don't need to be EOF checking when doing stdin.

    If you're trying to populate an integer array, then you'll be using %d, not %c.

    Code:
    for ( i = 0; i < 21; ++i )
    {
          scanf ( "%d", &c[i] );
    }
    But I need to know when the user has done inputting....
    if i dont do EOF checking, is there another way to know the user has finished inputting?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dr Saucie View Post
    You definitely don't need to be EOF checking when doing stdin.
    Well you can actually enter EOF via stdin. I'm not sure why none of you mentioned %s instead of %c though.


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

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    Code:
    int main () {
            char c[21];
            int  i=0;
            while (scanf("%c",&c[i])!=EOF) {
                    printf("%c",c[i++]);
            }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating input to an array
    By ij.cohen in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 10:48 AM
  2. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM