【问题标题】:wrong input will exit the program c++输入错误会退出程序c++
【发布时间】:2016-09-16 06:55:19
【问题描述】:

所以我开始学习 c++ 并编写了这个简单的程序,当用户输入错误的数字时,它会提供重试选项,但是当用户输入任何字符时,它会提供重试选项并直接退出程序为什么会这样? `

#include<unistd.h>
#include<stdio.h>


int main(){


int a;
char b ,c;

start:


    printf("INPUT ONLY NUMBER 1 : ");

    scanf(" %d", &a);

    if(a==1)
    {
        printf( " you entered correctly \n");
        printf("do you want to try again? <Y> <N> \n");
        scanf(" %c", &c);

        if(c=='Y' ||c=='y')

        {
            goto start;
        }

    }
else {
    sleep (1);
    printf("wrong number , do you want to try again? <Y> <N> \n");
    scanf(" %c" , &b);



}

if (b=='Y'||b=='y')
{
sleep(1);
goto start;
}

else
if(b=='n'||b=='N') 
{

sleep(1);
printf("thank you and goodbye");
exit (1);
}
}

`

【问题讨论】:

  • 如果用户输入了错误的数字,它可以正常工作,只有当用户输入一个字符时它才会退出
  • “所以我已经开始学习 c++” - 这看起来像 C,而不是 C++。确定你选对了书?无论如何,:得到一本更好的书! goto 有它的应用程序,但初学者应该从它开始! 1970 年代/80 年代早已不复存在,使用结构化代码!并正确格式化和缩进代码。
  • 使用while循环代替goto语句
  • 但我想知道为什么会这样?
  • 用循环语句重写代码并使用 C++ iostreams,而不是 C 风格。然后再试一次。如果这太复杂,从更简单的开始。另见How to Ask

标签: c++


【解决方案1】:

scanf(" %d")
我敢打赌,您的问题来自%d 之前的那个空间。试试scanf("%d")。稍后相同:scanf("%c") 而不是 scanf(" %c ")

无论如何,您的代码非常脏。这看起来像 C,而不是 C++。
在这里不要想成为一个疯子,但你应该正确缩进并避免goto 无论如何。您的程序结构很简单,while 循环将为您解决问题。您还应该避免显式调用exit(1)exit 主要用于引发提前终止。在您的情况下,您的程序正常结束,您应该使用 main 函数的 return 0 退出。

#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>        //Reauired to use boolean type in C

int main()
{
    bool stop = false;      //boolean type only has two states: true and false. Very useful for loops!
    while(!stop)            //Read as "While we don't need to stop, execute the loop's contents
    {                       //Much easier to read!
        printf("INPUT ONLY NUMBER 1 : ");
        scanf("%d", &a);
        if(a == 1)
        {
            printf("you entered correctly \n");
            printf("do you want to try again? <Y> <N>\n");
            scanf("%c", &c);

            if(c == 'N' || c == 'n')
            {                   //We only need to tell the loop when to stop
                stop = true;    //by setting stop to true 
            }                   //The loop's default behavior is to loop execution of its content
        }
        else
        {
            sleep(1);

            printf("wrong number , do you want to try again? <Y> <N> \n");
            scanf("%c" , &b);

            if(b=='n'|| b=='N') 
            {
                stop = true;    //Same as above
            }

            sleep(1);
        }
    }

    printf("thank you and goodbye");
    return 0;
}

【讨论】:

    【解决方案2】:

    您的 scanf 留下了一些东西,有时您可以将其清理干净,但在您的代码中似乎不起作用。

    看看这个问题会遇到: http://c-faq.com/stdio/stdinflush2.html

    这基本上告诉你,如果你的方法不起作用,那么你应该改变它。 希望对您有所帮助,您在其中一个问题中也询问过,所以:

    #include <iostream>
    
    int main(){
    
        char input;
        char try_again;
    
        do {
            std::cout << "INPUT ONLY NUMBER:";
            std::cin >> input;
            // http://en.cppreference.com/w/cpp/string/byte/isdigit
            // does not return a bool, you can check that >0
            if (std::isdigit(input)) {
                 // do what you want.
                 std::cout << "digit\n";
                 continue;
            }  else {
                // you can as for more imput like:
                std::cout << "Not a number, try again?(y/Y):";
                std::cin >> try_again;
                std::tolower(try_again);
                if (try_again == 'y') {
                    continue; // will start the loop again.
                } else {
                    break; // it will exit the loop.
            }
            }
        } while(true);
    
        return 0;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2017-08-02
      • 2018-12-04
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多