Thread: Need help from the gurus. homework

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Need help from the gurus. homework code provided.

    here is the question posed.

    # Ask the user to enter a digit between 0 and 9. Have the program print out the digit in words, for example:

    Enter a digit between 0 and 9: 4
    You entered the number four

    Assume that the user will enter only a single digit. The user may accidentally enter a single character, and this should generate an error message.

    Code:
    #include <stdio.h>
    int main()
    {
        int num;  
        printf("Please enter a digit from 0 through 9\n");
        scanf("%d,",&num);
        
        if (num == 0)
           printf("The number you have typed is zero.\n");
        else if (num == 1)
            printf("The number you have typed is one.\n");
        else if (num == 2)
            printf("The number you have typed is two.\n");
        else if (num == 3)
            printf("The number you have typed is three.\n");
        else if (num == 4)
            printf("The number you have typed is four.\n");
        else if (num == 5)
            printf("The number you have typed is five.\n");
        else if (num == 6)
            printf("The number you have typed is six.\n");
        else if (num == 7)
            printf("The number you have typed is seven.\n");
        else if (num == 8)
            printf("The number you have typed is eight.\n");
        else if (num == 9)
            printf("The number you have typed is nine.\n");
        else
            printf("You have selected to not follow the instructions therefore an error has occurred.\n"); 
            
            
            return 0;
    }
    when i type in other integers greater then 9 i display the error message, however when i type a character i return the your number is two.

    let me know where i went wrong.
    Last edited by 1BadRbt; 11-20-2006 at 12:12 AM.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Go through the following code. If you have any questions, please don't hesitate to ask!
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
      int num;  
      printf("Please enter a digit from 0 through 9\n");
      num=getchar();    
      if (isdigit(num))
      {    
        if (num == '0')
          printf("The number you have typed is zero.\n");
        else if (num == '1')
          printf("The number you have typed is one.\n");
        else if (num == '2')
          printf("The number you have typed is two.\n");
        else if (num == '3')
          printf("The number you have typed is three.\n");
        else if (num == '4')
          printf("The number you have typed is four.\n");
        else if (num == '5')
          printf("The number you have typed is five.\n");
        else if (num == '6')
          printf("The number you have typed is six.\n");
        else if (num == '7')
          printf("The number you have typed is seven.\n");
        else if (num == '8')
          printf("The number you have typed is eight.\n");
        else if (num == '9')
          printf("The number you have typed is nine.\n");
      }
      else
        printf("You have selected to not follow the instructions therefore an error has occurred.\n"); 
        
      return 0;
    }
    As a further exercise, try making the code more compact.
    Last edited by noodles; 11-20-2006 at 01:02 AM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    yeah i appreciate the help

    the isdigit i assume detects if it is an actual interger.

    I could make it more compact however it was the display as a word which got me.

    I hope by me providing code and not asking people to do my homework, I can come back for more.

    Sad i am a nuclear engineering major but c kicks my but i cant think in a logarithmic fashion to well sometimes.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > the isdigit i assume detects if it is an actual interger.
    It checks by lookup if ascii values are decimal digits. That's all. C is generally type safe so an int stays an int unless you cast it to something else or attempt to store a value differently.

    > I hope by me providing code and not asking people to do my homework, I can come back for more.
    Yes.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Sad i am a nuclear engineering major but c kicks my but
    What does a nuclear engineer (should we all be afraid at this point?) want with a language which isn't rated for such safety critical applications like nuclear installations?
    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.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    This could be bad
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    *contemplates the irony of his own avatar...*
    If you're dyslexic, are you unclear about nuclear?
    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.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Well my friend who posted there code the issue is the last Else if statement is var ==9 so you have a few things you can do. A just simply add a loop. Inside all the Else If statements place a break inside all the statements. Then add an Else with a Printf saying retype the number and as a result you reloop. Another way is just too simply say but have it wrong n<=9. a little back story the reason why when you typed in a char you received an error is because you scanf is looking for a decimal "scanf("%d,",&num);" and in ANSI C all chars are trying ints example A==1 B==2 (also caps are greater value then lower case A>a). To avoid this error to a degree is to have the code gets(num) then num1 = atoi(num). This will take any int or char and turn it into an int. Lets say you typed in 12334 and did not have atoi what can happen is the computer looks at the number like a solid string "12334". Sorta like you zip code but if you atoi the int string it will break it up to read 12,334 and from there you can apply you int.
    Last edited by KoG Metalgod; 11-20-2006 at 07:29 AM.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Stop hijacking threads, posting opinionated nonsense, and generally acting like an idiot.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    Quote Originally Posted by Salem
    > Sad i am a nuclear engineering major but c kicks my but
    What does a nuclear engineer (should we all be afraid at this point?) want with a language which isn't rated for such safety critical applications like nuclear installations?
    It is a requirement for the degree is all. I chose it a class due to my limited linux and programming skills and needing to learn more about programming languages. Plus learning BASIC was not really exciting to me. I work more on the hardware end and use ladder logic and AML programming at my work and C will help big time in coding and debugging practices especially pertaining to robotics.

    I appreciate the humorous comment though.

    BTW only reason i am taking nuclear engineering major is cause i was prior military, waiting to get that done to work on a project management - possibly IT or business focused MBA.

    Thanks again everyone and honestly i would be scared if a reactor was only electronically controlled chernobyl 2007 haha

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    That would be like Chernobyl/Matrix
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    "nuclear about unclear"? What on earth is Salem talking about

    Thanks again everyone and honestly i would be scared if a reactor was only electronically controlled chernobyl 2007 haha
    That's an excellent idea, actually. It's relatively harder to program a computer to perform crazy, random, unsanctioned runtime experiments without safety precautions, than to allow dyslexic technicians to do the same.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM