Results 1 to 7 of 7

Thread: how to repeatedly run a program until the user perss yes or y

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    how to repeatedly run a program until the user perss yes or y

    As I am new to C language I tried to work on loops and started with while loop.

    I want to make a program which ask the user lets say do you have friends. The user will press the y or n key from keyboard.

    If the user press y so the program will display
    Yes you have friends.

    Again the user will be asked for answering the same question with y or n option and this process will be going on and on until the user will be pressing y.

    I have tried something like this:

    Code:
    int main()
    {
    
    char a = 'y', store;
    
    printf("Do you have friends, press y for yes and n for no = \n");
    scanf("%c", &store);
    
    
    	while (a == store)
    	{
    		
    		printf(" Yes I do have friends \n");
    		break;
    	}
    
    	printf("Loop ended");
    
    	getch();
    	
    }
    I can't make it complete code that how it would satisfy the above-mentioned situation.

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: how to repeatedly run a program until the user perss yes or y

    If the user press y so the program will display
    Yes you have friends.

    Again the user will be asked for answering the same question with y or n option and this process will be going on and on until the user will be pressing y.
    Part of the problem here is the program requirement. From the above, if the user answers 'y', then the user will be asked the same question until the user answers 'y'. ??? What is the termination condition for the loop? Where does the requirement for input need to be? Before writing code, you need to be very clear as to the logic of the program and how it is supposed to work.

    The other issue is that the program isn't dealing with the new-line character that is present in the input when <return> key is pressed. So the first time scanf() is used, it retrieves the entered character. The next time scanf() used it retrieves the new-line character.

    Consider

    Code:
    int main()
    {
    	char store;
    
    	do {
    		printf("Do you have friends, press y for yes and n for no = \n");
    		do {
    			scanf("%c", &store);
    		} while (store == '\n');
    
    		if (store == 'y')
    			printf(" Yes I do have friends \n");
    	} while (store == 'n');
    
    	printf("Loop ended");
    }
    Last edited by 2kaud; Sep 7th, 2017 at 01:04 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: how to repeatedly run a program until the user perss yes or y

    Part of the problem here is the program requirement. From the above, if the user answers 'y', then the user will be asked the same question until the user answers 'y'. ??? What is the termination condition for the loop?
    The termination condition will be pressing the n key. The while loop will be continue until and unless the user is pressing y. As the user will press 'N' instead of 'Y' the program will be exit.


    This do-while loop is not satisfying my requirement.

    What i expect from my code is that as long as the user is pressing 'y' the code will be executing over and over again. As the user presses the 'n' the program will stop executing.

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: how to repeatedly run a program until the user perss yes or y

    is that as long as the user is pressing 'y' the code will be executing over and over again
    Then just change

    Code:
    } while (store == 'n');
    to

    Code:
    } while (store == 'y');
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: how to repeatedly run a program until the user perss yes or y

    @ 2kaud

    As you know that I am very new to C language. So understanding this much code is little difficult.

    what is this line used for

    Code:
    while (store == '\n');

  6. #6
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: how to repeatedly run a program until the user perss yes or y

    '\n' is the new line character (when you press return key). scanf("%c"...) only obtains one character from input. So if the user types y then return the return is still in the input and has to be extracted. I based the code in post #2 upon the posted code in post #1. However this has an 'obvious' problem. If the user types yy return then as %c format only obtains 1 char it reads the first y and then the next time round the loop as there is already a y in the input which is read by scanf() without waiting for further input from the user. As you continue to learn c, you should come across how to deal with this.

    The character \ followed by another char is called an escape sequence and it is treated as one character. The meanings of the various allowed escape sequences are

    Code:
    Escape  Represents
    \a 	Bell (alert)
    \b 	Backspace
    \f 	Formfeed
    \n 	New line
    \r 	Carriage return
    \t 	Horizontal tab
    \v 	Vertical tab
    \' 	Single quotation mark
    \" 	Double quotation mark
    \\ 	Backslash
    \? 	Literal question mark
    \ ooo 	ASCII character in octal notation
    \x hh 	ASCII character in hexadecimal notation
    \x hhhh 	Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.
    
    For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".
    Note that when you press return, this is translated by the OS into newline which is \n, not \r which is what might have been expected from the above. This is because the program is using 'text mode' for input as opposed to 'binary' mode which you'll come onto later.

    PS How are you learning c?
    Last edited by 2kaud; Sep 10th, 2017 at 05:33 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: how to repeatedly run a program until the user perss yes or y

    For your info, a program that deals with incorrect input could be

    Code:
    int main()
    {
    	char store;
    
    	do {
    		do {
    			printf("Do you have friends, press y for yes and n for no: ");
    			while ((scanf("%c", &store) == 1) && (store == '\n'));
    			for (char tmp; (scanf("%c", &tmp) == 1) && (tmp != '\n'););
    
    		} while ((store != 'n') && (store != 'y') && printf("Invalid input\n"));
    
    	} while ((store == 'y') && printf("Yes I do have friends\n"));
    
    	printf("Loop ended");
    }
    As you will notice, this is more complicated then previous as dealing with input correctly is not actually easy!

    This program will display a warning if chars other than y or n are entered. Also if say yyy or yyn etc are entered then only the first char is considered. The rest are discarded.
    Last edited by 2kaud; Sep 10th, 2017 at 12:18 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width