Switch Case始终为默认值

时间:2018-06-06 22:58:56

标签: c++ switch-statement case

我正在尝试制作一个小型操作系统,它可以从一个开关......案例中获得一个微型游戏或一个简单的计算器。但是,无论我给出什么输入(甚至是正确的输入),输出始终是默认值。 我正在使用的编译器(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;

}

3 个答案:

答案 0 :(得分:3)

  1. 'Game'不是有效的字符串
  2. 即使您将"Game"替换为有效字符串,switch也无法使用字符串。
  3. 因此,要么在您的交换机中使用单个字符,要么使用if - else个阻止您通过std::string比较==

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

答案 1 :(得分:2)

使用map to item回调

理想情况下,将项目菜单映射到其各自的操作会更好。 std::map<std::string, std::function<void()>>允许这一点!阅读内联评论以了解其余部分:

#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
}

Demo

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

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

答案 2 :(得分:1)

当变量iFileNum = FreeFile Open strNotesPath For Input As iFileNum While Not EOF(iFileNum) Line Input #iFileNum, strText If Left(strText, 3) = "===" Then strText = "" If lngIndex + 1 <= ActivePresentation.Slides.Count Then lngIndex = lngIndex+1 Set NotesPLC = getPLC(ActivePresentation.Slides(lngIndex)) If Not b_append Then NotesPLC.TextFrame.DeleteText Else If Not NotesPLC Is Nothing Then NotesPLC.TextFrame.TextRange = _ NotesPLC.TextFrame.TextRange & vbCrLf & strText End If string时,您正在提示用户answer,将提示更改为charc等字符,从而使其成为g更方便,因此您可以在switch / 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;
...