Reading Separate Digits as Variables

This is a discussion on Reading Separate Digits as Variables within the C Programming forums, part of the General Programming Boards category; Hello, I am currently taking an intro course to C programming, and have a few questions about an assignment I'm ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Reading Separate Digits as Variables

    Hello, I am currently taking an intro course to C programming, and have a few questions about an assignment I'm working on. I've Googled for this, and searched these forums, but have not been able to find anything to help thus far.

    The program I have to write has to accept a five digit integer as input, print out each digit with three spaces between them, and then print the largest and smallest digit. After searching, I was able to read the digits and print them like this:

    Code:
    int c1;
    int c2;
    int c3;
    int c4;
    int c5;
    
    printf( "Input a five-digit integer:\n" );
    scanf( "%c%c%c%c%c", &c1, &c2, &c3, &c4, &c5 );
    
    printf( "\nThe separated digits are:\n" );
    printf( "%c   %c   %c   %c   %c\n", c1, c2, c3, c4, c5 );
    This works fine, except that I next need to use Boolean logic to find the greatest and least digit. I played with a few different ways to get the smallest and largest digits, but none of them seemed to work (ie, they'd report the wrong number as being the largest). Finally, I tried to debug things by just using one if statement to compare the first two integers, and assign the value of the greater one to the variable greatest (which has its own int greatest;, which is not shown in the listing above).

    Code:
    if ( c1 > c2 ) { 
    greatest = c1;
    }
    else greatest = c2;
    
    printf( "Greatest: %c\n", greatest )
    The code above did not give any compile errors (with gcc on a Linux system, if it matters). However, no matter if c1 > c2 is true or not, the variable greatest always gets assigned the value of c1. So, if c2 is greater, greatest still gets the value of c1.

    After looking at it, I realized that it might have something to do with using %c rather than %d; ie, it was looking for ASCII rather than integers. (I think; correct me if I'm wrong.) So, I'm thinking this could be why the conditional wasn't working. I'm reading up on the atoi function. I attempted it as follows, first creating a new set of variables set up as character strings, for instance:

    Code:
    char c1;   /*I used one for each of the c variables respectively.*/
    int d1;     /*I used this for the integer values.*/
    
    d1 = atoi( c1);
    I then used the d variables (d1, d2, etc.) in the conditionals, and compiled. However, I got compilation errors:

    Code:
    digits.c:41: warning: passing arg 1 of `atoi' makes pointer from integer without  a cast
    I had one of those for each of the five atoi functions I used.

    Any thoughts on this? I have a few more days for this program, and so will continue to mess with it. Thanks for any insight!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by linuxpyro
    Any thoughts on this? I have a few more days for this program, and so will continue to mess with it. Thanks for any insight!
    atoi, the worst of the text-to-number conversion functions, expects a null-terminated string -- which a single character can never be. Read as a string and (attempt to) convert each character to a digit using cn - '0'.

    And perhaps post a small but complete snippet that demonstrates the issue.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Use loop for input five integers, and store them in integer array.
    Then, again loop with printf to display them with spaces. In the same loop you can find min and max.
    Last edited by Micko; 09-12-2006 at 02:37 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    And logic like this may make more sense:
    Code:
                int digit = text[i] - '0';
                if ( digit > hi )
                {
                   hi = digit;
                }
                if ( digit < lo )
                {
                   lo = digit;
                }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    Quote Originally Posted by Dave_Sinkula
    atoi, the worst of the text-to-number conversion functions, expects a null-terminated string -- which a single character can never be. Read as a string and (attempt to) convert each character to a digit using cn - '0'.

    And perhaps post a small but complete snippet that demonstrates the issue.
    Could you just show me an example of this? I tried the following, but got errors when compiling:

    Code:
    int d1 = cn[c1] - '0';
    I'm sorry; once again I am new to C, and I couldn't find anything on that by searching.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you actually have a variable called cn? It helps if you actually post what your error is.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    This is the error I was getting:

    Code:
    digitsold.c:36: error: `cn' undeclared (first use in this function)
    digitsold.c:36: error: (Each undeclared identifier is reported only once
    digitsold.c:36: error: for each function it appears in.)
    I thought cn was a function; I was trying to convert the character to an integer.

    Anyway, I talked to my professor, and after mentioning just reading each digit, he said the he would rather I used division. So, I have the program done and working fine. Thanks for the help thus far. I still wouldn't mind setting up the program my way for practice, though, and will post here when I get it worked out.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by linuxpyro
    Could you just show me an example of this? I tried the following, but got errors when compiling:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void)
    {
       char source[] = "hello world";
       char destination[sizeof source / sizeof source];
       int hi = INT_MIN, lo = INT_MAX;
       char text[10];
       fputs("Input a five-digit integer: ", stdout);
       fflush(stdout);
       if ( fgets(text, sizeof text, stdin) != NULL )
       {
          int i;
          for ( i = 0; text[i] != '\0'; ++i )
          {
             if ( isdigit(text[i]) )
             {
                int digit = text[i] - '0';
                if ( digit > hi )
                {
                   hi = digit;
                }
                if ( digit < lo )
                {
                   lo = digit;
                }
             }
          }
       }
       printf("hi = %d, lo = %d\n", hi, lo);
       return 0;
    }
    
    /* my output
    Input a five-digit integer: 15243
    hi = 5, lo = 1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  4. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 12:49 PM
  5. reading hexadecimal variables
    By juhigarg in forum C++ Programming
    Replies: 7
    Last Post: 11-06-2001, 10:10 PM

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