小编Tah*_*hir的帖子

为什么不在 switch 语句中处理字符?C++

我试图通过使用 char +、-、/、* 来获取和处理用户决定,为什么 switch 语句忽略它们,因此我在我的代码中看不到任何错误。

#include <iostream>
using namespace std;

void optionMenu();
double UserOutput(int);

int main ()
{
    int UserChoice;

    optionMenu();
    cout << " Choice: ";
    cin >> UserChoice;
    UserOutput(UserChoice);


    return 0;
}
void optionMenu()
{
    cout << " Select your choice" << '\n';
    cout << " + for Addition" << '\n';
    cout << " - for Subtraction" << '\n';
    cout << " / for Division" << '\n';
    cout << " * for Multiplication" << '\n';
}
double UserOutput …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
107
查看次数

为什么 Switch 语句会被跳过?

我想了解为什么我的代码中跳过了 switch 语句?编译器只是没有读取 switch 语句中的数据并返回 0。我一定是做错了什么,但如果这是一个原因,我无法指出错误!

代码:

#include <iostream>
using namespace std;

void mainMenu();
void ResultUsr(int);

int main()
{
    int userChoice;
    int ResultUsrInput;

    mainMenu();

    cout << "Choice: ";
    cin >> userChoice;

    ResultUsr(ResultUsrInput);
    return 0;
}
void mainMenu()
{
    cout << "Enter your Choice for calculation" << '\n';
    cout << "1 for Addition" << '\n';
    cout << "2 for Subtraction" << '\n';
}
void ResultUsr (int ResultUsrInput)
{
    int value1, value2;
    switch (ResultUsrInput)
    {
    case 1:
        cout << "Enter Your first …
Run Code Online (Sandbox Code Playgroud)

c++

-1
推荐指数
2
解决办法
693
查看次数

标签 统计

c++ ×2