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

时间:2016-03-07 22:28:33

标签: c++

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

我很难将这个东西放在一起,我认为这很大程度上是因为我很难理解函数间调用是如何工作的(例如,一个函数将信息发送到另一个函数)。

我有getInteger函数从用户获取整数输入,我正在使用processdata(intA,intB);将此发送到processData(int,int)函数;但我的getData(int)函数也需要向processData发送一个整数输入 - 但是processData(select)无效,因为它没有足够的参数。 (我真的不明白这意味着什么)

这可能有点令人困惑,所以我在这里得到了整个(未完成/ wip /实际上没有工作)程序:

//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中。

3 个答案:

答案 0 :(得分:0)

您的声明和定义与您传递的参数不匹配。即void processData()在您的定义中,但您将其声明为void processData(int,int);

答案 1 :(得分:0)

这个问题的传统方法是以某种方式收集所需的所有数据,然后调用函数来完成工作。对于您的情况,您必须计算select值,然后计算intAintB值[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...
}

我故意不写完整的代码 - 学习编程的方法就是为自己做事。 Copy-n-paste是你可能已经可以做的事情。

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

答案 2 :(得分:-1)

以下是您的代码的工作版本...记下这些更改,并注意在getInteger中使用指针(int *,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;
}