c++ - 是或否程序,用于请求对应用程序进行评级的权限

标签 c++ char cin

我在编码方面很陌生(大约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;

最佳答案

我已经改变的事情:

  • 将响应类型从char更改为boolean
  • 将整体结构更改为切换用例,而不是if语句
  • 最后添加了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;
    }
    
  • 关于c++ - 是或否程序,用于请求对应用程序进行评级的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62614887/

    相关文章:

    c++ - c++问题上的c样式字符串

    c++ - cin 对于一个 int 输入一个 char 会导致应该检查输入的循环变得疯狂

    c++ - 我可以检查输入的值是 C++ 中的字符串还是整数吗

    c++ - 从模板函数调用的模板类的模板成员函数

    c++ - 如何在模板类的嵌套类中提供友元运算符的定义?

    c++ - Qt Windows 上的 dlib。程序意外结束

    c++ - 为什么从字符串常量到 'char*' 的转换在 C 中有效但在 C++ 中无效

    c++ - 比 strncpy 更好的复制 char 数组部分的方法

    c++ - cin.get() 和 cin.getline() 的区别

    c++ - 我无法在 Debian(Ubuntu) 中运行在 Redhat(Centos) 中运行的 C++ 程序