简体   繁体   English

将值从函数返回到while循环正在测试的值。 C ++

[英]Returning value from function into a value being tested by a while loop. C++

I am creating a basic calculator program using constructors by making classes in other ".cpp" files and calling them in header files. 我通过在其他“ .cpp”文件中创建类并在头文件中调用它们来使用构造函数创建一个基本的计算器程序。 The program worked perfectly fine the way I wrote it. 该程序以我编写它的方式运行良好。 I decided to then add a question at the end asking if the user would want to continue by pressing "c" or "C". 我决定在最后添加一个问题,询问用户是否要通过按“ c”或“ C”继续。 I created a function to test for this. 我创建了一个函数来对此进行测试。 I am able to return two different values based on if c was pressed or if it was not. 我能够根据是否按了c返回两个不同的值。 How do I assign that value to "x" which is being tested in the while loop? 如何将该值分配给在while循环中测试的“ x”? I want this to either end the program or continue it. 我希望它结束​​程序或继续执行。

I did not include the header files or ".cpp" files necessary for the constructors, but I don't think they are necessary to find how to set x to the returned value of the function. 我没有包含构造函数所需的头文件或“ .cpp”文件,但我认为它们对于找到如何将x设置为函数的返回值不是必需的。

#include <iostream>
#include "Add.h"
#include "Sub.h"
#include "Mult.h"
#include "Div.h"

using namespace std;

int cont(char input);

int main()
{
    char operatr;
    int x = -2;
    char y;

    while (x = 1) {

        cout << "Enter operator (+, -, *, or /): ";
        cin >> operatr;


        if(operatr =='+'){
            Add ob;
            cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
            cin >> y;
            cout << (cont(y));
            x = (cont(y));
        }
        if(operatr== '-'){
            Sub ob;
            cout << "Would you like to continue? (Press \"c\" to continue and any key to     escape.)";
            cin >> y;
            cout << (cont(y));
            x = (cont(y));
        }
        if(operatr=='*'){
            Mult ob;
            cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
            cin >> y;
            cout << (cont(y));
            x = (cont(y));
        }
        if(operatr=='/'){
            Div ob;
            cout << "Would you like to continue? (Press \"c\" to continue and any key to escape.)";
            cin >> y;
            cout << (cont(y));
            x = (cont(y));
        }
    }
    return (0);
}

int cont(char input) {
    int a;
    switch(input) {
        case 'c':
            a = -2;
            break;

        case 'C':
            a = -2;
            break;

        default:
            a = 0;
            break;
     }
return(a);
}

Couple of issues: 几个问题:

  1. You're testing x being equal 1 in the loop but using -2 in your conf(int) method. 您正在循环中测试x等于1,但在conf(int)方法中使用-2。
  2. Your while loop is actually not testing x bit "assigning" 1 to x. 您的while循环实际上并未测试x位“将1分配给x”。

You meant to do: 您打算这样做:

while (x == -2)

You could also.just save on method calls to #cont with: 您也可以使用以下方法保存对#cont的方法调用:

x = cont(y);
cout << x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM