Quick question

This is a discussion on Quick question within the C Programming forums, part of the General Programming Boards category; I had a quick question about this part of the code Code: for (i=0; i < MAXLEN && i!= EOF; ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Quick question

    I had a quick question about this part of the code

    Code:
    for (i=0; i < MAXLEN &&  i!= EOF; i++)
        {
            printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */
            number = getchar() - '0';                /* sets the string to a int */
            
                if (number >= '0' && number <= '9') /* looks for a number '0' - '9' */
                {
                    --i;
                }
                else                                /* gives a error message if not the write parameters */
                {
                    printf("invalid number\n");
                }            
                getchar();
                array[i] = number;                    /* puts the number in the array */
        }
    why will it not do the if/else function correctly?!

    also I do not understand the point to state getchar() again after the if/else statement??

    here is the whole code just for reference

    Code:
    #include <stdio.h>
    
    #define MAXLEN 5    /* sets the length of the array */
    
    main()
    {
        int array[MAXLEN];   /* places the integeres in the array */
        int i, sum, number;
    
        for (i=0; i < MAXLEN &&  i!= EOF; i++)
        {
            printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */
            number = getchar() - '0';                /* sets the string to a int */
            
                if (number >= '0' && number <= '9') /* looks for a number '0' - '9' */
                {
                    --i;
                }
                else                                /* gives a error message if not the write parameters */
                {
                    printf("invalid number\n");
                }            
                getchar();
                array[i] = number;                    /* puts the number in the array */
        }
    
            
        for (i=0; i<MAXLEN && i!= EOF; i++)
        {    sum += array[i]; /* will count the sum and store in the array */
                printf("%d  ", array[i]); /* prints out the numbers til it gets to count 5 */
        }
    
        printf("= %d\n", sum); /* will print the sum */
    }
    
    /*  expected output:  
    Enter numbers between 0-9 
    1
    Enter numbers between 0-9 
    2
    Enter numbers between 0-9 
    5
    Enter numbers between 0-9 
    6
    Enter numbers between 0-9 
    3
    1  2  5  6  3  = 17 */
    Last edited by zach48191; 10-19-2011 at 10:55 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In the first code sample ... comment out line 4 and try it again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    It just made it go to invalid number first and every number after that it would not take and the output was 0 0 0 0 0 = 0

    I can get it to work correctly without the else statement, but I want my program to be more idiot proof i guess and also the getchar() after the loop why is that needed?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Why are questions always quick?

    number = getchar() - '0'; /* sets the string to a int */
    if (number >= '0' && number <= '9') /* looks for a number '0' - '9' */


    You already did the -'0', so you should be testing for 0 to 9, not '0' to '9'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    what do you mean by 0 to 9 and not '0' to '9'?? I thought that ' ' around a character defines it?

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by zach48191 View Post
    what do you mean by 0 to 9 and not '0' to '9'?? I thought that ' ' around a character defines it?
    getchar() - '0' reduces the ASCII value for the characters of 0 - 9 (48 - 57) to the actual values. '0', wrapped in single quotes, is the ASCII value for the character of '0', which is 48. 0 is zero. Since you already converted the ASCII value to the actual value with getchar() - '0', your if statement should be comparing integers, not characters.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Thanks for clearing that up makes perfect sense now... I cant believe it still worked even with them. It works great now and I added continue in the IF loop so that it will keep going. I took out the else statement, because it was too much trouble to get it to work correctly and if a user put in a 10 it would only count the 1. Was running into that problem so here is the working code, btw what would be the fix if they entered a 10, I tried to use a

    Code:
     
    if (number < 0 || number > 9 || number == (double)number);
    but it sadly did not work. Anyways here is my code it works now thought I might post it for others that might have trouble in the future

    Code:
    #include <stdio.h>
    
    #define MAXLEN 5    /* sets the length of the array */
    
    void getArray (int s[], int maxlen);
    void sumArray (int s[], int maxlen);
    
    main()
    {
        int array[MAXLEN];   /* places the integeres in the array */
        int array2[MAXLEN];
    
        getArray (array, MAXLEN);    /* fills the array with the function getArray */
        getArray (array2, MAXLEN);
    
        sumArray (array, MAXLEN);    /* sums and prints with the function sumArray */ 
        sumArray (array2, MAXLEN);
    }
    
    void getArray ( int s[], int maxlen )
    {
        int i, number;
    
        for (i=0; i < MAXLEN &&  i!= EOF; i++)
        {
            printf("Enter numbers between 0-9 \n"); /* Asks the users for a number 0-9 */
            number = getchar() - '0';                /* sets the string to a int */
            
                if (number < 0 || number > 9) /* looks to check to see if the number is 0 - 9 */
                {
                    --i;
                    continue;
                }
    
                
                getchar();
                s[i] = number;                    /* puts the number in the array */
        }
    }
    
    void sumArray ( int s[], int maxlen )
    {
        int i, sum;
    
        for (i=0; i<MAXLEN && i!= EOF; i++)
        {    sum += s[i]; /* will count the sum and store in the array */
                printf("%d  ", s[i]); /* prints out the numbers til it gets to count 5 */
        }
    
        printf("= %d\n", sum); /* will print the sum */
    }
    
    /*  expected output:  
    Enter numbers between 0-9 
    1
    Enter numbers between 0-9 
    2
    Enter numbers between 0-9 
    3
    Enter numbers between 0-9 
    4
    Enter numbers between 0-9 
    5
    Enter numbers between 0-9 
    6
    Enter numbers between 0-9 
    7
    Enter numbers between 0-9 
    8
    Enter numbers between 0-9 
    9
    Enter numbers between 0-9 
    0
    1  2  3  4  5  = 15
    6  7  8  9  0  = 45 */
    Last edited by zach48191; 10-20-2011 at 09:58 PM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Well at the moment, you're just reading single digits in isolation, and ignoring everything else.

    So if you type in say
    123
    or
    1 2 3

    You read the '1' and then what?
    If the next character is a '2', then you've got a compound string which you need to read.

    But if it's a space, then the '1' you read is just that, a single digit number.


    You should also remove all the
    && i!= EOF
    tests from your code, since they don't actually do anything useful.

    The only place you could test EOF is like so.
    int ch = getchar();
    if ( ch == EOF ) break;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question
    By SenorJesucristo in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 09:06 PM
  2. Quick Question
    By strokin_442 in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2005, 10:22 PM
  3. Just a Quick Question
    By Krak in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2005, 06:36 PM
  4. Quick question?
    By gvector1 in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2003, 03:38 PM
  5. A quick question(s)
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2003, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21