confused on how the logic for this program

This is a discussion on confused on how the logic for this program within the C Programming forums, part of the General Programming Boards category; hey everyone i am new to the site and c programming i have to make a program that takes input ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    confused on how the logic for this program

    hey everyone i am new to the site and c programming i have to make a program that takes input from the user lets say they enter 322 and displays it back to the user as three two two i understand how to use the switch statement which i what my professor wants but the logic to get there is missing me some how any help would be greatly appropriated

    -confused in c

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The key to these kinds of problems is to find or set up, a relationship between the input on the one hand, and the output you want, on the other.

    Imagine you had some strings:

    Code:
    str0="zero"
    str1="one"
    str2="two"
    You see the relationship, there? If you haven't had arrays yet, this is what you can use. However, Instead of just having a bunch of strings all over, you could put them all together, in an array of strings (if you have been OK'd to use arrays):

    Code:
    str[10][6]= {{"zero"},{"one"}, {"two"}, ... etc. };
    You COULD put "str[0]="seven" or something of that ilk, but that's just confusing and best left for the obfuscation contests. So your switch statement will assign or print the right word, for each of the 10 digits (0-9).

    See what I mean?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    i get what you are saying there i guess where im having the issue is on the input itself i have it where i can break it down from 1-9 using a base ten scale which works if i am only counting up to nine once two digit numbers come into play i just cant figure out how to peel it back thanks for reading

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    One option is to read the input in one character at a time using getchar() or scanf() (whichever you've covered in your studies).

    Then for each character you read in, go through your switch/case and output the correct name string:
    Code:
    ...
    switch(ch)
    {
      case '3': printf("three "); break;
      case '4': ...
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    thanks for the help guys so i got it where it works but when i enter like 20 or 10 and number you can devide by 10 it only prints out the first number like two or one any ideas how to fix this thanks

    Code:
     
    #import <stdio.h>
    int main(void)
    {
            int a;
            int b=0;
            //do while loop allows feedback to user
            do
            {
                    printf("Please enter an integer:");
                    scanf("%i",&a);
            }while(a<0);
            // allows me to peel back the number
            do
            {
                    b *= 10;
                    b += a % 10;
                    a /= 10;
            }while (a > 0);
            do
            {
                    switch (b % 10)
            // switch statment allows me to change the value of b to the value of the case
                    {
                            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(" ");
                    b /= 10;
            }while (b > 0);
                    printf("\n");
    
    
            return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    See if this makes sense. I have not tried to compile or run this code.

    Code:
    #import <stdio.h>
    int main(void)
    {
            int a;
            int b=0;
    
            //do while loop allows feedback to user
            do
            {
                    printf("Please enter an integer:");
                    scanf("%i",&a);
            }while(a<0);
    
            // allows me to peel back the number
            do
            {
                           
                    b += a % 10;
                    switch (b % 10)
                    {
                            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;
                    }
    
                    a /= 10;
            }while (a > 0);
            printf("\n");
    
    
            return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    thanks for the quick reply looking it over the only thing i see is that it does allow for numbers like 10 or 20 but it reverses them so if i type in 10 it prints zero one

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    i have been working on this for a while now and i just cant find a way to make it work for number divisible by 10 any help would be great or how to make it not reverse the number

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of printing out words right away, let's put the numbers into a 1D array: n[6].

    Every element on n corresponds to a number, so n[1] has the value of the 1's column digit, from the number. if the 1's digit was a 3, then then n[1] equals 3.

    If the second digit in the number (counting from the right, same order we're getting by peeling off the digits), was 5, then n[2]==5.

    So 153 would be:
    n[1]=3, n[2]=5, n[3]=1.

    Now work through the n array index, by decrementing i:
    Code:
    for(i=6;i>0;i--) 
      if(n[i]) {
        //printf("%d", n[i]);  //run this line of code to show it works, prints out 531
        //have it work through your switch statement, right here
      }
    }
    Don't forget to set all elements of n[] to zero before making any number assignments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 01:55 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  3. What is the logic for this program
    By Shidlingayya in forum C Programming
    Replies: 4
    Last Post: 08-27-2007, 07:03 AM

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