c++ - 没有正确检测输入

标签 c++

我今天刚刚开始学习 C++,我正在研究基于文本的高级计算器。无论如何,我正在研究指数,但是当我启动我的程序并输入启动指数模式的字符串时,它不会进入指数模式,只会进入常规计算器模式。这是我的代码:

//
//  main.cpp
//  C++ Calculator
//  This is just a basic Calculator Application to be run through the command line.
//  v.0.02 - Second version of calculator, basic text, command line interface, loop.
//  Created by Johnny Carveth on 2013-04-17.
//  Copyright (c) 2013 Johnny Carveth. All rights reserved.
//
#include <math.h>
#include <iostream>
int int1, int2, answer;
bool bValue(true);
std::string oper;
std::string cont;
using namespace std;
std::string typeOfMath;
double a;
double b;
int answerExponent;


int main(int argc, const char * argv[])
{

    // Taking user input, the first number of the calculator, the operator, and second number.   Addition, Substraction, Multiplication, Division
    cout<<"______________________________________________\n";
    cout<<"|Welcome to The ExpCalc! Do you want to do   |\n";
    cout<<"|Exponent Math, or Basic Math(+, -, X, %)    |\n";
    cout<<"|Type in 'B' for basic Math, and'E' for      |\n";
    cout<<"|Exponential Math! Enjoy! (C) John L. Carveth|\n";
    cout<<"|____________________________________________|\n";
    cin>> typeOfMath;
    if(typeOfMath == "Basic" || "basic" || "b" || "B")
    {
        cout << "Hello! Please Type in your first integer!\n";
        cin>> int1;
        cout<<"Great! Now Enter your Operation: ex. *, /, +, -...\n";
        cin>> oper;
        cout<<"Now all we need is the last int!\n";
        cin>> int2;

        if (oper == "+") {
            answer = int1 + int2;
        }
        if (oper == "-") {
            answer = int1 - int2;

        }if (oper == "*") {
            answer = int1 * int2;
        }if (oper == "/") {
            answer = int1 / int2;
        }
        cout<<answer << "\n";
        cout<<"Thanks for Using The ExpCalc!\n";

    }else if(typeOfMath == "Exp" || "E" || "e" || "Exponent"){
        cout<<"Enter the desired Base. Example: 2^3, where 2 is the base.\n";
        cin>> a;
        cout<<"Now what is the desired exponent/power of the base? Ex. 2^3 where 3 is the exponent!\n";
        cin>>b;
        answerExponent = double (pow(a,b));
    } else(cout<<"Wrong String!");
}

仅提供有用的提示,请记住这是我使用 C++ 的第一天。另外,如果有任何帮助,我正在使用 XCode 4!

最佳答案

你可能想看看你的 if 表达式

if(typeOfMath == "Basic"|| "basic"|| "b"|| "B")

|| 之间的每一件事被评估为有条件的。所以尝试这样的事情:

if(typeOfMath == "Basic" || 
   typeOfMath == "basic" ||
   typeOfMath == "b" || 
   typeOfMath =="B") { 
// do basic

else if(typeOfMath == "Exp"|| "E"|| "e"|| "Exponent") 进行同样的修改

关于c++ - 没有正确检测输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16071094/

相关文章:

c++ - 多次调用 std::condition_variable::notify_one() 而不进行上下文切换

c++ - 使用结构传递多个值

python - 如何使用 tensorflow 在 C++ 中训练模型?

c++ - 我想要三个变量 n1、n2、n3 中两个最大变量的平均值。有人能帮我吗。

c++ - 将长方形切成正方形的最少切割次数

c++ - 如何在程序中间等待来自串口的输入

c++ - 如何在 C++ 中从另一个类的方法访问一个类的方法

c++ - 两个类中的循环依赖 - 代码无法编译

c++ - 从资源 ios 中的文件夹加载图像

c++ - 从 __int64 到 size_t 的安全转换