Thread: program dosen't work for double digit input

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    41

    program dosen't work for double digit input

    I wrote code that replaces integers from 0 to 3 with strings. I was only allowed to use getchar() and putchar(). If the input is 1, the output will become "one".

    And I am only allowed to use getchar and putchar and no functions.

    However if the input is 11 the output becomes "oneone"

    Code:
    
    #include <stdio.h>
    
    int main()
    {
        int c;            
        char* arr[4] = {"zero", "one", "two","three"};
        int i;
    
            while ((c = getchar ()) != EOF) 
            {
                if(c==0+'0') {
                    char* str = arr[0];
                    for (i = 0; str[i] != '\0'; i++) {
                        putchar(str[i]);
                    }
                }
                else if(c==1+'0') {
                    char* str = arr[1];
                    for (i= 0; str[i] != '\0';i++) {
                        putchar(str[i]);
                    }
                }
                else if(c==2+'0') {
                    char* str = arr[2];
                    for (i = 0; str[i] != '\0'; i++) {
                        putchar(str[i]);
                    }
                }
                else if(c==3+'0') {
                    char* str = arr[3];
                    for (i = 0; str[i] != '\0'; i++) {
                        putchar(str[i]);
                    }   
                }
                else
                    putchar(c);
       }
    
    return 0;
    }
    Could someone give me a hint on how to modify the code ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    Add a
    putchar ('\n');
    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.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    41
    in the loop?
    that won't help i believe

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    So did you just believe it wouldn't work.

    Or you tried it and it didn't work?

    Beliefs are no substitute for actual experiments and results.

    > Could someone give me a hint on how to modify the code ?
    There are several loops, I suppose you need to think about which one I meant.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    41
    no I tried it
    I added a putchar('\n') in the while loop and in the for loops
    I still didn't get the result I wanted.
    for example it converts 11 to oneone. It should only work for int from zero to three.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    But 11 is oneone

    1
    1
    would be
    one
    one
    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.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    Maybe you should state the result you want!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Apr 2018
    Posts
    41
    for example if the input is 0, 1,2 or 3
    the output should be "zero", "one" "t
    wo" or "three".
    for all other numbers the output should remain the same. so if the input is 22 the ouput should be 22 not twotwo or
    two
    two

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    How would you know that 11 is eleven and not oneone, when you're reading and processing one character at a time?
    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.

  10. #10
    Registered User
    Join Date
    Apr 2018
    Posts
    41
    I dont know thats what i am trying to solve

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    Well I guess you need to call getchar more than once before deciding what to do.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timer dosen't work
    By geek@02 in forum Windows Programming
    Replies: 8
    Last Post: 09-14-2007, 09:10 PM
  2. Why O Why dosen't this work?
    By styles in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2007, 11:10 AM
  3. Why this dosen't work in C
    By ruab in forum C++ Programming
    Replies: 9
    Last Post: 08-31-2006, 10:12 AM
  4. Why dosen't this work
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 08-16-2002, 08:18 PM

Tags for this Thread