这里不允许使用c ++函数定义

时间:2017-10-11 01:53:26

标签: c++ function switch-statement

这是我的c ++课程,我在Windows上使用xcode时在Windows上讲授。问题陈述:  编写一个程序,使用displayMenu函数显示以下菜单:

数学课程:  1.添加两个数字  2.减去两个数字  3.将两个数相乘  4.除以两个数字  5.退出

基本上我应该使用switch语句/ case来运行程序。我不断得到“这里不允许使用函数定义”而且我仍然坚持如何让程序运行。如果有人能帮助我找到我的错误,或者我出错了那将是一个救生员!

这是我写的代码:

    #include <iostream>

    float addNumbers(float, float);
    float subtractNumbers(float, float);
    float multiplyNumbers(float, float);
    float divideNumbers(float, float);
    float Result;
    void displayMenu();

    int main(void)
    {
    {

int choice;
float Number1, Number2, Result, quit;

displayMenu();
std::cin >> choice;
do
{
    if (choice > 0 && choice < 5)
    {
        std::cout << std::endl << "Enter two numbers: ";
        std::cin >> Number1 >> Number2;
    }
    switch (choice)
    {
        case 1:
            Result = addNumbers(Number1, Number2);
            std::cout << "The numbers added are " << Result << std::endl;
            break;

        case 2:
            Result = subtractNumbers(Number1, Number2);
            std::cout << "The numbers subtracted are " << Result << std::endl;
            break;

        case 3:
            Result = multiplyNumbers(Number1, Number2);
            std::cout << "The numbers multiplied are " << Result <<                              std::endl;
            break;

        case 4:
            Result = divideNumbers(Number1, Number2);
            std::cout << "The numbers divided are " << Result << std::endl;
            break;

        case 5:
            Result = quit;

        default:
            break;

    }

    displayMenu();
    std::cin >> choice;

} while (choice != 5);
    }

    void displayMenu()
    {
        std::cout << "Math Program\n";
        std::cout << "1. Add two numbers\n";
        std::cout << "2. Subtract two numbers\n";
        std::cout << "3. Multiply two numbers\n";
        std::cout << "4. Divide two numbers\n";
        std::cout << "5. quit\n";
    }

    float AddNumbers(float Number1, float Number2)
    {
       Result = (Number1 + Number2);
    }

    float SubtractNumbers(float Number1, float Number2)
    {
         Result = (Number1 - Number2);
    }

    float MultiplyNumbers(float Number1, float Number2)
    {
         Result = (Number1 * Number2);
    }

    float DivideNumbers(float Number1, float Number2)
    {
        Result = (Number1 / Number2);
    }

        return 0;
    }

2 个答案:

答案 0 :(得分:1)

您的代码存在许多问题。确保将函数放在main()之外,同时确保函数返回值。如果将函数声明为float getSum(),则应返回浮点值。那些是明显的错误。轻微错误包括您忘记保持名称与函数完全相同。你用小写字母开始的一部分,然后你给了他们资本。

我在这里为您修复了代码。

#include <iostream>

float addNumbers(float, float);
float subtractNumbers(float, float);
float multiplyNumbers(float, float);
float divideNumbers(float, float);
float Result;
void displayMenu();

int main(void)
{
    {

        int choice;
        float Number1, Number2, Result, quit;

        displayMenu();
        std::cin >> choice;
        do
        {
            if (choice > 0 && choice < 5)
            {
                std::cout << std::endl << "Enter two numbers: ";
                std::cin >> Number1 >> Number2;
            }
            switch (choice)
            {
            case 1:
                Result = addNumbers(Number1, Number2);
                std::cout << "The numbers added are " << Result << std::endl;
                break;

            case 2:
                Result = subtractNumbers(Number1, Number2);
                std::cout << "The numbers subtracted are " << Result << std::endl;
                break;

            case 3:
                Result = multiplyNumbers(Number1, Number2);
                std::cout << "The numbers multiplied are " << Result << std::endl;
                break;

            case 4:
                Result = divideNumbers(Number1, Number2);
                std::cout << "The numbers divided are " << Result << std::endl;
                break;

            case 5:
                Result = quit;

            default:
                break;

            }

            displayMenu();
            std::cin >> choice;

        } while (choice != 5);
    }

    return 0;
}

void displayMenu()
{
    std::cout << "Math Program\n";
    std::cout << "1. Add two numbers\n";
    std::cout << "2. Subtract two numbers\n";
    std::cout << "3. Multiply two numbers\n";
    std::cout << "4. Divide two numbers\n";
    std::cout << "5. quit\n";
}

float addNumbers(float Number1, float Number2)
{
    return (Number1 + Number2);
}

float subtractNumbers(float Number1, float Number2)
{
    return (Number1 - Number2);
}

float multiplyNumbers(float Number1, float Number2)
{
    return (Number1 * Number2);
}

float divideNumbers(float Number1, float Number2)
{
    return (Number1 / Number2);
}

答案 1 :(得分:0)

您的函数displayMenu()及其后的所有函数都在main()中定义。

这正是编译器所说的。将它们全部移出main(),你就可以了。