954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading varied numbers of integers separated by spaces

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;
}
asuprem
Newbie Poster
20 posts since Dec 2011
Reputation Points: 10
Solved Threads: 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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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;
}
asuprem
Newbie Poster
20 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

asuprem
Newbie Poster
20 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Thanks WaltP. It worked!

asuprem
Newbie Poster
20 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: