void()函数返回一个值而不是char或string?

时间:2015-02-24 06:01:45

标签: c++

这是我的代码,我已经工作了很长一段时间。我主要陷入了我的

void displayProblem(int operand1, int operand2, char getOperator)
{
cout << " operand1 getOperator operand2 = ";
}

说('displayProblem':不能将参数1从'int(__ cdecl *)(int)'转换为'int')然后另一个

int doIt(int operand1, int operand2, char getOperator)
{
    result = (operand1 getOperator operand2);

    return result;
}

错误说('doIt':无法将参数1从'int(__ cdecl *)(int)'转换为'int')它还说“result”是未声明的标识符 我想知道我做错了什么......

这是我的代码

//This program is a mathematical question generator.
//By asking user to enter 2 separate value from 1 - 100
//This program will generate a problem which involved addition, subtraction, amultiplication

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std;

void displayProblem(int, int, char);

int main()
{
    bool checkGuess(int, int);
    char getOperator(int);
    int operand1(int);
    int operand2(int);
    int doIt(int, int, char);

    srand(time(NULL));
    int max;
    int result;
    int answer;
    int correctAnswer;

    //make the program run 10 times
    for( int counter = 0; counter < 10; counter++)
    {
        //prompt user to enter positive value to generate random number up to entered value
        cout << "Please enter value from 0 - 100" << endl;
        cin >> max;

        //check for validity of entered value
        while((max < 0) || (max > 100))
        {
            cout << "Error. Please enter value from 0 - 100" << endl;
            cin >> max;
        }

        //prompt user to enter positive value to generate random number up to entered value
        cout << "Please enter value from 0 - 100" << endl;
        cin >> max;

        //check for validity of entered value
        while((max < 0) || (max > 100))
        {
            cout << "Error. Please enter value from 0 - 100" << endl;
            cin >> max;
        }

        //display the problem
        displayProblem(operand1, getOperator, operand2);

        cout << endl;

        //prompt user to enter the answer
        cout << "Please enter your answer. ";
        cin >> answer ;

        //display the correct answer
        cout << "The right answer is " << doIt(operand1, operand2, getOperator) << endl;

        //determine whether the answer is right or wrong
        checkGuess(result, answer);

        if(result == answer)
        {
            correctAnswer += 1;
        }

        cout << endl;
    }

    cout << "You got " << correctAnswer << " numbers correct." << endl;
}

//function to get the first random number
int operand1(int max)
{
    int number1;
    number1 = rand() % max;
    return number1;
}

//function to get the second random number
int operand2(int max)
{
    int number2;
    number2 = rand() % max;
    return number2;
}

//function to get a random char for mathematical operation
char getOperator(int randOperator)
{
    randOperator = rand() % 3;

    if(randOperator = 0)
    {
        cout << '+';
    }else if(randOperator = 1)
    {
        cout << '-';
    }else
    {
        cout << '*';
    }

    return randOperator;
}

//function to solve the problem
int doIt(int operand1, int operand2, char getOperator)
{
    result = (operand1 getOperator operand2);

    return result;
}

//function to show the problem
void displayProblem(int operand1, int operand2, char getOperator)
{
    cout << " operand1 getOperator operand2 = ";
}

//function to check for correct answer
bool checkGuess(int result, int answer)
{
    if(answer == result)
    {
        cout << "You are correct. " << endl;
    }
    else
    {
        cout << "Your are wrong. " << endl;
    }
}

4 个答案:

答案 0 :(得分:1)

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std;

void displayProblem(int, int, char);
 bool checkGuess(int, int);
    char getOperator(int);
    int operand(int); 
    int doIt(int, int, char);

int main()
{

    srand(time(NULL));
    int max;
    int result;
    int answer;
    int correctAnswer = 0;

    //make the program run 10 times
    for( int counter = 0; counter < 10; counter++)
    {         

        //check for validity of entered value
        do
        {
            cout << "Error. Please enter value from 0 - 100" << endl;
            cin >> max;
        } while((max < 0) || (max > 100));

    // getting the operands and operators
        int op1 = operand(max);
        int op2 = operand(max);
        char opert = getOperator(max); 

        //display the problem
        displayProblem(op1,op2,opert);
        result = doIt(op1, op2, opert);
        cout << endl;

        //prompt user to enter the answer
        cout << "Please enter your answer. \t" << endl;
        cin >> answer ;

        //display the correct answer
        cout << "The right answer is\t "<< result <<endl;

        //determine whether the answer is right or wrong 

        if(checkGuess(result, answer))
        {
            correctAnswer++;
            cout << "Your score is : " << correctAnswer;
        }

        cout << endl;
    }

    cout << "You got " << correctAnswer << " numbers correct." << endl;
}

//function to get the first random number
int operand(int max)
{
    int number1;
    number1 = rand() % max;
    return number1;
} 

//function to get a random char for mathematical operation
char getOperator(int randOperator)
{
    randOperator = rand() % 3;

    if(randOperator == 0)
        return '+'; /* If condition true function returs here */
    if(randOperator == 1) 
        return '-'; /* If condition true function returs here */
    return '*'; /* both If condition fails function returs here */
}

//function to solve the problem
int doIt(int operand1, int operand2, char getOperator)
{
    int result;  

// doin appropriate operation
    switch(getOperator)
    {
        case '+':   
                    result = operand1 + operand2 ;
                    break;
        case '-':
                    result = operand1 - operand2 ;
                    break;
        case '*':
                    result = operand1 * operand2 ;
                    break; 
    }


    return result;
} 

//function to show the problem
void displayProblem(int operand1, int operand2, char getOperator)
{
    cout << "Qn : \t" << operand1 <<"\t" << getOperator <<"\t" << operand2  << endl;
}

//function to check for correct answer
bool checkGuess(int result, int answer)
{
    if(answer == result)
    {
        cout << "You are correct. " << endl;
        return true;
    }
    cout << "Your are wrong. " << endl; 
    return false;
}

以下是完整的代码,添加了正确的评论,

答案 1 :(得分:1)

  1. 函数displayProblem期望接受带有类型(int,int,char)的参数列表。您传递的是返回整数的函数的名称,而不是传递它的整数,而且您正在以错误的顺序传递参数。要从此函数获取整数值,您需要使用括号调用它并传递参数。换句话说,而不是

    displayProblem(operand1, getOperator, operand2);
    
    你应该

    displayProblem(operand1(max), operand2(max), getOperator(max));
    

    顺便说一句,getOperator没有充分的理由接受参数,因为它没有使用它,但是你的代码应该运行得很好。

  2. 结果是未声明的标识符的原因是因为您从未声明过该变量。要声明变量,您需要先将其类型放在:

    int result;
    
  3. 除此之外,C ++不知道如何解释:

    (operand1 getOperator operand2);
    

    您需要将其拆分为不同的情况,例如

    int doIt(int operand1, int operand2, char getOperator)
    {
        int result;
        if(getOperator == '+')
        {
            result = operand1 + operand2;
        }
        if(getOperator == '-')
        {
            result = operand1 - operand2;
        }
        if(getOperator == '*')
        {
            result = operand1 * operand2;
        }
        //and so on
        return result;
    }
    

答案 2 :(得分:0)

您正在尝试将函数传递给displayProblem()。 但该函数只接受&#39; int&#39;,尝试将整数传递给displayProblem()as       displayProblem(number1,number2,getOperator);

displayProblem(operator1(int),operator2(int),getOperator);

答案 3 :(得分:0)

这些行

int operand1(int);
int operand2(int);

operand1operand2声明为两个带int输入并返回int的函数。

如果需要operand1,则无法使用int。您需要通过使用适当的参数调用operand1来替换它。您致电displayProblem

    displayProblem(operand1, getOperator, operand2);

需要更改为:

    displayProblem(operand1(max), operand2(max), getOperator(some argument));

我不知道getOperator会有什么好的理由。看起来它根本不需要争论。您应该能够将其更改为:     // 宣言。     char getOperator();

// Definition.
char getOperator()
{
    int randOperator = rand() % 3;

    // This is an error.
    // if(randOperator = 0)
    // It should be
    if(randOperator == 0)    {

        // Also, I think you want to return '+`, not just print it.
        cout << '+';

    // Also an error
    // }else if(randOperator = 1)
    // It should be
    }else if(randOperator == 1)
    {
        cout << '-';
    }else
    {
        cout << '*';
    }

    return randOperator;
}

鉴于,您可以使用

进行更改
    displayProblem(operand1(max), operand2(max), getOperator());

我会让你弄清楚其他地方的变化来修复编译错误。