我正在开发一个项目,我必须创建一个基于用户输入工作的简单程序。我已经使用了基本的计算器,但是我无法使用if / else if语句。基本上,如果用户键入“Addition”,我希望程序说“...我会帮助你添加!”等等,用户是否说“减法”,“分区”和“乘法”
我是新手,所以这已经花了我几个小时,不是要你为我做这件事,而是指出我的错误并提出建议,以便我可以从中学到它会很棒。
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;
REST OF CODE
//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 ;
}
答案 0 :(得分:2)
这个代码完全没错,你需要先正确了解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;
}
答案 1 :(得分:1)
首先,不要使用char
数组,而是使用std::string
。
其次,if-else
语句有语法错误。
if-else
语句的基本结构就像
if(condition)
{
//code
}
else
if(condition)
{
//code
}