【发布时间】: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