Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

c++ - C Looping a switch using Y/N options. Choosing N doesn't close the program

I've only started coding C. (freshman here, no experience, only basic html) For some reason, whenever I choose the option 'N', it just creates a new line.

At my second scanf whenever I change my '%s', the result changes. When I use, "%d",after entering a number, it continuously enter "Choose a number between 1-4". If I use "%c",after entering a number, it will skip directly to the loop. Help?

#include <stdio.h>
int main()
{
    int n, ok = 0;
    char input;
    while (ok == 0)
    {
          printf("Choose a number between 1-4
");
          scanf("%d", &n);
          switch (n)
          {
                 case 1:
                      printf("You've chosen number 1");
                      break;
                 case 2:
                      printf("You've chosen number 2");
                      break;
                 case 3:
                      printf("You've chosen number 3");
                      break;
                 case 4:
                      printf("You've chosen number 4");
                      break;
                 default:
                         printf("You have chosen an invalid number");
          }
          printf("
Input again? (Y/N)
");
          scanf("%s", &input);
          if (input=='n'||input=='N')
          {ok++;}
          else if (input=='Y'||input=='y')
          {printf("
");}


    }

 getchar();
 getchar();   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

change

scanf("%s", &input);

to

scanf(" %c", &input);

and get rid of getchar()s at the end. It will work as you wanted. %c specifier reads a char character, but it does not omit whitespace characters. Putting space onto scanf's format will deal with whitespaces and read first non-whitespace char. To be more clear, see what man page says about %c format specifier:

(...) The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

However, if you are really learning C++, stick with cin/cout, rather than scanf/printf's.

Here's how your program would look like if you would replace printf/scanf with cin/cout. If you've done that previously, you wouldn't had that kind of trouble.

#include <iostream>

using namespace std;

int main()
{
    int n, ok = 0;
    char input;
    while (!ok)
    {
        cout << "Choose a number between 1-4
";
        cin >> n;

        switch (n)
        {
            case 1:
                cout << "You've chosen number 1";
                break;
            case 2:
                cout << "You've chosen number 2";
                break;
            case 3:
                cout << "You've chosen number 3";
                break;
            case 4:
                cout << "You've chosen number 4";
                break;
            default:
                cout << "You have chosen an invalid number";
        }
        cout << "
Input again? (Y/N)
";
        cin >> input;

        if (input=='n' || input=='N')
        {
            ok++;
        }
        else if (input=='Y' || input=='y')
        {
            cout << "
";
        }
    }

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...