This is for an extra credit assignment for class. Requirements I am having trouble with:

1. User enters number specifying numbers of integers
2. User enters integers
3. program will read them and parse.

----------------------------------------------------------
Here is a sample input and what the program should do:

How many numbers: 5
Enter numbers: 4 66 56 3 2

Here are the numbers:

4
66
56
3
2
----------------------------------------------------------

This program is for storing 2D matrices.

Here is what I have, but I can't get it to store my string of numbers. Without the part where I enter "how many numbers" and the for loop, it works (i.e. it gets the string). With the two additions, it skips to the end after I enter the number of numbers:

#include <stdio.h>

int main()
{
    int num;
    int i;
    
    char string[256];                               
    
    printf ("How many different numbers?:  ");
    scanf("%i", &num);
    
    printf( "Please enter a long string: " );
    
   
    fgets ( string, 256, stdin );
    for ( i = 0; i < 256; i++ )
{
    if ( string[i] == '\n' )
    {
        string[i] = '\0';
        break;
    }
}
               
    int counter = 0;
    char temp[256];
    int tempcount = 0;
    for (counter = 0;counter<256;counter++)
    {
        if (string[counter] != " ")
        {
           temp[tempcount] = string[counter]; 
           tempcount++;
        }
        
        else
        {
            printf("Here is the string, %s\n", temp);
            free(temp);
            tempcount = 0;
            
            
        }
        
        
        
    }

    return 0;
}

>>With the two additions, it skips to the end after I enter the number of numbers

Because scanf() leaves the Enter key '\n' in the keyboard buffer, and fgets() stops reading at the first '\n' it encounters. Call getchr() after scanf() to remove '\n' from the keyboard buffer.

I implemented your suggestion. This time, I can enter my data. However, it still skips to the end. Is there something wrong with the following code?

if (string[counter] != " ")
        {
           temp[tempcount] = string[counter]; 
           tempcount++;
        }

Here is the rest of it:

#include <stdio.h>

int main()
{
    int num;
    int i;
    
    char string[256];                               
    
    printf ("How many different numbers?:  ");
    scanf("%i", &num);
    getchar();
    printf( "Enter numbers: " );
       
    fgets ( string, 256, stdin );
              
    int counter = 0;
    char temp[256];
    int tempcount = 0;
    for (counter = 0;counter<256;counter++)
    {
        if (string[counter] != " ")
        {
           temp[tempcount] = string[counter]; 
           tempcount++;
        }
        else
        {
            printf("Here is the string, %s\n", temp);
            free(temp);
            tempcount = 0;
        }
   }
    
    system("PAUSE");
    return 0;
}

Instead of using fgets() , use a FOR loop to read num numbers using scanf("%d"...) .

I am wondering, will that work, since the numbers are on the same line.

Also, by extension, can I use the same technique in fscanf, because I've herad it reads only line by line.

BTW, I am going to try it.

This question has already been answered. Start a new discussion instead.