Thread: New to C programming

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    Smile New to C programming

    I need help in a program i build for C, which prints out the names of the numbers i input into the program. I did this by first splitting the digits, and put them in seperate registers in an array. i got lost in how i was going to convert the numbers to letters.

    Code:
                   #include <stdio.h>       
        void main() {
         int temp,  num, count, reg[4],remainder;
         
                 printf("Enter Number\n");
                  
                   scanf("%d",&num);
                   
                       temp = num;
                   count = 0;
    
                   while(temp !=0)
                   { temp = temp/ 10;
                       remainder = temp % 10;
                       ++count; 
                      if(count == 1)
                             reg[0] = remainder;
                       else if(count == 2)
                             reg[1] = remainder;
                         else 
                              reg[2] = remainder; }
                              
                    switch(reg[0])
                      {
                        case 1:  
                                 printf("one\n");
                                break;
                         case 2:
                                 printf("two\n");
                                 break;
                         case 0: 
                                  printf("error");
                                  break;
                         default: 
                                  printf("three");
                                  }
                              }

    the switch is to test my loop, but it kept saying error soo i couldn't figure out how come it keeps going to 0.


    Code:
    #include <stdio.h>
        void main()
        {
           int count, remainder, num, reg[2],one,two,three,four,five,six,seven,eight,nine,i;
           int word[11] = {one, two, three, four, five, six, seven, eight, nine }; 
                       
    
                 printf("what is your number?\n");
                        scanf("%d",&num);
                printf("how many digits, and yes this program is stupid\n"); // 2 digits only 4 now..
                        scanf("%d",&count);
                    
    	switch(count)
                        {
                           case  1 :
                              do
                              { remainder = num % 10;
                                reg[0] = remainder;
                                num = num / 10;
                               }while(num !=0);
                             
    
                                      break;
    
                       case 2 :
                               do
                               { remainder = num % 10;
                                 if(num >= 10)
                                    reg[1] = remainder;
                                  else
                                      if(num < 10){
                                          reg[0] = remainder; }
                                          num = num / 10;
    		} while(num !=0);
    
                                        break;
                                default:
                                         printf("Unavailable to determine\n");
                                          break;
                                      } 
                                for( i=count, i !=0; i = i - 1){ 
                                         switch(reg[0])
                                   count 1:    
                                          printf("%d",word[0]);
                                         break;
                                                  }
                            }

    my other program which i still can't figure out
    Last edited by Darkinyuasha1; 10-02-2006 at 02:43 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >              count = 0;
    
    >               while(temp !=0)
    >               { temp = temp/ 10;
    >                   remainder = temp % 10;
    >                   ++count; 
    >                  if(count == 1)
    >                         reg[0] = remainder;
    >                   else if(count == 2)
    >                         reg[1] = remainder;
    >                     else 
    >                          reg[2] = remainder; }
    Maybe:
    Code:
                  count = 0;
    
                   while(temp !=0)
                   {
                       remainder = temp % 10;
                       temp = temp/ 10;
                       reg[count] = remainder;
                       ++count; 
                   }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    thanks for that part of my code ^_^ ,but im still having a hard time making the values into words.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >im still having a hard time making the values into words.
    This question has popped up before. A forum search might do you some good.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int word[11] = {one, two, three, four, five, six, seven, eight, nine };
    Code:
           char *word[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    Make word a array of ten char pointers to the words as is shown above (versus the int array that you have). Then use a for-loop to loop thru the reg array printing the word equivalent of the number stored at each index, and you're done.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    gracias mucho, that all i needed to know ^_^

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > void main()
    Just one more thing. Always use:
    Code:
    int main(void)
    See this Faq entry for why: What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    i still get some errors ....
    Code:
      #include <stdio.h>
       int  main(void) {
         int temp,  num, count, i,reg[5],remainder;
         char *word[11] = {"one","two","three","four","five","six","seven","eight","nine"};
                 printf("Enter Number\n");
                  
                   scanf("%d",&num);
                   
                       temp = num;
                       count = 0;
    
                   while(temp !=0)
                   {
                       remainder = temp % 10;
                       temp = temp/ 10;
                       reg[count] = remainder;
                       ++count; 
                   }
                      for( i=1; i<=count; ++i) {      
                       
                       switch(i){
                            case 1:
                                    switch(reg[0]){
                   case 1:
                           printf("%s",word[0]);
                           break;
                   case 2:
                           printf("%s",word[1]);
                           break;
                   case 3:
                           printf("%s",word[2]);
                           break;
                           
                   case 4: 
                           printf("%s",word[3]);
                           break;
                   case 5:
                           printf("%s",word[4]);
                           break;
                   case 6: 
                           printf("%s",word[5]);
                           break;
                   case 7:
                           printf("%s",word[6]);
                           break;
                   case 8:
                           printf("%s",word[7]);
                           break;
                   default:
                           printf("%s",word[8]);
                           break;
                           }    
                           break;
                case 2 :
                         switch(reg[1]){
                   case 1:
                           printf("%s",word[0]);
                           break;
                   case 2:
                           printf("%s",word[1]);
                           break;
                   case 3:
                           printf("%s",word[2]);
                           break;
                           
                   case 4: 
                           printf("%s",word[3]);
                           break;
                   case 5:
                           printf("%s",word[4]);
                           break;
                   case 6: 
                           printf("%s",word[5]);
                           break;
                   case 7:
                           printf("%s",word[6]);
                           break;
                   case 8:
                           printf("%s",word[7]);
                           break;
                   default:
                           printf("%s",word[8]);
                           break;}  
                            break;
                case 3:
                           switch(reg[2]){
                   case 1:
                           printf("%s",word[0]);
                           break;
                   case 2:
                           printf("%s",word[1]);
                           break;
                   case 3:
                           printf("%s",word[2]);
                           break;
                           
                   case 4: 
                           printf("%s",word[3]);
                           break;
                   case 5:
                           printf("%s",word[4]);
                           break;
                   case 6: 
                           printf("%s",word[5]);
                           break;
                   case 7:
                           printf("%s",word[6]);
                           break;
                   case 8:
                           printf("%s",word[7]);
                           break;
                   default:
                           printf("%s",word[8]);
                           break;}  
                            break;
                 case 4: 
                            switch(reg[3]){
                   case 1:
                           printf("%s",word[0]);
                           break;
                   case 2:
                           printf("%s",word[1]);
                           break;
                   case 3:
                           printf("%s",word[2]);
                           break;
                           
                   case 4: 
                           printf("%s",word[3]);
                           break;
                   case 5:
                           printf("%s",word[4]);
                           break;
                   case 6: 
                           printf("%s",word[5]);
                           break;
                   case 7:
                           printf("%s",word[6]);
                           break;
                   case 8:
                           printf("%s",word[7]);
                           break;
                   default:
                           printf("%s",word[8]);
                           break;}       
                               break;
                   default:
                            print("i do not know");
                               break;
                               }
                        }
                    }

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > while(temp !=0)
    I didn't say this earlier, but I'd put an extra check here to be sure you don't overrun the reg array:
    Code:
                   while(temp !=0 && count < 5)
    > for( i=1; i<=count; ++i) {
    Arrays are indexed starting at 0. So
    Code:
                      for( i=0; i<count; ++i) {
    Code:
    >                   switch(i){
    >                        case 1:
    >                                switch(reg[0]){
    You only need one switch-case, not two. Switch on the value of reg[i].

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Don't even use a swich.

    Do this:

    Code:
    for( i=0; i<count; ++i)
         printf("%s",word[ reg[i] ]);
    Assuming count remains less than 5.
    Last edited by King Mir; 10-02-2006 at 10:05 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Some effort on the indentation consistency wouldn't go amiss either.

    Do NOT mix spaces and tabs when you post your code to message boards. Your fancy code editor might be able to cope, but message boards usually make a mess of the result.
    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.

  12. #12
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    This may sound like a stupid question, but do you have to input and store the number as an integer? It would make you program so much easier if you could inout the number and store it in a character array. For one thing, you'd know how many digits there were in the number.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Quote Originally Posted by SKeane
    This may sound like a stupid question, but do you have to input and store the number as an integer? It would make you program so much easier if you could inout the number and store it in a character array. For one thing, you'd know how many digits there were in the number.
    explain that a little better to me, and i think the answer is yea they got to be inputed.

    this is the program i have now soo far i modify
    Code:
    #include <stdio.h>
       int  main(void) {
         int temp,  num, count, i,reg[5],remainder;
         char *word[11] = {"one","two","three","four","five","six","seven","eight","nine"};
                 printf("Enter Number\n");
                  
                   scanf("%d",&num); // input number
                   
                       temp = num;
                       count = 0;
    
             while(temp !=0 && count < 5)  //divides number and puts it in a special container
                   {
                       remainder = temp % 10;//which prints the numbers that were broken
                       temp = temp/ 10;      //up into single digits to be printed into words
     
                       reg[count] = remainder; // base on reg
                       ++count; 
                   }
                    
                for( i=0; i<count; ++i)
         printf("%s",word[ reg[i] ]);  
         }

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char *word[11] = {"one",
    You need a "zero" in there, don't you think?

    I'll leave it to you to figure out how to print the output in the correct order.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    okey and yea i gotta fix that

Popular pages Recent additions subscribe to a feed