小编skr*_*_CZ的帖子

使用字符串作为条件时减少 else if 语句的方法

我正在制作一个类似终端的程序(用于计算货币),并使用自定义命令作为输入,但我遇到了问题。每次执行新命令时,我都必须添加新else if语句。这不是问题,但对于类似终端的程序来说可能有很多命令。

这是我的代码:

#include <iostream>
#include <string>
#include <Windows.h>
#include <math.h>

float user_balance = 0.0f;
float eur_in_czk = 24.0f;   //ammount of czk in single euro
std::string currency = "czk";
bool czk_to_eur_enabled = true;
bool eur_to_czk_enabled = false;

//------------------START method definition---------------------------------------------------------
void czk_to_eur()
{
    if (czk_to_eur_enabled) //to prevent using twice in a row
    {
        user_balance /= eur_in_czk;
        user_balance = floorf(user_balance * 100) / 100;    //limit to two decimal numbers
        currency = "eur";
        czk_to_eur_enabled = false;
        eur_to_czk_enabled = true;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ string if-statement

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

标签 统计

c++ ×1

if-statement ×1

string ×1