Thread: my do while loop not working

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    my do while loop not working

    when i run my code even if i enter "yes" for the input it doesnt loop over. any idea?

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    int x;
    char tin[2];
    printf("\n");
    do{
    printf("enter a ascii value to see its corresponding character - ");
    scanf("%i", &x);
    printf(" \n  the ascii character of %i is %c \n \t Would you like to start over?(yes or no)", x , (char)x);
    
    fgets(tin, 2, stdin);
    getchar();
    }while(tin == "yes");
    }
    Last edited by rodrigorules; 09-06-2005 at 05:41 PM.

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Code:
    while(tin == "yes")
    That's not how you compare strings. Use strcmp or strncmp. Furthermore, tin can never be "yes" since it's only got room for one character plus the nul character. Either make tin 4 bytes, or just store yes or no as a single character (y/n) and then use scanf to read it in.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    you could try whit this....


    Code:
    #include <stdio.h>
    
    int main()
    {
    char tin[2];
    char z[2];
    int x,flag;
    
    do{
    sprintf(tin,"no\0");
    
    printf("enter a ascii value to see its corresponding character - ");
    scanf("%d",&x);
    printf(" \n  the ascii character of %d is %c \n",x,x);
    printf(" \t Would you like to start over?(yes or no)");
    scanf("%s",&z);
    flag = strcmp(tin,z);
    
    
    }while(flag == 0 );
    }

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    Code:
    char tin[2];
    ...
    sprintf(tin,"no\0");
    You're still writing past the array boundary.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    char z[2];
    How many elements will you need if the user enters "yes"? How many if they enter "no"? (Hint, with either "yes" or "no" a 2 element array will not be big enough)

    This is nice:

    Code:
    scanf("%s",&z);
    1) The variable z is not a scalar variable, so you should not be passing the address of z as an argument. Get rid of the '&'.

    2) In a similar fashion to the gets() function, the user would be able to overflow the array z, as there is no limitation on the input . If you wish to use scanf() to get a string into an array, use a width specifier something like this:

    Code:
    char z[5]
    scanf("%4s",&z);  /* Read in at most 4 chars to leave room for '\0' */
    As well, if you are using scanf(), you ought to check its return value, like so:

    Code:
      char z[5]
       if(scanf("%4s",&z) != 1) {
         /* clean up the mess */
       }
    The return value of scanf corresponds to the number of arguments succesfully converted. If possible though, consider using fgets() instead of scanf() to read in a string.

    You forgot to #include <string.h>

    main() ought to return something meaningfulL

    Code:
    int main(void)
    {
       ....
       return 0;
    }
    Last edited by kermit; 09-07-2005 at 04:02 AM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    so ur saying i need to always leave room for \o in a string array?
    (so i need [4] if i want to put "yes" in it?

    ACtually - heres the edits i made based on ur comments
    still doesnt work, it doesnt loop, (and i changed the loop type)..

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    char tin[4];
    char yes[4];
    int x,flag=0;
    
    
    while(flag == 0 )
    {
    strcpy(yes,"yes");
    
    printf("enter a ascii value to see its corresponding character - ");
    scanf("%3d",&x);
    printf(" \n  the ascii character of %d is %c \n",x,x);
    printf(" \t Would you like to start over?(yes or no)");
    fgets(tin, 4 , stdin);
    getchar();
    flag = strcmp(tin,yes);
    }
    }
    Last edited by rodrigorules; 09-07-2005 at 02:04 PM.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Yes, you should always leave room for the '\0' character.
    All standard functions expect char arrays to be NULL terminated.

    so i need [4] if i want to put "yes" in it?
    Correct, for "yes" you should reserve room for 4.


    still doesnt work
    a newline character is left in the stdin buffer after your call to scanf.

    after your call to fgets tin contains only the newline character (since it was available in the buffer), and
    the rest of what you typed is once again left in the buffer.

    You need to flush the buffer after your call to scanf.
    There are many posts on these boards on how to do that. You can search for fflush, but do NOT use it to flush stdin.
    (to be lazy you can just put a call to getchar() before fgets to remove the newline, this will only help you in a perfect world where the user enters what you want them to).

    Also, when using fgets the newline character gets stored in your string
    (well not necessairly in your case since you only allow 4 characters with fgets).
    It's something you should be aware of, as it may affect your comparison. ("no\n" will not match "no")
    Last edited by spydoor; 09-07-2005 at 02:38 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    here ur advice is good but i dont think i know enough to do it, i changed it even more but it still dont work

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    char tin[4];
    char yes[4];
    char buffer[4];
    int x,flag=0;
    
    while(flag == 0 ){
    strcpy(yes,"yes");
    
    printf("enter a ascii value to see its corresponding character - ");
    scanf("%3d",&x);
    printf(" \n  the ascii character of %d is %c \n",x,x);
    printf(" \t Would you like to start over?(yes or no)");
    
    fgets(buffer, sizeof(buffer) , stdin);
    sscanf(buffer, "%s", &tin);
    fflush(stdout);
    getchar();
    flag = strcmp(tin,yes);
    }
    }

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    sorry, I probably should have not mentioned fflush, it was just a keyword for you to search with.

    try this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char tin[4];
        // no need to store "yes" in a variable... it never changes
        int x,flag,ch;
        flag =0;
    
        while(flag == 0 )
        {
            printf("enter a ascii value to see its corresponding character - ");
            scanf("%3d",&x);
            while ((ch = getchar()) != '\n' && ch != EOF);  //flush stdin
    
            printf(" \n  the ascii character of %d is %c \n",x,x);
            printf(" \t Would you like to start over?(yes or no)");
    
            fgets(tin, 4 , stdin);
            while ((ch = getchar()) != '\n' && ch != EOF);  //flush stdin 
    
            flag = strcmp(tin, "yes"); //compare should be okay since we fgets(,4,) otherwise we'd have to worry about newline char in tin
        }
    }
    Last edited by spydoor; 09-07-2005 at 02:56 PM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    what does this do?
    -
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);  //flush stdin
    -
    (btw program works great, but i just need to find out how it works)
    Last edited by rodrigorules; 09-07-2005 at 02:58 PM.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    EOF stands for End of File.

    getchar() returns EOF (an int) when there is nothing more to read or when there is an error reading.

    The while loop is calling getchar() as many times as needed to read (and essentially discard as we never use it again) everything that is sitting in the stdin buffer up to a newline character.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    why is the comment - // flush stdin
    in there?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rodrigorules
    why is the comment - // flush stdin
    in there?
    Probably because getchar is often a macro for getc( stdin ). At any rate, it's just a comment, explaining to the reader what that line of code does. You know, kinda what comments were made for.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 09:56 PM
  2. Replies: 6
    Last Post: 07-19-2005, 01:03 PM
  3. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 06:28 PM
  4. while() loop isn't working right..
    By Captain Penguin in forum C++ Programming
    Replies: 20
    Last Post: 10-03-2002, 10:29 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM