1

I am taking a C programming class at my local college and we're starting on some of the bigger programs. I am breaking down the program into pieces so it's easier to manage, currently I'm trying to find away to use a yes/no loop for getting input from a user for a package deal and then verifying that the deal the user chose is what they actually wanted. If it wasn't what the user wanted it would repeat itself until the user input 'Y' and then go from there. If I find a way for it to do the loop the program just displays all of the printf functions over and over again with no exit to the loop or more input from the user. Any ideas?

#include <stdio.h>

int main()
{
char package_choice;
char package_verify;

do
{
printf("Which package would you like to select? ");
scanf("%c", &package_choice);

printf("You chose %c, correct?\n", package_choice);
printf("Y/N\n");

scanf(" %c", &package_verify);
}while(package_verify != 'Y' && package_verify == 'N');

printf("You chose package %c.", package_choice);
}

I've tried a number of retyping how the do...while loop works but I never get the result I am wanting. Is there another way to go about this or am I missing something?

jtward17
  • 19
  • 5
  • 1
    `while(package_verify != 'Y' && package_verify == 'N')` why test not `Y` and is `N`? Is `N` ever `Y`? What result do you get? What result are you wanting? – Elliott Frisch Mar 24 '23 at 00:08
  • Question: When entering the verification, do you type 'Y' or 'y'... Case matters... – Fe2O3 Mar 24 '23 at 00:12
  • If I type in a Y I get: Which package would you like to select? a You chose a, correct? Y/N Y You chose package a. – jtward17 Mar 24 '23 at 00:14
  • But if I type in a N (to theoretically get it to redo the loop): Which package would you like to select? a You chose a, correct? Y/N N Which package would you like to select? You chose , correct? Y/N Essentially just continuing the loop without anymore user input – jtward17 Mar 24 '23 at 00:14
  • You used the "trick" of a single space in the 2nd `scanf()`... Add that same "trick" to the first `scanf()` and get back to us... (That SP discards the LF that is left waiting in the input buffer...) – Fe2O3 Mar 24 '23 at 00:23
  • 1
    That worked for me. The answer below also worked but I essentially just copied and pasted it. Do you mind explaining why the space is needed before the %c? The only reason I used it was because I had seen it in some other programs and just tried to replicate the same information. Thank you for the help! I've been fighting with this for the past 3 hours lol – jtward17 Mar 24 '23 at 00:25
  • https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer – pm100 Mar 24 '23 at 00:28
  • Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – pm100 Mar 24 '23 at 00:28

1 Answers1

3
  1. I would start with proper formating. It makes everything easier
  2. The first scanf has to get rid of newline characters as well (it is not needed on the first iteration but required on the next ones)
  3. It is enough to check for Y
  4. User may enter lower case letter as well
int main(void)
{
    char package_choice;
    char package_verify;

    do
    {
        printf("Which package would you like to select? ");
        scanf(" %c", &package_choice);

        printf("You chose %c, correct?\n", package_choice);
        printf("Y/N\n");

        scanf(" %c", &package_verify);
    }while(toupper((unsigned char)package_verify) != 'Y');

    printf("You chose package %c.\n", package_choice);
}

https://godbolt.org/z/jffhnbG7W

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Maybe I'm missing something, partially because I have never seen "toupper" before, but why would the main need (void) and the char be unsigned? I haven't made any short programs with the use of a (void) function so I'm a little confused as to when or why you would use it. I appreciate any feedback! – jtward17 Mar 24 '23 at 00:21
  • @jtward17 because it is a valid form of the `main` function if it does not take any arguments – 0___________ Mar 24 '23 at 00:31
  • @jtward17 because in most versions of the C language, `main()` didn't actually say what you meant to say ("this function takes no arguments") while `main(void)` does. I'd like to apologize to you on behalf of humanity that this design quirk of C has reached you all the way from the 1980s. Luckily, this was finally fixed in the 2023 version of C. – mtraceur Mar 24 '23 at 01:18
  • @jtward17 For what it's worth, I wouldn't bother teaching a new student `foo()` vs `foo(void)`, except in a little "fun history tidbit" or "here's an example of a problem in language/interface design, here's why it's bad, and here's some principles/heuristics you can use to help you not make similar mistakes" . I say this as someone who spent years thinking that every C programmer has a duty to know this... Now I realize that your learning is better served by learning the fundamentals, not memorizing workarounds for language-specific historical accidents like this. – mtraceur Mar 24 '23 at 01:34