ç循环使用Y / N选项开关。选择N不关闭程序 [英] C Looping a switch using Y/N options. Choosing N doesn't close the program

查看:347
本文介绍了ç循环使用Y / N选项开关。选择N不关闭程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只开始编码C.(这里的新生,没有经验,只有基本的HTML)
出于某种原因,每当我选择的选项'N',它只是创建一个新的行。

在我的第二个 scanf函数每当我改变我的'%s'的,结果被更改。
当我使用,%D,输入一个数字,它不断地进入后选择1-4之间的一个数。如果我使用%C,输入号码后,就会直接跳到循环。帮助?

 的#include<&stdio.h中GT;
诠释的main()
{
    INT N,确定= 0;
    字符输入;
    而(OK == 0)
    {
          的printf(请选择1-4 \\ n之间的数字);
          scanf函数(%d个,&安培; N);
          开关(N)
          {
                 情况1:
                      的printf(你选择了1号);
                      打破;
                 案例2:
                      的printf(你选择了2号);
                      打破;
                 案例3:
                      的printf(你选择了3号);
                      打破;
                 情况4:
                      的printf(你选择了4号);
                      打破;
                 默认:
                         的printf(你选择了一个无效的号码);
          }
          的printf(\\ nInput试(Y / N)\\ n吗?);
          scanf函数(%S,&安培;输入);
          如果(输入=='N'||输入=='N')
          【OK ++;}
          否则,如果(输入=='Y'||输入=='Y')
          {printf的(\\ n);}
    } 的getchar();
 的getchar();
}


解决方案

改变

  scanf函数(%S,&安培;输入);

  scanf函数(%C,&安培;输入);

和摆脱中的getchar()■在年底。这将作为你想要的。 %C 说明读取一个char字符,但它不忽略空格字符。把空间到scanf函数的格式将处理空格和读取第一个非空格字符。为了更清楚,看什么人说页关于%C 格式说明:


  

(...)的前导空格通常的跳跃是pssed燮$ P $。跳过
  白空间的第一部分,格式使用一个明确的空间。
  结果


不过,如果你真的学习C ++,坚持与CIN / COUT,而不是scanf函数/ printf的的。

下面的程序将如何看就像如果你将取代的printf / scanf函数与CIN / COUT。如果你这样做previously,你不会有那样的麻烦。

 的#include<&iostream的GT;使用命名空间std;诠释的main()
{
    INT N,确定= 0;
    字符输入;
    而(OK!)
    {
        COUT<< 选择1-4 \\ n之间的数字;
        CIN>> N;        开关(N)
        {
            情况1:
                COUT<< 您已选择号码1;
                打破;
            案例2:
                COUT<< 您已选择号码2;
                打破;
            案例3:
                COUT<< 您已选择3号;
                打破;
            情况4:
                COUT<< 你选择了4号
                打破;
            默认:
                COUT<< 你选择了一个无效号码;
        }
        COUT<< ?\\ nInput试(Y / N)\\ n;
        CIN>>输入;        如果(输入=='N'||输入=='N')
        {
            OK ++;
        }
        否则,如果(输入=='Y'||输入=='Y')
        {
            COUT<< \\ n;
        }
    }    返回0;
}

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\n");
          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("\nInput again? (Y/N)\n");
          scanf("%s", &input);
          if (input=='n'||input=='N')
          {ok++;}
          else if (input=='Y'||input=='y')
          {printf("\n");}


    }

 getchar();
 getchar();   
}

解决方案

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\n";
        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 << "\nInput again? (Y/N)\n";
        cin >> input;

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

    return 0;
}

这篇关于ç循环使用Y / N选项开关。选择N不关闭程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆