Do-While looping issue

This is a discussion on Do-While looping issue within the C Programming forums, part of the General Programming Boards category; I wrote this code as a practice assignment and cannot seem to get my opening question to loop until the ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Do-While looping issue

    I wrote this code as a practice assignment and cannot seem to get my opening question to loop until the correct key is entered, correctly. I would like the question to be asked until the Y,y,N or n key is entered. If any other key is entered (ex. t or %) i would like it to ask the question again. i have tried many different loop combinations and there always seems to be one condition in which the question will loop indefinitly. If anyone knows how to loop the opening question until the correct key is entered and still make the program execute the subsequent "if" statements i would greatly appreciate your input.




    #include <stdio.h>
    int main()
    {
    int a;
    char noun_one[20];
    char adjective[20];
    char verb[20];
    char noun_two[20];

    do{
    printf ("Would you like to do a MadLib?(Y/N):");
    getchar(); } while (a!='Y'||a!='y'||a!='N'||a!='n');


    if (a=='y'|| a=='Y'){
    printf ("Type in a noun:");
    scanf ("%s", &noun_one);
    printf ("type an adjective:");
    scanf ("%s", &adjective);
    printf ("type a verb:");
    scanf ("%s", &verb);
    printf ("type another noun:");
    scanf ("%s", &noun_two);

    printf ("Compiling Madlib...");
    printf ("This is what i came up with =)\n The %s ribbon was wrapped around the %s.", adjective, noun_one);
    printf ("When the %s came to the house and saw he %s as fast as he could to the door",noun_two, verb);
    }

    else if (a=='N'||a=='n')
    printf ("Well maybe another time than!");

    getch();
    return 0;

    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Please post your code within code tags next time you post. << !! Posting Code? Read this First !! >>

    getchar(); returns a character from the standard input, but what are you assigning this value to?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by atom.sk View Post
    If anyone knows how to loop the opening question until the correct key is entered and still make the program execute the subsequent "if" statements i would greatly appreciate your input.
    First, no code tags. It makes your code very difficult to study when there's no indentation.

    For the Yes or No input...
    Code:
    char a;  // for yes or no
    
    
    int main(void)
      {
       printf ("Would you like to do a MadLib?(Y/N) :  "); 
       a = getchar( );
       if ((a != 'y') && (a != 'Y'))
         { 
          puts("Later dude...\n");
          exit(0); 
         }
      
    // rest of code goes here
    
       return 0 ;
      }
    This will allow the main body of your code to run only if a Y or a y is entered... everything else is treated as a no. It's done this way to avoid the problem of a user being too stupid to press the right keys. It's better the program bails out than having some ticked off idiot pounding your keyboard to death trying to quit without actually reading the screen.

    You can then take the rest of your code out of the surrounding if statements and let it run normally.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    First off, I do apologize for not using code tags, I am new to the forums and will read through the posting guidelines before posting again(Thanks for linking btw).

    And to tater,

    that was very helpful, and solved a piece of the puzzle, i would like to be able to have the program close only while the N or n keys are pressed and not when any other key is typed in. I will continue to work on it(It may have been the fact that i was using OR operands instead of AND).

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    The {noun} that Tater gave you will execute {number} time(s). You demonstrated from your first post that you understand the concept of a do-while {noun}, which {adverb} executes at least once. Simply make sure your loop terminates when the key pressed is either a 'N' {conjunction} a 'n'. Since do-while loops are looking for the "continue" condition instead of the "terminate" condition, reverse the logic from the previous sentence using {noun}.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by atom.sk View Post
    First off, I do apologize for not using code tags, I am new to the forums and will read through the posting guidelines before posting again(Thanks for linking btw).

    And to tater,

    that was very helpful, and solved a piece of the puzzle, i would like to be able to have the program close only while the N or n keys are pressed and not when any other key is typed in. I will continue to work on it(It may have been the fact that i was using OR operands instead of AND).
    Ok... I was assuming you wanted something really simple for that. If you want to trap it to either yes or no but ignore all else you can try something like this...

    Code:
    do
      {
       printf ("\n \n Would you like to do a MadLib?(Y/N) :  "); 
       a = getchar( );
       if ((a == 'n') || (a == 'N'))
         { 
          puts("Later dude...\n");
          exit(0); 
         }
       }
    while ((a != 'Y') && (a != 'y'));
    
    // rest of code
    Last edited by CommonTater; 12-19-2010 at 10:41 AM.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    So i ended up figuring it out with everyone's help. first i did not include "return 0" if the N or n condition was met after the program looped at least once, and secondly i changed the (Y or y or N or n) while statement, to read (Y or y and N or n) which i believe was one of the major problems also. my final code looks as follows:

    Code:
    #include <stdio.h>
    int main()
    {
        char a;
        char noun_one[20];
        char adjective[20];
        char verb[20];
        char noun_two[20];
    
        do {
        printf ("Would you like to do a MadLib?(Y/N):\n");
        scanf ("%s", &a);
    
        if (a=='y'|| a=='Y'){
        printf ("Type in a noun:\n");
        scanf ("%s", &noun_one);
        printf ("type an adjective:\n");
        scanf ("%s", &adjective);
        printf ("type a verb:\n");
        scanf ("%s", &verb);
        printf ("type another noun:\n");
        scanf ("%s", &noun_two);
    
        printf ("Compiling Madlib...\n");
        printf ("This is what i came up with =)\n The %s ribbon was wrapped around the %s.\n", adjective, noun_one);
        printf ("When the %s came to the house and saw he %s as fast as he could to the door\n",noun_two, verb);
        }
    
        else if (a=='N'||a=='n'){
        printf ("Well maybe another time than!\n");
        return 0;}} while (a!='Y'||a!='y'&&a!='N'||a!='n');/*this line seemed to correct everything*/
    
        getch();
        return 0;
    
    }
    now it did require me to expand my do-while loop to the end of the code as opposed to just the first line, but it does seem to be a fix.(no more looping under any circumstance)

    special thanks to tater for pointing out that i did not specify any terminating condition *thumbs up*.

    I will now try to work out an alternate solution using taters method(there is more than one way to skin a cat after all).

    thanks for all the help so far guys, it is greatly appreciated. I am still very new to the C language and to coding in its entirety.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That should work... congrats.

    The only suggestion I would offer is that it should always be int main (void) ... I know your way is working but some compilers may not like it.

    And you are right... with code there is always more than one way of doing things.
    Last edited by CommonTater; 12-19-2010 at 01:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. Getlines and looping
    By Blanked in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2009, 06:26 PM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. Looping Issue
    By stormy in forum C Programming
    Replies: 5
    Last Post: 01-24-2006, 01:35 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21