Thread: First time poster - need help repeating this program

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    1

    First time poster - need help repeating this program

    Hi all,

    I am beginner C Programmer and am working on a simple program. My issue is to get it repeat by typing in "y" after it has me made its initial run. I am familiar with loops and have tried "do, while" in numerous combinations but I have no luck in getting it to repeat after it is initially done. Here is my code without loops, any help would be GREATLY appreciated:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    
        char play;
        char choice;
        printf("Welcome to Two doors.\n");
        printf("Would you like to play? (y/n): ");
        scanf("%c", &play);
    
    
    
    
        if (play == 'y')
    
    
            {
    
    
            printf("You are a prisoner in a room with 2 doors and 2 guards.\n");
            printf("One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.\n");
            printf("One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either.\n");
            printf("You have to choose and open one of these doors, but you can only ask a single question to one of the guards.\n");
            printf("What do you ask so you can pick the door to freedom?\n");
            printf("\tA.Ask the truth-guard to point to the door of doom.\n");
            printf("\tB.Ask the liar-guard to point to the door of doom.\n");
            printf("\tC.Doesn't matter which one you pick.\n");
            scanf("%s", &choice);
    
    
            char answer;
    
    
            printf("No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n");
            switch (answer)
                    {
                case 1:
                    printf("C");
                    break;
                case 2: 
                    printf("C");
                    break;
                case 3:
                    printf("C");
                    break;
                default:
                   break;
                    }
            }
    
    
    return 0;
    
    
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Here is a template:
    Code:
    #include <stdio.h>
    int main() {
      char choice;
      do {
        puts("Continue?");
        scanf(" %c", &choice); 
      } while (choice == 'y');
    }
    Notice the space in " %c".
    It ignores whitespace from earlier inputs.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    You could also try:

    Code:
    int i = 1;
    
    do 
    
    {[your code here]} while (i > 0);

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%s", &choice);
    "%s" is for reading a string, but "choice" is only a single char.

    You may also want to look into "toupper" and "tolower" to normalize input case, since 'Y' and 'y' should both be valid, though they're not the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Repeating Program
    By Bouchat in forum C Programming
    Replies: 3
    Last Post: 01-20-2014, 09:21 PM
  2. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  3. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  4. operator!= and primary-expression error : First time poster
    By kentrenholmpei in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2011, 06:24 AM