Thread: C newbie needing help.

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    C newbie needing help.

    Hi, I'm new to C, and I'm trying to write a program in which I need the user to input a series of numbers separated by spaces. So far I've been able to read this input by using scanf() an array and a for loop, however, I can't find a decent way to end the input... The only way I found so far is to end the string of ints with a 0 then press enter, but it would be nice if I didn't have to put that 0. Any advice on how can I achieve this? Thank you!

    Code is something like this:

    Code:
    #include <stdio.h>
    
    int main () {
    
        int x;
        int numbers[50];
    
        for ( x = 0; x < sizeof(numbers) ; x++ ) {
    
            scanf ( "%d", &numbers[x] );
    
            if ( numbers[x] == 0 ) {
                break;
            }
        }
    
        printf ( "\n" );
    
        for ( x = 0; numbers[x] != 0; x++ ) {
            printf ( "%d", numbers[x] );
        }
        return 0;
    }
    Last edited by Enzo; 12-26-2009 at 06:26 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    scanf() returns a value indicating the number of items SUCCESSFULLY read. When this is less than 1, you're finished.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Code:
    for ( x = 0; x < sizeof(numbers) ; x++ )
    Are you sure that the size allocated at compile time and sizeof(numbers) same?
    Check for yourself by adding a printf statement

    Code:
    .
    .
    printf("Size of my array : %d", sizeof(numbers));
    	for ( x = 0; x < sizeof(numbers) ; x++ ) {
    .
    .
    Quote Originally Posted by Enzo
    ..but it would be nice if I didn't have to put that 0.
    Tell us how exactly would you want to terminate the input.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    Quote Originally Posted by zalezog View Post
    Are you sure that the size allocated at compile time and sizeof(numbers) same?
    Check for yourself by adding a printf statement
    You made me notice something quite surprising to me there. I didn't know it could be different "at compile time" and I'm really not sure why does it happen. I used the printf and it said sizeof(numbers) = 200, when it should have been 50.

    About the number input, I wanted to be able to finish when pressing enter. I managed to do this by using the functions "strtol", "fgets" and doing some other stuff a bit more complicated. However, I'm still confused about what MK57 suggested me to do in regards with the scanf function. Because when I wait until scanf < or != 1, it doesn't happen. It just keeps inputing characters. When does scanf not read a character sucessfully? How can I make the input stop, when hitting enter with the scanf funtion, if it just keep inputing? Also why is sizeof(numbers) different at compile time?

    Thank you for you time and for answering my questions! Your help is greatly appreciated!

    Code:
      for ( x = 0; scanf("%d",&numbers[x]) == 1 ; x++ )
            ;
        printf ( "\n" );
    In case my code is wrong, I found this one in wikipedia.org, which gives me the same results

    Code:
    int n;
        while (scanf("%d", &n) == 1){
            printf ("%d\n", n);
    thank you again!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The problem with scanf() is that's it's basically mindless. If you put in %d for the format specifier, it will happily wait for that number - you can press enter until the cows come home, basically.

    There are probably ways around that problem with scanf() specifiers, but scanf() just doesn't strike me as being good for flexible user input.

    I would do it something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
        char buff[20];
        int n, x = 0;
        int numbers[10];
    
        do {
          fgets(buff, sizeof(buff), stdin);
          numbers[x++] = atoi(buff);
        }while(buff[0] != '\n'); 
          
        --x;  
        printf ( "\n You entered %d numbers", x );
    
        for ( n = 0; n < x; n++ ) {
            printf ( "\n%d", numbers[n] );
        }
    
        printf("\n\n\t\t\t     press enter when ready");
        x = getchar();
    
        return 0;
    }
    Try that and see what you think.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    Pune, India
    Posts
    4
    Hello !
    Try this technique. It works and fulfills your requirement

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main () {

    int x = 0 ;
    int cnt;
    int numbers[50];
    char str[100];
    char *ptr ;

    // fgets(str,100,stdin);
    // or
    gets(str);
    ptr = strtok (str," ");
    while( ptr != NULL )
    {
    numbers[x++] = atoi(ptr);
    ptr = strtok(NULL," ") ;
    }
    printf ( "\n" );

    for ( cnt = 0; cnt < x; cnt++ ) {
    printf ( "\t%d", numbers[cnt] );
    }
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needing simple help
    By ggilmore in forum C Programming
    Replies: 2
    Last Post: 10-29-2009, 03:44 PM
  2. newbie needing help.
    By nacho4d in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-04-2009, 12:32 PM
  3. Newbie needing help with basic code!
    By TeZ258 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 08:35 PM
  4. Newbie to C++ needing help
    By Tazwoh in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2003, 12:50 PM
  5. Newbie needing Help!!
    By black blade in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-26-2002, 08:37 AM