Thread: scanf

  1. #1
    Unregistered
    Guest

    Question scanf

    i want to use scan f týo get such a lýne as ýnput and store ýt ýn an array.what should ý do?


    ex:100 12 23 43 53 64

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    16
    char array[10];
    scanf("%s",&array);

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use fgets(), not scanf(). It's safer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest
    char array[10];
    scanf("%s",&array);

    this does not work.afret i had done this i printed the all arrays(array[1], array[2],......)


    but it printed 0.0000 0.0000
    by the way i want to store floating points in the arrays.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you really must use scanf(), try something like this:
    Code:
    #include <stdio.h>
    
    #define MAX 10
    
    int main(void)
    {
    	float tmp, arr[MAX];
    	int i, j;
    	
    	i = 0;
    	while (scanf("%f", &tmp) == 1)
    	{
    		arr[i] = tmp;
    		i++;
    		if (i == MAX)
    			break;
    	}
    	
    	for (j = 0; j < i; j++)
    		printf("Element %i is %f\n", j, arr[j]);
    	
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    [code]
    #include<stdio.h>

    int main()
    {

    int num[10];

    for (int x=0; x<10;x++)
    {
    printf("Enter number :\t",x);
    scanf("%d",&num[x]);
    }
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by dayknight
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int num[10];
    
    for (int x=0; x<10;x++)
     {
      printf("Enter number :\t",x);
      scanf("%d",&num[x]);
     }
    }
    Inline declaration of variables is a C++ thing, so you can't do this:
    >for (int x=0; x<10;x++)

    What happens if the user enters a letter by mistake? This code does no validation.

    >printf("Enter number :\t",x);
    The x is not needed in this statement.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM