Thread: Stop if less than 0

  1. #31
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    O.K....This is what I have right now, it compiled, but is not converting to ASCII... (
    Also, how could I end the program if the user enters number 3?
    Thanks for help...

    Code:
    #include <stdio.h>
    
    main(void){
    
    
      int Number;
    
    
    
       printf("Type an ASCII code 4 through 100 to get their\n");
       printf("correspondent character constants:  ");
       scanf("%d",& Number);
       printf("%8c\n", Number);
    
       while( Number < 4 || Number > 100){
       printf("Icorrect! Number has to be between 4 and 100. TRY again\n");
       scanf("%d",&Number);
                      }
     /* if ( Number < 4 || Number > 100 ) Don't have to check ) */
      printf("Thank you for entering the number %d!\n", Number);
      }

  2. #32
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    to printf a char %c.

    Use Google, find a C printf/scanf cheat sheet.

  3. #33
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Still don't get it...:0(....
    Tried everything....
    Thanks for your help!

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Newbie999
    Still don't get it...:0(....
    Tried everything....
    No you didn't! You've been told at least three times now. Pay attention, damnit!

    USE %c TO PRINT A CHARACTER!

    USE %c TO PRINT A CHARACTER!

    USE %c TO PRINT A CHARACTER!

    USE %c TO PRINT A CHARACTER!

    USE %c TO PRINT A CHARACTER!

    USE %c TO PRINT A CHARACTER!



    Quzah.
    Hope is the first step on the road to disappointment.

  5. #35
    Registered User
    Join Date
    Oct 2006
    Posts
    28

    Unhappy

    I am using %c for print f...
    Sorry, I am just the beginner. just trying to get your guys help....:0(

    Code:
    #include <stdio.h>
    
    
    main(void){
    
    
      int Number;
                  
    
    
       printf("Type an ASCII code 4 through 100 to get their\n");
       printf("correspondent character constants:  ");
       scanf("%d",& Number);
       printf("%c\n", Number);
    
       while( Number < 4 || Number > 100){
       printf("Icorrect! Number has to be between 4 and 100. TRY again\n");
       scanf("%g",&Number);
       }
     /* if ( Number < 4 || Number > 100 ) Don't have to check ) */
      printf("Thank you for entering the number %d!\n", Number);
      }

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Hey! How could you say that it doesn't work?? It works, really. Though, you still have to make it tidier...

    It works for the first question, but when we enter a number for the second time, it doesn't give you the character.

    Or, you mean that: when we first enter 1, it will say INCORRECT.... and after entering the next value, it doesn't give you the character... Is that what you mean??

    Let me tell you my opinion... You are going to ask the user a number, and then show the ascii character. But if the user doesn't enter number between 4 and 100, the program will ask the user again with the same question. It will repeat again and again unless the user gives the appropriate number. Isn't it?

    OK? Let me give you a tips. In order to do that, you have to use do-while loop.
    1. First, write 'do' followed by '{' before the first printf...
    2. Then add this line after the first scanf:
    Code:
    if(Number < 4 || Number > 100)
          printf("\nIncorrect! Number has to be between 4 and 100. TRY again!\n\n");
    3. End the do-while loop with a '}' followed by while();. Within the brackets should be the expression.
    4. Then, print the character after do-while.

    So, you don't need to include the second scanf which asks the user again a number. It will repeat asking the same question if the entered number is wrong.

    Got it? Give it a try!

  7. #37
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Don't worry... There is nothing to do with your %c. Doesn't matter whether you put %c or %8c. It still works! Don't worry! That's not the mistake of your program... Do you understand my previous suggestion??

  8. #38
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    O.K...This is what I got...
    Is this what you mean...I want to write a program to convert any decimal ASCII codes from 4 to 100 to their correspondent character constant. I want to use a loop to repeatedly ask the user to enter a valid decimal number in case input is not between 4 and 100. I would like to end the program if user enters number 3.

    I am struggling here...


    Code:
    #include <stdio.h>
    
    main(void){
    
    
      int Number;
    
    
       do{
       printf("Type an ASCII code 4 through 100 to get their\n");
       printf("correspondent character constants:  ");
       scanf("%d",& Number);
       if(Number < 4 || Number > 100)
       printf("\nIncorrect! Number has to be between 4 and 100. TRY again!\n\n");
       printf("%c\n", Number);
    
       do-while loop{
    
       while( Number < 4 || Number > 100);{
       printf("Icorrect! Number has to be between 4 and 100. TRY again\n");
       }
     /* if ( Number < 4 || Number > 100 ) Don't have to check ) */
      printf("Thank you for entering the number %d!\n", Number);
      }

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Mmmm, you never uses do-while loop, do you?

    Do and while should always be in pair in do-while loop. You need read some reference.

    OK... This line:
    Code:
     do-while loop{
    Should be replaced by:
    Code:
    } while(Number < 4 || Number > 100);
    Then, move this line below it:
    Code:
       printf("%c\n", Number);
    And you don't need these lines anymore:
    Code:
     while( Number < 4 || Number > 100);{
       printf("Icorrect! Number has to be between 4 and 100. TRY again\n");
       }
    Erase them.

    Try it!

  10. #40
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    O.K I changed it, it compile, but it won't convert numbers...
    It just does this :

    ---Type an ASCII code 4 through 100 to get their
    correspondent character constants: 5

    Thank you for entering the number 5!---

    I apprecaite your help!


    Code:
    #include <stdio.h>
    
    main(void){
    
    
      int Number;
    
    
       do{
       printf("Type an ASCII code 4 through 100 to get their\n");
       printf("correspondent character constants:  ");
       scanf("%d",& Number);
       if(Number < 4 || Number > 100)
       printf("%c\n", Number);
    
      } while(Number < 4 || Number > 100);
        printf("%c\n", Number);
    
       /* if ( Number < 4 || Number > 100 ) Don't have to check ) */
      printf("Thank you for entering the number %d!\n", Number);
      }

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Mmmm??

    Replace this line at line 9:
    Code:
    printf("%c\n", Number);
    Replace it with an error message like this:
    Code:
    printf("\nIncorrect! Number has to be between 4 and 100. TRY again!\n\n");
    But, overall, your program works properly in my computer. Really, it shows you the ascii character.

  12. #42
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    I've replaced it. It compiled, but it doesn't work it still gves me the same thing:

    Type an ASCII code 4 through 100 to get their
    correspondent character constants: 5

    Thank you for entering the number 5!


    Code:
    #include <stdio.h>
    
    main(void){
    
    
      int Number;
    
    
       do{
       printf("Type an ASCII code 4 through 100 to get their\n");
       printf("correspondent character constants:  ");
       scanf("%d",& Number);
       if(Number < 4 || Number > 100)
       printf("\nIncorrect! Number has to be between 4 and 100. TRY again!\n\n");
    
      }
      while(Number < 4 || Number > 100);
        printf("%c\n", Number);
    
       /* if ( Number < 4 || Number > 100 ) Don't have to check ) */
      printf("Thank you for entering the number %d!\n", Number);
      }

  13. #43
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > printf("%c\n", Number);
    Wake up, smell the roses and STOP trying to print a number as a character!

    You've been told so many times that this is wrong, yet you still do it.

    PICNIC
    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.

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Whoa?? How come??

    It really works in my computer... I don't know about it... I'm using devC++ and Quincy for compiling, and they give me the same result: it works!!

    Mmmm, after all, I think you should learn more about some basic things. Pay attention at syntax. Try to find some books for reference, you can try Deitel&Deitel: C How to Program.

  15. #45
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Well, Thank you all...o(
    You're right, I should learn more.
    Salem, I tried to follow directions. I apologize, but I am just the beginner trying to solve the problem.
    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  3. how do I stop text from being erased
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 02-20-2008, 12:00 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM