-5

So i need to have my switch statement go through and write out five two if the user says 52 but i cannot get it pass my 0-9. if they type 0-9 it works perfect but if i try to do any number past that it makes a blank. help!

#include <stdio.h>

int main (void)
{

        int x;

        printf("Please enter an integer: ");
        scanf("%d", &x);

        printf("\nYou have entered:\n\n");

        for(x;x<0;x++);

        switch (x)

        {
        case 0:
                printf("zero");
                break;
        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;
        }

        printf("\n\n");



        return 0;
}
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • Homework, right? You might want to try writing out in words what you need to do and figure out how to take those words and turn them into code. – plinth Nov 07 '13 at 18:14
  • 6
    The line `for(x;x<0;x++);` probably doesn't do what you think it does. – Adam Liss Nov 07 '13 at 18:15
  • (Hints rather than code since this is homework) You could convert `x` to a string using `sprintf`, then loop over the characters in the string, using your switch statement for each one. – simonc Nov 07 '13 at 18:15
  • i have not learned sprintf so i won't use it. i'm trying to think around the ways we have learned but its not easy. – user2963635 Nov 07 '13 at 18:26

7 Answers7

3
do {
  switch (x%10) {
    ...
  }
  x = x / 10;
} while (x>0) ;

or to get it in the right order use recursion

void f(int x) {
  if (x==0) return;
  f(x/10);
  switch(x%10) { ... }
}
Sorin
  • 11,863
  • 22
  • 26
0

This question has been asked-and-answered before.

The for-loop you wrote is empty, because the body of the loop ends with the semi-colon:

for(x;x<0;x++) /* Empty Body!!*/ ;

The way a typical for loop works is:

for( /*Initialize*/;  /*Test*/;  /*Change*/)
{
    /* Body */
}

In your case, I think you want:

for(int i=0;  i < x; ++i)
{
    switch(x)
    {
         [...]
    }
}

This will:

  • Initialize i to 0
  • Test if i is LESS THAN x (the number you entered)
  • Keep increasing i by 1, until it gets up to x.
Community
  • 1
  • 1
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • the for loop i put in there is just something i tried to get it to work. i thought that when i put in like 15 if will find one but i need to loop it so it also picks out the five. thats where i'm running into my problems. keep in mind i'm a very base level student and i haven't learned a lot of things that would make this easier. – user2963635 Nov 07 '13 at 18:19
  • You can see my answer to a similar question at: http://stackoverflow.com/questions/16967008/how-to-get-different-digits-starting-from-the-most-significant-digit-in-a-number – abelenky Nov 07 '13 at 18:23
0

Here's what you need to do. Similar to what @simonc said, it's a good idea to convert the user input to a string, then loop through the characters. For each character, convert it into an int and then take that into the switch statement.

EDIT---------------------------

Here's a way with strictly using integers.

First find how many digits your integer has. You can do this by a method mentioned here.

Then do integer division starting from the largest power of 10 that divides the integer you have and divide the divisor by 10 until you reach 1. For example:

If user input is 213, it has 3 digits. We divide this by 100 first. 213/100 = 2

We take the 2 and put it into the switch statement, outputting 2. Now we're done with the hundreds digit, so now we take the 13 from 213 and divide it by 10.

13/10 = 1 So now we output one.

Keep doing this process until you get to the ones digit.

Does this make sense?

Community
  • 1
  • 1
masotann
  • 901
  • 10
  • 29
0

You could solve this with recursion.

void printDigit(int x) {

  int digit = x%10;
  if(digit!=x)
    printDigit(x/10);
  switch(digit) {
  ...
  }
}

This will print the most significant figure first, unlike the while loops most people are mentioning.

geekster777
  • 297
  • 1
  • 5
0

I'm not going to do your homework for you but consider the following pseudo-code:

print_text_digits(x)
{
    if (x >= 10) print_text_digits(x / 10);
    switch (x) {
        print "zero" through "nine" as appropriate
    }
}

main()
{
    scan number into x;
    print_text_digits(x);
}

This relies on a recursive routine so that you get your digits processed one at a time, with the might significant digit printed first.

mah
  • 39,056
  • 9
  • 76
  • 93
0

I believe you need this:

#include <stdio.h>

int main (void)
{

int count = 0;
int x, count2;

printf("Please enter an integer: ");
scanf("%d", &x);

printf("\nYou have entered:\n\n");

int aux = x;
while(aux>0) {
    aux=aux/10;
    count++;
}
count2 = count;
while(count) {
    aux = x;
    for(int i=count-1;i>0;i--)
        aux=aux/10;
    for(int i=count2-count;i>0;i--)
        aux%=10;
    switch (aux) {
        case 0:
            printf("zero");
            break;
        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;
    }
    count--;
    if(count) printf(" ");
}
printf("\n\n");
return 0;
}

Now, with an input 52 it will propelly return five two.

Coneone
  • 292
  • 1
  • 8
0
#include <stdio.h>

static const char * const num[] = {
    "zero ", "one ", "two "  , "three ", "four ",
    "five ", "six ", "seven ", "eight ", "nine "
};

void printNum(int x)
{
    if (x < 10) {
        printf(num[x]);
        return;
    }
    printNum(x / 10);
    printNum(x % 10);
}

int main (void)
{
    int x;

    printf("Please enter an integer: ");
    scanf("%d", &x);

    printf("\nYou have entered:\n\n");

    printNum(x);
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159