首先,我刚刚开始学习C ++,所以如果我给这个标题命名不正确,或者我不知道这意味着什么,请原谅我。如果您确实回答了这个问题,请不要使用我不知道的所有疯狂词汇。谢谢:)。
我目前(正在尝试)制作一个计算器,可以让您选择加减,乘或除。我知道这似乎不太有用,但是在我所做的事情中,我学到了很多东西。
这是我现在拥有的代码:
#include <iostream>
#include <string>
using namespace std;
int add()
{
int x;
int y;
int sum;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
sum = x + y;
cout << x << " + " << y << " = " << sum << endl;
}
int subtract()
{
int x;
int y;
int difference;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
difference = x - y;
cout << x << " - " << y << " = " << difference << endl;
}
int multiply()
{
int x;
int y;
int product;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
product = x * y;
cout << x << " * " << y << " = " << product << endl;
}
int divide()
{
int x;
int y;
int quotient;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
quotient = x / y;
cout << x << " / " << y << " = " << quotient << endl;
}
int main()
{
int Add, Subtract, Multiply, Divide;
string str;
cout << "Add, Subtract, Multiply, Divide?: ";
getline(cin, str);
if (str == (cin, add));
cout << "Starting process " << str << "..." << endl;
add();
getline(cin, str);
if (str == (cin, "subtract"));
cout << "Starting process " << str << "..." << endl;
subtract();
}
我遇到的问题在main()
部分中。我希望程序通过getline
读取用户输入,然后将其与预定变量进行比较。然后,我尝试创建一个if语句,如果用户输入例如是“ add”,则if语句将读取该内容,然后运行我的add
函数。
我已经深入到getline
部分,但是如您所见,if语句什么也不做。我以为可能是这样的:
if (str == "add")
cout << "Starting Process" << str << "..." << endl;
add();
让我失望的是一些错误。我不确定如何构造if
语句,我们将不胜感激。
谢谢, 尼克
答案 0 :(得分:0)
因此,我进行了一些研究,最终使它起作用。造成您的不便,敬请见谅。不过,我可以看到,这是一个非常活跃和友善的社区。我将确保将其作为“第一道防线”再次提及。这是我的最终代码:
#include <iostream>
using namespace std;
void add()
{
int x;
int y;
int sum;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
sum = x + y;
cout << x << " + " << y << " = " << sum << endl;
}
void subtract()
{
int x;
int y;
int difference;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
difference = x - y;
cout << x << " - " << y << " = " << difference << endl;
}
void multiply()
{
int x;
int y;
int product;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
product = x * y;
cout << x << " * " << y << " = " << product << endl;
}
void divide()
{
int x;
int y;
int quotient;
cout << "Enter the value for x: ";
cin >> x;
cout << "Enter the value for y: ";
cin >> y;
quotient = x / y;
cout << x << " / " << y << " = " << quotient << endl;
}
int main()
{
int Add, Subtract, Multiply, Divide;
string str;
cout << "Add, Subtract, Multiply, Divide? (Letters are case sensitive): ";
getline(cin, str);
if (str == "Add")
{
cout << "Starting Process " << str << "..." << endl;
add();
}
//getline(cin, str);
if (str == "Subtract")
{
cout << "Starting process " << str << "..." << endl;
subtract();
}
//getline(cin, str);
if (str == "Multiply")
{
cout << "Starting process " << str << "..." << endl;
multiply();
}
//getline(cin, str);
if (str == "Divide")
{
cout << "Starting process " << str << "..." << endl;
divide();
}
}