将 getline() 与 'If' 语句中的预定变量进行比较

  • 本文关键字:变量 比较 语句 getline If c++
  • 更新时间 :
  • 英文 :


首先,我刚开始学习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语句,如果有任何帮助,我们将不胜感激。

谢谢,Nick

所以,我做了一些研究,终于成功了。如果给您带来不便,我深表歉意。我可以看到,这是一个非常活跃和友好的社区。我会确保把它作为"第一道防线"。这是我的最终代码:

#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();
    }
}

最新更新