Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Loop problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Loop problems



I'm a beginning programmer, and I'm working on a problem in Programming in Objective-C, by Stephen G. Kochan from Chapter 6, number 6, and the code for my current solution is below, which isn't satisfactory, because it leaves me with this output (I have a bunch of extra output that I have left in from intermediary stages of development for debugging) :

BRUNOISE.local_vieuxnez_/Users/vieuxnez/Documents/Programming/ Objective-C/Programming in Objective-C by Stephen G. Kochan_05/12/05_0:24_762_Chapter\ 6/Exercises/Ex6/Digit\ to\ Number\ Converter
Enter your number.
1234
12
Number length is 4
1234 1234
1
10 2
100 3
1000 4
Number's place value is 1000
one
two
three
four


The output should be simply:

Enter your number.
1234
one two three four

If you look at the code, though, there's a small part at the end of the loop.

<CODE BIT>
           printf ( "\n" ) ;
</CODE BIT>

That for some reason, the loop won't run without, meaning, if I change it to

<CODE BIT>
           printf ( " " ) ;
</CODE BIT>

Like I want, the loop doesn't run. I should also mention that for some reason, after I added these commented out debugging statements, the loop started running as well, but not correctly. For some reason the last digit would never get printed out, and I could never single out which of the statements was enabling the loop. To be honest, I was shooting in the dark when I put the statements in there, since at the time, nothing seemed to make the loop work, then for some reason, the combination of adding all those debugging printf statements made the loop work, though the last digit wouldn't be printed with just them.)

<CODE CHUNK>
       remainder = number % place_value ;
       //printf ( "remainder %i ", remainder ) ;
       digit = number / place_value ;
       //printf ( "digit %i ", digit ) ;
       number = remainder ;
       //printf ( "number %i ", number ) ;
       place_value /= 10 ;
       //printf ( "place_value %i ", place_value ) ;
       //printf ( " " ) ;
</CODE CHUNK>


<CODE>

// Program to reverse the digits of a number

#import <stdio.h>

int main (int argc, char * argv[])
{
char neg_flag ;
int digit, remainder, number, number_swap, number2, left_digit, right_digit, place, place_count, place_value, i1, i2 ;


  place = 1 ;
  place_count = 0 ;
  place_value = 1 ;

  neg_flag = ' ' ;

  printf ("Enter your number.\n") ;
  scanf ("%i", &number) ;

  printf ( "%i\n", number / 100 ) ;

  number_swap = number ;

  if ( number < 0 )
    {
      neg_flag = '-' ;
      number *= -1 ;
    }

   do
     {
       number /= 10 ;
       place_count += 1 ;
     }
   while ( number != 0 ) ;

   printf ( "Number length is %i\n", place_count ) ;

   number = number_swap ;

   printf ( "%i %i\n", number, number_swap ) ;

   printf ( "%i\n", place_value ) ;

   for ( i1 = 2 ; i1 <= place_count ; i1++ )
     {
       place_value *= 10 ;
       printf ( "%i %i\n", place_value, i1 ) ;
     }

   printf ( "Number's place value is %i\n", place_value ) ;

   //   remainder = number % place_value ;
   //   printf ( "%i\n" , remainder ) ;
   //   digit = number / place_value ;
   //   printf ( "%i\n" , digit ) ;

   do
     {
       remainder = number % place_value ;
       //printf ( "remainder %i ", remainder ) ;
       digit = number / place_value ;
       //printf ( "digit %i ", digit ) ;
       number = remainder ;
       //printf ( "number %i ", number ) ;
       place_value /= 10 ;
       //printf ( "place_value %i ", place_value ) ;
       //printf ( " " ) ;
         {
           switch ( digit )
         {
         case 1 :
           printf ( "one " ) ;
           break ;
         case 2 :
           printf ( "two " ) ;
           break ;
         case 3 :
           printf ( "three " ) ;
           break ;
         case 4 :
           printf ( "four " ) ;
           break ;
         case 5 :
           printf ( "five " ) ;
           break ;
         case 6 :
           printf ( "six " ) ;
           break ;
         case 7 :
           printf ( "seven " ) ;
           break ;
         case 8 :
           printf ( "eight " ) ;
           break ;
         case 9 :
           printf ( "nine " ) ;
           break ;
         case 0 :
           printf ( "zero " ) ;
           break ;
         }
           printf ( "\n" ) ;
         }
     } while ( remainder > 0 ) ;



  return 0;
}


</CODE>


Dustin Kick

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.