Thread: Help: Do while not a character

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    43

    Help: Do while not a character

    Hello everyone. I am very new to programming and I have just signed up in hopes that I'll be able to learn better and have access to better resources.

    I have one question in particular, for now:

    Is there a way to make 'while' carry out an argument if the input is a character string instead of a number? For example, I wanted to create a while statement which will output an error message if the user enters a character (i.e. a-z) instead of a numerical value.

    I tried while (x != %c) but obviously it doesn't work.

    Thanks in advance for any help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by MC++ View Post
    Hello everyone. I am very new to programming and I have just signed up in hopes that I'll be able to learn better and have access to better resources.

    I have one question in particular, for now:

    Is there a way to make 'while' carry out an argument if the input is a character string instead of a number? For example, I wanted to create a while statement which will output an error message if the user enters a character (i.e. a-z) instead of a numerical value.

    I tried while (x != %c) but obviously it doesn't work.

    Thanks in advance for any help!
    Welcome to the madhouse -- er, *forum*!

    In programming, you can OWN the computer, if you just know how! So yes.

    Code:
       int number;
    
       while((scanf("%d", &number)) < 1) {
          getchar();
          printf("Enter a NUMBER, please\n");
          
       } 
       
       printf("\nYou entered %d\n",number);

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Awesome, thanks a lot!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Okay, so now I've got another issue which I need help with!

    So thanks to my friend up there I have sort of resolved the above issue, but there is one problem;

    Code:
    while ((*rooms < 1) || (*rooms > 26)){
                getchar();
                printf("###Error! must be positive\n\n>>> How many rooms are there?: ");
                scanf(" %d", rooms);
                }
    What this code block does is that if it detects a character input (or a value outside the specified range ( 1 - 26)) it outputs the ###Error! message line. The problem I'm having is that if the user enters multiple characters (abc) it will spit out the message 3 times. What can I do to counteract this?

    Thanks guys!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this:

    Code:
    #include <stdio.h>
    
    int main(void) {
       int i, rooms=0;
    
       while ((rooms < 1) || (rooms > 26)){
          printf("\nEnter the number of rooms [1-26]? ");
          i=scanf(" %d", &rooms);
          if(!i || rooms < 1 || rooms > 26) {
             printf("###Error! Number of rooms must be positive.\n");
             while((i=getchar()) != '\n');
          }
       }
       
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-05-2011, 08:21 PM
  2. warning: multi-character character constant
    By aadil7 in forum C Programming
    Replies: 2
    Last Post: 12-12-2010, 10:02 PM
  3. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  4. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  5. gibrish when copying strings character by character
    By captain-cat in forum C Programming
    Replies: 2
    Last Post: 07-23-2004, 07:48 AM