Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 开关箱始终处于默认状态_C++_Switch Statement_Case - Fatal编程技术网

C++ 开关箱始终处于默认状态

C++ 开关箱始终处于默认状态,c++,switch-statement,case,C++,Switch Statement,Case,我正在尝试制作一个小的操作系统,它可以从一个开关盒中获取响应,然后转到一个微型游戏或一个简单的计算器。然而,无论我给出什么输入(即使是正确的输入),输出总是默认的。 我使用的编译器(Microsoft Visual Studio;这可能是问题所在)没有给我任何错误,我也找不到或想不出任何错误。你们中的一些人真的很擅长这个,对我的问题有什么答案吗 #include "stdafx.h" #include <iostream> #include <limits> using

我正在尝试制作一个小的操作系统,它可以从一个开关盒中获取响应,然后转到一个微型游戏或一个简单的计算器。然而,无论我给出什么输入(即使是正确的输入),输出总是默认的。 我使用的编译器(Microsoft Visual Studio;这可能是问题所在)没有给我任何错误,我也找不到或想不出任何错误。你们中的一些人真的很擅长这个,对我的问题有什么答案吗

#include "stdafx.h"
#include <iostream>
#include <limits>

using namespace std;

int calc() {

char op;
float num1, num2;

cout << "Enter operation:";
cin >> op;

cout << "Enter two numbers:";
cin >> num1 >> num2;

switch (op)
{
case '+':
    cout << num1 + num2;
    break;

case '-':
    cout << num1 - num2;
    break;

case '*':
    cout << num1 * num2;
    break;

case '/':
    cout << num1 / num2;
    break;

default:
    cout << "That is not an operation";
    break;
}

return 0;
};

int main()
{
char answer;

cout << "Welcome to the FR Operating System. \n";
cout << "If you want to go to the calculator, type in 'Calc'. \n";
cout << "If you want to go to the game, type in 'Game'. \n";

cin >> answer;

switch (answer) {
case 'Calc' || 'calc':
    cout << "Welcome to the calculator. \n";
    break;

case 'Game':
    cout << "Welcome to our game, 'A Day in the Life'. \n";
    break;

default:
    cout << "That is an invalid answer. This has caused the system to crash. \n";
    break;
}

atexit([] { system("PAUSE"); });

return 0;

}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int calc(){
char op;
浮点数num1,num2;
cout>op;
cout>num1>>num2;
开关(op)
{
格“+”:
库特
  • “游戏”
    不是有效字符串
  • 即使将其替换为有效字符串
    “Game”
    开关也不能处理字符串
  • 因此,要么在交换机中使用单字符,要么使用
    if
    -
    else
    块,通过
    =
    比较
    std::string
    s

    std::string answer;
    cin >> answer;
    if (answer == "Calc" || answer == "calc")
        //...
    else if (answer == "Game")
        //...
    else
        // invalid 
    
  • “游戏”
    不是有效字符串
  • 即使将其替换为有效字符串
    “Game”
    开关也不能处理字符串
  • 因此,要么在交换机中使用单字符,要么使用
    if
    -
    else
    块,通过
    =
    比较
    std::string
    s

    std::string answer;
    cin >> answer;
    if (answer == "Calc" || answer == "calc")
        //...
    else if (answer == "Game")
        //...
    else
        // invalid 
    

    当变量
    answer
    char
    时,提示用户输入
    string
    ,将提示更改为
    c
    g
    这样更方便,因此您可以在
    开关中使用和枚举字符

    int main()
    {
    char answer;
    
    cout << "Welcome to the FR Operating System. \n";
    cout << "If you want to go to the calculator, type in 'c'. \n";
    cout << "If you want to go to the game, type in 'g'. \n";
    cin >> answer;
    switch (answer) {
    case 'c':
    case 'C':
        cout << "Welcome to the calculator. \n";
        break;
    
    case 'g':
    case 'G':
        cout << "Welcome to our game, 'A Day in the Life'. \n";
        break;
    ...
    
    intmain()
    {
    答案;
    
    cout当变量
    answer
    char
    时,提示用户输入
    字符串
    ,将提示更改为
    c
    g
    这样更方便,因此您可以在
    开关中使用和枚举字符
    /code>case
    语句:

    int main()
    {
    char answer;
    
    cout << "Welcome to the FR Operating System. \n";
    cout << "If you want to go to the calculator, type in 'c'. \n";
    cout << "If you want to go to the game, type in 'g'. \n";
    cin >> answer;
    switch (answer) {
    case 'c':
    case 'C':
        cout << "Welcome to the calculator. \n";
        break;
    
    case 'g':
    case 'G':
        cout << "Welcome to our game, 'A Day in the Life'. \n";
        break;
    ...
    
    intmain()
    {
    答案;
    无法使用映射到项回调
    理想情况下,最好将项目菜单映射到其各自的操作。
    std::map
    正好允许这样做!阅读内联注释以了解其余内容:

    #include <string>
    #include <map>
    #include <iostream>
    #include <functional>
    
    int main()
    {
        std::map<std::string, std::function<void()>> menu_items;
        menu_items.emplace("calc", [](){std::cout << "calculate chosen\n";}); //use lambdas to spare boilerplate
        menu_items.emplace("game", [](){std::cout << "game is chosen\n";});
    
        std::string chosen_item;
        std::cin >> chosen_item;
        auto item = menu_items.find(chosen_item); //search by the string
        if (item == menu_items.end()) //item was not found in the list
            std::cout << "invalid item is chosen\n";
        else
            item->second(); //execute the stored function
    }
    
    #包括
    #包括
    #包括
    #包括
    int main()
    {
    标准::地图菜单项;
    菜单项。放置(“计算”,[](){std::cout selected_项;
    自动项=菜单项。查找(选定项);//按字符串搜索
    if(item==menu_items.end())//在列表中找不到项
    std::cout second();//执行存储的函数
    }
    

    根据您的使用情况,您可能希望将
    void*()
    用于
    std::function
    ,将
    std::unordered_map
    用于
    std::map
    。但对于您的使用情况,这似乎并不重要

    此外,您可能希望对输入进行规范化,例如将字符串小写,或执行其他一些规范化操作。由于这不是代码中对性能敏感的部分,因此我认为
    std::function
    std::map
    的开销在这种情况下无关紧要。

    使用map-to-item回调 理想情况下,最好将项目菜单映射到其各自的操作。
    std::map
    正好允许这样做!阅读内联注释以了解其余内容:

    #include <string>
    #include <map>
    #include <iostream>
    #include <functional>
    
    int main()
    {
        std::map<std::string, std::function<void()>> menu_items;
        menu_items.emplace("calc", [](){std::cout << "calculate chosen\n";}); //use lambdas to spare boilerplate
        menu_items.emplace("game", [](){std::cout << "game is chosen\n";});
    
        std::string chosen_item;
        std::cin >> chosen_item;
        auto item = menu_items.find(chosen_item); //search by the string
        if (item == menu_items.end()) //item was not found in the list
            std::cout << "invalid item is chosen\n";
        else
            item->second(); //execute the stored function
    }
    
    #包括
    #包括
    #包括
    #包括
    int main()
    {
    标准::地图菜单项;
    菜单项。放置(“计算”,[](){std::cout selected_项;
    自动项=菜单项。查找(选定项);//按字符串搜索
    if(item==menu_items.end())//在列表中找不到项
    std::cout second();//执行存储的函数
    }
    

    根据您的使用情况,您可能希望将
    void*()
    用于
    std::function
    ,将
    std::unordered_map
    用于
    std::map
    。但对于您的使用情况,这似乎并不重要


    此外,您可能希望对输入进行规范化,例如将字符串小写,或执行其他一些规范化。由于这不是代码中对性能敏感的部分,因此我认为
    std::function
    std::map
    的开销在这种情况下无关紧要。

    case'Calc'|'Calc':
    不是有效的
    case
    statement.对于要比较的每个值,都需要单独的
    case
    语句。但是不能将字符串与
    case
    语句一起使用。
    'Calc'
    'Calc'
    'Game'
    ,它们不被视为字符串,而是被视为多字节字符,因为您使用的是单引号而不是双引号-引述出于好奇:你是认真开发操作系统的吗?Visual Studio能够编译一个操作系统对我来说是一件新鲜事:D我想你说的是控制台应用程序而不是操作系统?:)@sebrockm我只是做一个小的来帮我学习。@McOcelot嗯,我看到它很小:)但是你真的在谈论一个真正的带引导扇区的操作系统吗?我从来没有涉及过这个话题,这就是为什么我问
    case'Calc'| |'Calc':
    不是一个有效的
    case
    语句。你需要对每个要比较的值使用单独的
    case
    语句。但是你不能将字符串与
    case
    语句一起使用.
    “Calc”
    “Calc”
    “Game”
    ,它们不被视为字符串,而是被视为多字节字符,因为您使用的是单引号而不是双引号。出于好奇:开发操作系统是认真的吗?Visual Studio可以编译一个对我来说是新的:D我认为您的意思是反对ole应用程序而不是操作系统?:)@sebrockm我只是做一个小的来帮助我学习。@McOcelot好吧,我知道它很小:)但是你真的在谈论一个真正的带引导扇区的操作系统等等吗?我从来没有接触过这个话题,这就是为什么我问如果我理解正确,OP想要处理这两个“Calc”| |“Calc”的情况离子正确或相同。您的代码是什么