是或否程序,用于请求对应用程序进行评分的权限

Yes or No program for asking permission to rate an app

我是编码方面的新手(大约 2 周),我一直在努力编写程序来练习,所以不要对我太粗鲁:D。我今天尝试的是制作一个需要插入一个字母的程序,因为我一直在玩int函数,所以我想稍微改变一下。 所以我一直在努力让它工作,虽然它执行了程序,但当你输入一个字母时,它只是关闭了程序。感谢任何帮助,如果你能解释我犯的错误。谢谢大家!! :D

#include <iostream>

int main()
{
    
    char Answer;
    char responsetype;

    std::cout<<"Please rate this app\n";
    std::cin>> Answer;
while (responsetype=false)
        if (Answer=='Y')
        {
            responsetype=true;
            std::cout<< "Thanks for rating :D\n";
            }
            else if (Answer/='Y')
            {
                if (Answer=='N')
                {
                    responsetype=true;
                    std::cout<< "awwww... Okay then.. :(\n";
                }
                    else(true);
                        responsetype=false;
                        std::cout<<"You need to answer Y or N :/\n";
            }
            
                
return 0;

我更改的内容:

  1. 将响应类型从字符更改为布尔值

  2. 将整体结构更改为 switch case 而不是 if 语句

  3. 在最后添加了getchar(),所以程序不会在最后自动关闭

    #include <iostream>
    
    int main() {
    
    char Answer;
    bool responsetype = false;
    
    std::cout<<"Please rate this app\n";
    std::cin>> Answer;
    
    while (!responsetype){
       switch(Answer)
       {
         case 'Y':
           responsetype=true;
           std::cout<< "Thanks for rating :D\n";
           break;
         case 'N':
           responsetype=true;
           std::cout<< "awwww... Okay then.. :(\n";
           break;
         default:
           responsetype=false;
           std::cout<<"You need to answer Y or N :/\n";
       }
    }
    
    getchar(); // Asks for another input before closing console          
    return 0;
    }
    

您的代码有几个问题,所以我将尝试列出我发现的问题。

  1. while(responsetype=false)
    似乎是您的问题的原因,您在此处将 false 分配给 response 然后检查始终为 false 的 responsetype。
  2. 您需要 std::cin>>Answer; 在您的 for 循环中,以便用户可以实际更改他们的答案。
  3. if (Answer/='Y') => if (Answer!='Y')
  4. else(true);
    这没有任何作用,您可以将它们一起删除,但是您最初想要的可能是 else{...}