【问题标题】:I can't figure out how to make my program use local variables instead of global variables我不知道如何让我的程序使用局部变量而不是全局变量
【发布时间】:2019-06-21 02:16:10
【问题描述】:

我的程序需要使用局部变量而不是全局变量。但是,当我尝试这样做时,我似乎找不到正确的参数来从 main 和函数来回传递数据。我不断收到一条错误消息,提示“int 的参数类型与 float 类型的参数不兼容”。请帮助我了解在这里做什么。感谢您的宝贵时间,我很感激。

我尝试在谷歌上搜索错误代码,但只能找到关于我尚未了解的指针问题的答案/问题。 我已经研究了几个小时,只是为了让变量在“int main”中工作,但无济于事。

//This program asks user how many grades there are, 
//inputs grades, and displays median of said grades.

//"int main" is at the bottom of the program, preceded by
//variables, function headers, and a single array.
#include <iostream>
using namespace std;

void grdTaker(float [], int);
void sortArray(float[], int);
void median(float[], int);

//Main
int main()
{
    //Variables
//int grdsCounted; //Number of grades from user.
    const int arraySize = 20;
    int grdsCounted; //Number of grades from user.
    float grades[arraySize]; //Max grades that can be entered.

    grdTaker(grdsCounted, grades[]);
    sortArray(grades, grdsCounted);
    median(grades, grdsCounted);

    system("pause");
}

void grdTaker(float array[], int size) //Function gathers grades.
{
    //const int arraySize = 20;
    //int grdsCounted; //Number of grades from user.
    //float grades[arraySize]; //Max grades that can be entered.

    cout << "You may input up to 20 grades. \n";
    cout << "First enter the number of grades you have: ";
    cin >> grdsCounted;

    while (grdsCounted > arraySize)
    {
        cout << "That is more than 20 grades, try again: \n";
        cin >> grdsCounted;
    }

    cout << "Enter each grade: \n";

    //requests how many grades there are and stores them in array
    for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
    {
        cin >> grades[grdCount];
    }
};

void sortArray(float array[], int size) //Function sorts array values.
{
    bool swap;
    float temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void median(float array[], int size) //Outputs the median of entered grades.
{
    int med = size / 2;
    int odd = med - 1;

    cout << "The median grade is: ";

    if (size % 2 == 1)
    {
        cout << array[med] << endl;
    }
    else
    {
        cout << (array[med] + array[odd]) / 2 << endl;
    }

}

【问题讨论】:

  • 您将参数转换为 grdTaker 方法。
  • 仔细查看您将哪些变量作为每个函数的参数传递。
  • 关于grdTaker(grdsCounted, grades[]);,如果你想要一个特定的数组元素,你只需要[],在这种情况下你需要指定元素的索引。 grades[4] 例如。如果您想传递整个数组(decayed to a pointer, mind you,请注意不要忘记数组的长度)离开 []s。
  • 关于cin &gt;&gt; grdsCounted;read up on scope。它不仅仅是给你干净的呼吸。如果在另一个作用域中声明了一个变量,程序的其余部分就不能使用它。在这种情况下,grdsCountedsize 的形式传入(它应该是。见上面的 cmets)但是因为 sizepassed by value,所以没有办法让你在函数中设置的任何值退出函数。

标签: c++ arrays visual-c++


【解决方案1】:

您分配给浮点数的问题可能是由于在 C++ 中以这种方式创建数组。尝试将数组声明为 de c++ 方式,使用 new

你有没有想过返回值!看看这个!比如把第一个函数分成2!编程总是把问题分成小问题。

int numberGradesFromUser() {

    int grdsCounted;
    int arraySize = 20;

    cout << "You may input up to 20 grades. \n";
    cout << "First enter the number of grades you have: ";
    cin >> grdsCounted;

    while (grdsCounted > arraySize)
    {
        cout << "That is more than 20 grades, try again: \n";
        cin >> grdsCounted;
    }
    return grdsCounted;
}

float* grdTaker(int grdsCounted) //Function gathers grades.
{

    float * grades = new float[grdsCounted];

    cout << "Enter each grade: \n";

    //requests how many grades there are and stores them in array
    for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
    {
        cin >> grades[grdCount];
    }
    return grades;

};

int main()
{
    //Variables

    int grdsCounted; //Number of grades from user.      

    grdsCounted = numberGradesFromUser();
    float *gradess = new float[grdsCounted];

    sortArray(gradess, grdsCounted);
    median(gradess, grdsCounted);

    system("pause");
}

有了这个,我想其余的功能应该可以工作了。以您的方式调整它们!

另外,最好在头文件中声明函数,或者至少在主文件的顶部,而不是在下面!

【讨论】:

    【解决方案2】:

    试试这个:

    //This program asks user how many grades there are, 
    //inputs grades, and displays median of said grades.
    
    //"int main" is at the bottom of the program, preceded by
    //variables, function headers, and a single array.
    #include <iostream>
    using namespace std;
    
    void grdTaker(float [], int, const int);
    void sortArray(float[], int);
    void median(float[], int);
    
    //Main
    int main()
    {
        //Variables
    //int grdsCounted; //Number of grades from user.
        const int arraySize = 20;
        int grdsCounted; //Number of grades from user.
        float grades[arraySize]; //Max grades that can be entered.
    
        grdTaker(grades,arraySize);
        sortArray(grades, grdsCounted);
        median(grades, grdsCounted);
    
        system("pause");
    }
    
    void grdTaker(float array[], const int arraySize) //Function gathers grades.
    {
        //const int arraySize = 20;
        //int grdsCounted; //Number of grades from user.
        //float grades[arraySize]; //Max grades that can be entered.
        int grdsCounted;
        cout << "You may input up to 20 grades. \n";
        cout << "First enter the number of grades you have: ";
        cin >> grdsCounted;
    
        while (grdsCounted > arraySize)
        {
            cout << "That is more than 20 grades, try again: \n";
            cin >> grdsCounted;
        }
    
        cout << "Enter each grade: \n";
    
        //requests how many grades there are and stores them in array
        for (int grdCount = 0; grdCount < grdsCounted; grdCount++)
        {
            cin >> array[grdCount];
        }
    };
    
    void sortArray(float array[], int size) //Function sorts array values.
    {
        bool swap;
        float temp;
    
        do
        {
            swap = false;
            for (int count = 0; count < (size - 1); count++)
            {
                if (array[count] > array[count + 1])
                {
                    temp = array[count];
                    array[count] = array[count + 1];
                    array[count + 1] = temp;
                    swap = true;
                }
            }
        } while (swap);
    }
    
    void median(float array[], int size) //Outputs the median of entered grades.
    {
        int med = size / 2;
        int odd = med - 1;
    
        cout << "The median grade is: ";
    
        if (size % 2 == 1)
        {
            cout << array[med] << endl;
        }
        else
        {
            cout << (array[med] + array[odd]) / 2 << endl;
        }
    
    }
    

    说明:我在 grdTaker 函数中添加了 arraySize,并在其中声明了 grdsCounted

    【讨论】:

    • 注意:添加arraySize 使size 未使用。 size 可以删除。
    猜你喜欢
    • 2011-03-30
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2013-08-04
    • 2019-08-16
    相关资源
    最近更新 更多