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;
}

Dani AI

Generated

Good progress — nailed the original problem (the leftover '\n' after scanf) and ’s suggestion to read num integers with scanf is the simplest fix for input like 4 66 56 3 2. The remaining bugs in the posted code that caused “skipping” and weird output are:

  • Comparing a char to a string literal (" ") — use the character literal ' ' instead.
  • Calling free(temp) on a local array — free is for heap memory from malloc.
  • Not terminating temp with '\0' before using printf("%s", temp).
  • Looping a fixed 256 times instead of stopping at the string NUL ('\0') or when you’ve seen num tokens.

If you just want the numbers printed, this is the straightforward, robust approach using scanf in a loop:

#include <stdio.h>

int main(void) {
    int num, i, x;
    if (scanf("%d", &num) != 1) return 1;
    for (i = 0; i < num; ++i) {
        if (scanf("%d", &x) != 1) { fprintf(stderr, "bad input\n"); return 1; }
        printf("%d\n", x);
    }
    return 0;
}

If you need to read one whole line and then parse it (more control, better error handling), use fgets + strtok + strtol:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char buf[256];
    int n, count = 0;
    if (scanf("%d", &n) != 1) return 1;
    getchar(); /* consume newline */
    if (!fgets(buf, sizeof buf, stdin)) return 1;
    char *tok = strtok(buf, " \t\n");
    while (tok && count < n) {
        long v = strtol(tok, NULL, 10);
        printf("%ld\n", v);
        ++count;
        tok = strtok(NULL, " \t\n");
    }
    return 0;
}

Notes: check strtol’s end pointer for invalid tokens if needed, store values in a malloc'd array if you must keep them, and avoid system("PAUSE") for portability.

Recommended Answers

All 5 Replies

>>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.

Thanks WaltP. It worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.