Thread: do while loop for chars

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    do while loop for chars

    I have created a program where by a user inputs a set of upper case characters and then the program carries out a set of tasks based on the input.

    What I'd like to know is how do I implement a do while loop so if the user enters either a number or a lower case letter then the program should jump to the end and tell the user that they have entered an invalid input.

    The name of the array that stores the char's is called string.

    Code:
    do {
    
    //code here
    
    
    
    
    } while (string = "A", "B", "C", "D") //etc etc

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hencherz View Post
    how do I implement a do while loop so if the user enters either a number or a lower case letter then the program should
    Use an if() inside the loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    x is their input say and so now you write it as:

    Code:
    do
    {
      if(x!="A" && x!="B" && x!="C".......&& x!="z")
    {
     return(1)
    }
    }while ...............

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Note, "A" is a string, and consists of the letter 'A' followed by a terminating null character. You can not compare strings with == or !=, you must use strcmp. If, however, you want to compare individual characters, you need to put the character in single quotes, like 'A'. For your specific problem, you can use the isupper function, which returns true if the character is an upper case letter, and false for everything else:
    Code:
    #include <ctype.h>  // needed for the isupper() function
    
    do {
        some stuff
        x = read char from user
    } while (!isupper(x));

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Code:
    do {
    // carry out tasks
    } while (!isupper(string));

    I get a string is an undeclared identifier? but string has already been declared and is inside the do while function.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    do while is not a function, though the curly brackets it uses do create a separate scope (an "inner" scope that the outer scopes can't see). If you declare a variable inside the curly brackets and try to use it outside (i.e. in the "while" condition of the do while loop), then that variable is not visible. Move the declaration outside the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  2. Int and Chars
    By sql.scripter in forum C Programming
    Replies: 3
    Last Post: 02-26-2006, 01:37 AM
  3. How do I loop through a string of chars?
    By silverfoxtp in forum C++ Programming
    Replies: 9
    Last Post: 11-24-2003, 11:04 AM
  4. help with chars
    By kurz7 in forum C Programming
    Replies: 7
    Last Post: 01-07-2003, 08:40 AM
  5. HELP with chars!!!!
    By poopy_pants2 in forum Windows Programming
    Replies: 2
    Last Post: 12-18-2002, 05:58 PM