【问题标题】:Using if/else if in my calculator program在我的计算器程序中使用 if/else if
【发布时间】:2017-03-19 21:44:19
【问题描述】:

我正在做一个项目,我必须创建一个基于用户输入工作的简单程序。我已经使用了一个基本的计算器,但是我的 if/else if 语句无法正常工作。基本上,如果用户输入“加法”,我希望程序说“......我会帮你加法!”,等等用户是否说“减法”、“除法”和“乘法” .

我是新手,所以这已经花了我好几个小时,不是指望你为我做,而是指出我的错误并提出建议,以便我可以从中吸取教训。

TIA。

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;

//user inputs what he needs help with/program output
char Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
char inpsum[20];
cin >> inpsum;
char output;
if (inpsum == "Addition")
{
    cout << "Great! I'll help you with addition!" << endl;
}
else if (inpsum == "Subtraction")
{
    cout << "Great! I'll help you with subtraction!" << endl;
}
else if (inpsum == "Division")
{
    cout << "Great! I'll help you with division!" << endl;
}

else if (inpsum == "Multiplication")
{
    cout << "Great! I'll help you with multiplication!" << endl;
}


return 0; 

其余代码

//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}

//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}

//division function
void Div()
{
    float div1, div2;
    cout << "Please enter two values you want divided" << endl;
    cin >> div1;
    cin >> div2;
    cout << "The answer is: " << (div1 / div2) << endl;
}

//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}



int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();

    return 0 ; 
}

【问题讨论】:

  • 那段代码中几乎每一行都是错误的。你从哪里学的这些东西?
  • 互联网。在我尝试为不同的结果添加 if/else if 之前,它正在工作,哈哈。试图修复自己,但不能,所以我在这里
  • 你不会从互联网资源中学习 C++;你需要一本好的教科书。
  • 我同意尼尔的观点。您可以找到推荐书籍列表here
  • 为这个 lmao 买书有点太晚了,但谢谢

标签: c++ visual-studio if-statement


【解决方案1】:

这段代码全错了你需要先正确了解C++,这里是更正的代码

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include<string>
using namespace std;

//addition function
float Add(float add1, float add2)
{

    return (add1 + add2);
}
//subtraction function
float Subt(float subt1, float subt2) {

    return (subt1 - subt2);
}

//division function
float Div(float div1, float div2)
{

    return (div1 / div2);
}
//multiplication function
float Mult(float mult1, float mult2)
{

    return (mult1 * mult2);
}
void input(float &num1, float &num2)
{
    cout << "\nEnter First Number : ";
    cin >> num1;
    cout << "Enter Second Number : ";
    cin >> num2;
}
//user inputs what he needs help with/program output
void Inpsum()
{
    cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
    float num1;
    float num2;
    string inpsum;
    cin >> inpsum;
    if (inpsum == "adding")
    { //if user enters "adding"
        cout << "Great!, I will help you with " << (inpsum) << endl;
        input(num1, num2);
        cout << "\nAnser Is " << Add(num1, num2);
    }//then output = "...i will help with adding"
    else if (inpsum == "subtraction") //otherwise, if user enters "subtraction"
    {
        cout << "Great!, I will help you with " << (inpsum) << endl; //then output = "...i will help with subtraction"
        input(num1, num2);
        cout << "\nAnser Is " << Subt(num1, num2);
    }
    else if (inpsum == "division") //if user enters "division" 
    {

        cout << "Great!, I will help you with " << (inpsum) << endl; ////then output = "...i will help with division
        input(num1, num2);
        cout << "\nAnser Is " << Div(num1, num2);
    }
    else if (inpsum == "multiplication") //if user enters "muliplication"
    {
        cout << "Great, I will help you with " << (inpsum) << endl; ////then output = "...i will help with multiplication"
        input(num1, num2);
        cout << "\nAnser Is " << Mult(num1, num2);
    }
    else
    {
        cout << "Enter A Correct Mathematical Operation";
    }
}
    int main()
    {
        Inpsum(); //user inputs what they want help with
        cout<<endl;
        system("pause");
        return 0;
    }

【讨论】:

  • 谢谢老哥,但我不能让它运行? prntscr.com/em0l2i - prntscr.com/em0lhy
  • 以上截图。 “系统找不到指定的文件”和两个未解决的外部错误。
  • 我编辑了代码,但是对于这个错误,您应该更改您的项目名称或在新的 c++ 项目中尝试此代码。您的 Visual Studio 出现 LNK 错误,代码没有问题。
【解决方案2】:

首先,不要使用char数组,而是使用std::string

其次,if-else 语句有语法错误。

if-else 语句的基本结构是这样的

if(condition)
{
   //code
}
else
if(condition)
{
  //code
}

更多关于if-else statements in C++

【讨论】:

  • 好吧,std::string。
  • 我已经更新了 OP,但它完全忽略了“太好了!我会帮你做____”。 (我可能又做错了什么)我在尝试使用 std::string 时也遇到了 cout 错误?
猜你喜欢
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多