Thread: do loop does not exit on key word

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    1

    do loop does not exit on key word

    hi ,
    for starters i hope this question wasn't posted yet.
    i want to write a small program that askes the user what he wants to do , and then executes the comand in system . But for some reason it doesn't quit on the key word
    this is the code:
    Code:
     #include <stdio.h>
    
    /*a simple interaction programme that schow the system options*/
    #define systemt "\x1b[32m"                 /*changes the color to green for system output*/
    #define user "\x1b[0m"                    /*changes the color to white for user input*/
    int main()
    { 
     char name[20],choise[20];
    
        printf(systemt "hello user what's you name? \n");
          printf(user);
        gets(name);
    do{
        printf(systemt"hi %s, how can I help you ? \n",name);
          printf(user);
        gets (choise);
        system(choise);}
        while(choise !="bye");/*does not quit loop when i type bye*/    
    
    return(0);
    }
    /*void help()
    {
    }*/
    Is there something wrong white the code or does the system bock me from reusing the choise string.
    thx for all reply's
    ps i'll inculde the original c file as well
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The way to compare C strings is with the strcmp() function - declared within the standard header <string.h> - not comparison operators (==, !=, >=, etc).

    Also, gets() has been removed from the latest C standard for a reason - it is dangerous. Look up the function fgets() [all versions of C] or gets_s() functions [C11 only].

    Your program is calling system(choise) BEFORE any check on what choise contains. That is also quite dangerous - it allows a user to do almost anything, malicious or not.

    And choice is not spelt with an 's'.
    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. my do-while loop won't exit
    By Darkroman in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2011, 08:42 AM
  2. why it can not exit the while loop
    By letmoon in forum C Programming
    Replies: 3
    Last Post: 10-30-2008, 09:59 AM
  3. Can't exit a do/while loop - please help
    By Vireyda in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2005, 06:46 AM
  4. Cannot exit 'while' loop
    By s_ny33 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2005, 04:59 PM
  5. Can't exit this loop
    By Goalie35 in forum C Programming
    Replies: 4
    Last Post: 10-11-2001, 01:39 PM

Tags for this Thread