Thread: Can't get do while loop working

  1. #1
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45

    Can't get do while loop working (SOLVED)

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define MAX_STRING_LENGTH 1
    
    int main(void) {
    
        /* Variáveis. {{{ */
        char estado_civil;
        char resp[MAX_STRING_LENGTH];
        int idade;
        int casad = 0; /* Casado(a). */
        int solt = 0; /* Solteiro(a). */
        int viuv = 0; /* Viúvo(a). */
        int sep = 0; /* Desquitado ou separado. }}} */
    
        
        do { 
            printf("Idade?\n");
            scanf("%d", &idade);
            printf("%d.\n", idade); getchar();
    
            printf("Etado civíl ?\n" 
                "C, casado(a); S, solteiro(a); V, viúvo(a); D, desquitado(a):\n");
            estado_civil = getchar();
            
            switch (estado_civil) { /* {{{ */
                case 'C': case 'c':
                    casad++;
                    break;
                case 'S': case 's':
                    solt++;
                    break;
                case 'V': case 'v':
                    viuv++;
                    break;
                case 'D': case 'd':
                    sep++;
                    break;
                default:
                    printf("Caractere inválido.\n");
                    break;
            } /* }}} */
    
            printf("Mais uma pessoa? (s/n)\n");
            scanf("%s", resp);
            /* scanf("%1c", resp); */
            /* getchar(); */
            /* resp = getchar(); */
    
        } while (strcmp(resp, "s") != 0); 
    
        printf("C = %d, S = %d, V = %d, D = %d.\n", casad, solt, viuv, sep);
    
        return 0;
    
    }
    
    /* 
     * vim: foldmethod=marker foldmarker={{{,}}}
     */
    No matter the resp is "s" or not, the do while loop always stops..
    Last edited by FernandoBasso; 10-05-2011 at 06:36 PM. Reason: Solved!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    After you do this:
    Code:
    estado_civil = getchar();
    You hit enter. Then you do this:
    Code:
     scanf("%s", resp);
    I bet if after that line you put in:
    Code:
    printf( "\'%s\'\n", resp );
    You will find your missing enter.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are you by chance working with your caps lock turned on?

    As it is it will only exit on lower case "s" a capital "S" would not be detected.

    Also you don't even need strcmp for single characters...

    Try... while(tolower(resp) != 's');

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Try... while(tolower(resp) != 's');
    resp is a string.

    Actually, no it's not:
    Code:
    #define MAX_STRING_LENGTH 1
    
    int main(void) {
    
        /* Variáveis. {{{ */
        char estado_civil;
        char resp[MAX_STRING_LENGTH];
    There isn't enough room in resp to store the nul character, so it's not valid treatment to use it as a string. So even if the extra enter key isn't causing the issue, the fact that it's running off the end of the buffer when it's used as a string could be making it fail the strcmp.


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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are you by chance working with your caps lock turned on?

    As it is it will only exit on lower case "s" a capital "S" would not be detected.

    Also you don't even need strcmp for single characters...

    Try...
    Code:
            printf("Mais uma pessoa? (s/n)\n");
        } while (tolower(getchar()) != 's');

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    resp is a string.
    You're too quick for me, my friend... I was mid-way through fixing it when you posted

  7. #7
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    I am sure I don't have caps lock on.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FernandoBasso View Post
    I am sure I don't have caps lock on.
    Then go back and take another look at message #5 in the thread...

  9. #9
    Registered User FernandoBasso's Avatar
    Join Date
    Oct 2011
    Location
    Brazil
    Posts
    45
    Quote Originally Posted by CommonTater View Post
    Are you by chance working with your caps lock turned on?

    As it is it will only exit on lower case "s" a capital "S" would not be detected.

    Also you don't even need strcmp for single characters...

    Try...
    Code:
            printf("Mais uma pessoa? (s/n)\n");
        } while (tolower(getchar()) != 's');
    It did the trick. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with 'while' loop.
    By Von Fuzzball in forum C Programming
    Replies: 2
    Last Post: 05-03-2011, 08:04 PM
  2. Why my while loop is not working ??? pls help
    By spurs01 in forum C Programming
    Replies: 22
    Last Post: 11-11-2009, 05:11 AM
  3. my do while loop not working
    By rodrigorules in forum C Programming
    Replies: 12
    Last Post: 09-07-2005, 06:52 PM
  4. for loop not working
    By clementd in forum C Programming
    Replies: 4
    Last Post: 01-01-2003, 10:29 AM