Thread: Help with Array.

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Help with Array.

    Code:
        printf("\t\tPlease Choose:\n");
        printf("\t\t[S]ales\n");
        printf("\t\t[P]ayment\n");
        printf("\t\t[E]xit\n");
        printf("\t\tYour choice: ");
        scanf(" %c", &choice);
    
    
        while (choice != 'S' && choice != 's' && choice != 'P' && choice != 'p' && choice != 'E' && choice != 'e')
        {
            printf("\n\t\t*WARNING* INVALID INPUT\n\t\tPlease input your choice correctly: ");
            scanf(" %c", &choice);
        }
    How do I change the line :
    Code:
    while (choice != 'S' && choice != 's' && choice != 'P' && choice != 'p' && choice != 'E' && choice != 'e')
    into an Array?

    For example,
    Code:
    char valid[] = {'S', 's', 'P', 'p', 'E', 'e'};
    Then I call valid instead of using the long code? ;O

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    man page strchr section 3
    Like
    while ( strchr("SsPpEe",choice) == NULL )

    Or if you don't care about case,
    while ( strchr("SPE", toupper(choice) ) == NULL )
    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
    May 2012
    Posts
    12
    Code:
    while ( strchr("SPE", toupper(choice) ) == NULL )
    What is toupper mean? ;O It include the 's' 'p' 'e'?

    Another question :
    How do I make my program delay for a few second? sleep(1) seem like don't work for me..
    I included <windows.h> but it display "No target architecture" Error.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Milo Juak MuTou View Post

    Another question :
    How do I make my program delay for a few second? sleep(1) seem like don't work for me..
    I included <windows.h> but it display "No target architecture" Error.
    You could use time.h and count time
    However sleep is much more appropriate.Maybe sleep(1) is too short ,so try sleep(5) and your program is going to stall for 5 seconds
    Or you could use function wait
    http://www.gidforums.com/t-11552.html
    Last edited by std10093; 07-05-2012 at 04:54 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Milo Juak MuTou View Post
    What is toupper mean? ;O It include the 's' 'p' 'e'?
    Aww ... cmon! It would take only a tiny effort on your part to find an answer to that question. Either online (googling toupper will find relevant information) or in even basic texts on C describe it.

    toupper(choice) converts the value of choice to something else. What do you think the "upper" would mean in the context of characters??

    Quote Originally Posted by Milo Juak MuTou View Post
    How do I make my program delay for a few second? sleep(1) seem like don't work for me..
    I included <windows.h> but it display "No target architecture" Error.
    [/FONT][/SIZE]
    Techniques to delay a program are compiler and system dependent. sleep() typically works with compilers/libraries under unix. Sleep() is the function in the win32 API. The case difference is important in C.

    If you want a portable technique, it is possible to loop checking time, until some period has passed. That technique is not generally recommended, however, as it consumes CPU cycles .... which is not a good idea with modern multi-tasking operating systems, as it affects the behaviour and responsiveness of both the host system and other running programs. The non-portable functions like sleep or Sleep() (depending on your operating system) are better, as they introduce a delay in a manner that has less impact on the host system or on other running programs
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM