如何将多个函数的输入发送到C++中的一个函数?

如何将多个函数的输入发送到C++中的一个函数?,c++,C++,作为学校作业的一部分,我需要构建一个模块化计算器,其中至少有四个模块getData、getInteger、processData、displayData对两个整数进行加/减/乘/除/模运算 我很难把这件事放在一起,我认为这主要是因为我很难理解函数间调用是如何工作的,例如,一个函数向另一个函数发送信息 我有一个getInteger函数从用户那里获取整数输入,我使用的是processdataintA,intB;将其发送到processDataint,int函数;但是我的getDataint函数还需要

作为学校作业的一部分,我需要构建一个模块化计算器,其中至少有四个模块getData、getInteger、processData、displayData对两个整数进行加/减/乘/除/模运算

我很难把这件事放在一起,我认为这主要是因为我很难理解函数间调用是如何工作的,例如,一个函数向另一个函数发送信息

我有一个getInteger函数从用户那里获取整数输入,我使用的是processdataintA,intB;将其发送到processDataint,int函数;但是我的getDataint函数还需要向processData发送一个整数输入-但是processDataselect无效,因为它没有足够的参数。我真的不明白这意味着什么

这可能有点让人困惑,所以我在这里得到了整个未完成/在制品/实际上不起作用的程序:

//calculator program
//4 modules required: getData, getInteger, processData, displayData
#include <iostream> //To input/output to the display (I think)
#include <conio.h>  //For getch() at end of program
using namespace std;

//prototypes
void getInteger(int, int); 
void getData(int); 
void processData(int, int); 
void displayData(); // haven't added anything yet

int main(){


  //prevents window from immediately closing
  getch();     
  return 0;
}

void getInteger(int, int) {
    int intA, intB;
    cout << "Please enter integer one: " << endl;
        cin >> intA;
    cout << "Please enter integer two: " << endl;
        cin >> intB;
    processData(intA, intB); //sends info to processData function
}

void getData(int) {
    int select;
    cout << "Available Functions" << endl;
    cout << "1. Addition (+)" << endl;
    cout << "2. Subtraction (-)" << endl;
    cout << "3. Multiplication (*)" << endl;
    cout << "4. Division (/)" << endl;
    cout << "5. Modulus (%)" << endl;
    cout << "Please type your selection (1-5): " << endl;
        cin >> select;
        if (select > 5 || select < 1) {
            cout << "Error: Out of Bounds, please re-enter your selection: " << endl;
            cin >> select;
        }
    processData(select); //sends info to processData function
}

void processData() {
    int add, sub, mul, div, mod, select, intA, intB;
    switch(select) {
        case 1:
            select = 1; //addition
            add = (intA + intB);
            displayData(add); //sends info to displayData function
            break;
        case 2:
            select = 2; //subtraction
            sub = (intA - intB);
            displayData(sub);
            break;
        case 3:
            select = 3; //multiplication
            mul = (intA * int B);
            displayData(mul);
            break;
        case 4:
            select = 4; //division
            div = (intA / intB);
            displayData(div);
            break;
        case 5:
            select = 5; //modulus
            mod = (intA % intB);
            displayData(mod);
            break;
        default:
            cout << "There's been an error :(" << endl;
    }
    return 0;
}

void displayData() {

}

我是不是在倒退?我觉得如果我能在更少的函数中包含它会容易得多,但必须至少保留4个。

您的声明和定义与您传递的参数不匹配。i、 e.void processData在您的定义中,但您声明它为void processDataint,int

解决此问题的传统方法是以某种方式收集所需的所有数据,然后调用函数来完成工作。对于您的情况,您必须先计算select值,然后计算intA和intB值[1],然后将这三个值都传递到processData中

另一种方法是将调用链接在一起,因此首先请求select值,然后将select传递给读取数据的函数,并从那里调用processData

所以你会得到这样的结果:

void getInteger(int select)
{
    cout << "Please enter integer one: " << endl;
    cin >> intA;
    cout << "Please enter integer two: " << endl;
    cin >> intB;
    processData(select, intA, intB);
}


void processData(int select, int intA, int intB)
{
    ... code goes here...
}
我故意不写完整的代码——学习编程的方法是自己动手。复制粘贴是你可能已经可以做的事情


[1] 这有点问题,一个函数只能返回一件事。因为要返回两个不同的值,所以这是行不通的。有经验的程序员要么使用引用参数,要么返回包含这两个值的结构,但我猜这是您在以后的课程中学习的内容的一部分,所以让我们跳过这个想法。

这是您的代码的工作版本。。。注意这些更改,并注意getIntegerint*和int*中指针的使用

希望这对你有帮助

#include <iostream> //To input/output to the display (I think)
#include <conio.h>  //For getch() at end of program
using namespace std;

//prototypes
void getInteger(int*,int*); 
void getData(); 
void processData(int, int, int); 
void displayData(int); // haven't added anything yet

int main(){

  getData();
  return 0;
}

void getInteger(int *ptrA, int* ptrB) {
    *ptrA = 0; //safety
    *ptrB = 0; //safety
    int tempA = 0;
    int tempB = 0;
    cout << "Please enter integer one: " << endl;
        cin >> tempA;
    cout << "Please enter integer two: " << endl;
        cin >> tempB;

    *ptrA = tempA;
    *ptrB = tempB;
}

void getData() {
    int select = 100;
        while(select != 0){
        cout << "Available Functions" << endl;
        cout << "0. Exit program" << endl;
        cout << "1. Addition (+)" << endl;
        cout << "2. Subtraction (-)" << endl;
        cout << "3. Multiplication (*)" << endl;
        cout << "4. Division (/)" << endl;
        cout << "5. Modulus (%)" << endl;
        cout << "Please type your selection (1-5): " << endl;
            cin >> select;
            if (select > 5 && select > 0) {
                cout << "Error: Out of Bounds, please re-enter your selection: " << endl;
                cin >> select;
            }else if(select == 0){
                break;
            }

        int intA, intB; //these are set in the following void
        getInteger(&intA, &intB);

        processData(intA, intB, select); //sends info to processData function
    }
}

void processData(int intA, int intB, int select) {
    int add, sub, mul, div, mod;
    switch(select) {
        case 1:
            select = 1; //addition
            add = (intA + intB);
            displayData(add); //sends info to displayData function
            break;
        case 2:
            select = 2; //subtraction
            sub = (intA - intB);
            displayData(sub);
            break;
        case 3:
            select = 3; //multiplication
            mul = (intA * intB);
            displayData(mul);
            break;
        case 4:
            select = 4; //division
            div = (intA / intB);
            displayData(div);
            break;
        case 5:
            select = 5; //modulus
            mod = (intA % intB);
            displayData(mod);
            break;
        default:
            cout << "There's been an error :(" << endl;
    }
   // return 0; void does not return
}

void displayData(int result){
    cout << "The result is: " << result << endl;
}

您使用2个参数调用processData,但函数定义中没有参数变量。getInteger的定义具有参数类型,但缺少参数名称。但是这个函数一开始可能不需要任何参数。主函数需要调用其中一个函数来开始所有处理。我认为你需要重新阅读教科书中关于函数的章节。它应该解释参数是如何使用的,但您显然还没有理解它。调用函数时必须使用其定义中存在的相同数量的参数。示例:必须使用一个int参数调用getData,如:int number=0;getDatanumber;或者直接:getData5;如果函数定义为void,则它不返回值,否则返回其类型。int main{…code…;return 0;}我认为您应该检查文档中的示例并澄清您的设计…这是真的,但是由于环境的原因,看到一个工作示例并没有什么坏处;在这种情况下,不需要过度复杂化,或者不必要地使用指针来教授不好的实践。坏括号?我不同意。谢谢你的反馈。参考传递是我接下来要做的另一项作业的一部分,因此,如果我能在本课程中使用它们,当我进入下一个课程时,它实际上会很有用。但我想说,在本课之外学习如何使用它们是很有用的。我不打算给你写那个答案,但如果你觉得你能从你的课文或其他任何东西中找到答案,那就去做吧。这里的要点是,您需要查看变量流,并将值从一个传递到下一个。