c++ - 我不知道如何让我的程序使用局部变量而不是全局变量

标签 c++ arrays visual-c++

我的程序需要使用局部变量而不是全局变量。然而,当我尝试这样做时,我似乎无法找到正确的参数来从 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;
    }

}

最佳答案

您分配给 float 的问题可能是由于在 C++ 中以这种方式创建数组造成的。尝试使用 new 以 de C++ 方式声明数组。

你有没有想过返回这些值!看看这个!例如,将第一个函数分成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");
}

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

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

关于c++ - 我不知道如何让我的程序使用局部变量而不是全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395875/

相关文章:

php - 如何在 Laravel 中对数组的值进行排序

c++ - 对我的 Makefile 如何重新制作目标文件感到困惑

c++ - CUDA 中的三重 For 循环

c++ - 用于突出显示 BNF 语法指定的语法语言的 Lexer?

c++ - 在 VS 中构建由 Boost 驱动的解决方案

c - 如何在内联汇编中声明字符缓冲区?

c++ - 我如何检查在无模式对话框中单击了该按钮

c++ - 排序数组文件 I/O

objective-c - 获得对 NSMutableArray 的访问权限

java - 编程错误 "XXX is already defined in main(String[])"