简体   繁体   English

如何从文件中读取数字并在数组中使用它们?

[英]How can I read numbers from an file and use them in an array?

I am making a program that asked the user to open and existing txt file.我正在制作一个程序,要求用户打开现有的 txt 文件。 The program should read the numbers from the file and storing them into arrays.程序应该从文件中读取数字并将它们存储到 arrays 中。 I am supposed to create different functions with those numbers in the array, such as getting the largest number, the lowest number, the sum, and the average.我应该用数组中的这些数字创建不同的函数,例如获取最大数字、最小数字、总和和平均值。 I have done the functions already but I do not know how to extract the numbers from the array.我已经完成了这些功能,但我不知道如何从数组中提取数字。

Here is an example of numbers but instead of being separated by space, they're separated by a new line.这是一个数字示例,但不是用空格分隔,而是用新行分隔。

53 22 87 103 -3 75 220 1 64 543 98 44 53 22 87 103 -3 75 220 1 64 543 98 44

int getLowest(int num[], int size);
int getHighest(int num[], int size);
int getSum(int num[], int size);
int getAverage(int num[], int size);

int main()
{
    string fileName;
    ifstream inputFile;
    const int ARRAY_SIZE = 12;
    int numbers[ARRAY_SIZE];

    cout << "Enter the name of imput file: ";
    cin >> fileName;

    inputFile.open(fileName);

    if (inputFile)
    {

            cout << " numbers read from input file.\n"
                << "The lowest value is " << getLowest(numbers, ARRAY_SIZE) << endl;
    }
    else
    {
        //Display error message
        cout << "Error, this file does not exist.";
    }
    system("pause");
}

int getLowest(int num[], int size)
{
    int temp = num[0];

    for (int i = 0; i < size; i++) 
    {        
        if (temp < num[i]) 
        {                 
            temp = num[i];
        }
    }
    return temp;
}

int getHighest(int num[], int size)
{
    int temp = num[0];

    for (int i = 0; i > size; i++)
    {
        if (temp > num[i])
        {
            temp = num[i];
        }
    }
    return temp;
}

int getSum(int num[], int size)
{
    int sum = 0;

    for (int i = 0; i < size; i++)
    {
        sum += num[i];
    }
    return sum;
}

double getAverage(int num[], int size)
{
    int sum = 0;
    double average;

    for (int i = 0; i < size; i++)
    {
        sum += num[i];
    }

    average = sum / size;

    return average;
}

When I open the file with the numbers shown above, the result I get does not work.当我用上面显示的数字打开文件时,我得到的结果不起作用。 I get:我得到:

010FFCE0 numbers read from input file. 010FFCE0 从输入文件中读取的数字。 The lowest value is 1968178332最低值为 1968178332

Where it says "010FFCE0" i would like to get something that says the numbers of values, and the lowest value I would like it to be the actual lowest value.在它说“010FFCE0”的地方,我想得到一些说明值的数量和最低值的东西,我希望它是实际的最低值。

Here is an example of reading numbers from a file into std::vector<int> :这是从文件中读取数字到std::vector<int>的示例:

int number = 0;
std::vector<int>    database;
while (inputFile >> number)
{
  database.push_back(number);
}

The std::vector is a good choice here because it expands dynamically as necessary (performing dynamic memory allocation as necessary). std::vector在这里是一个不错的选择,因为它会根据需要动态扩展(根据需要执行动态 memory 分配)。

However, you don't need to store the numbers in order to determine the highest, lowest and average:但是,您不需要存储数字来确定最高、最低和平均值:

int sum = 0;
int highest;
int lowest;
inputFile >> highest;
lowest = highest;
sum = highest;
int number;
int quantity = 1;
while (inputFile >> number)
{
  if (number > highest)
  {
    highest = number;
  }
  if (number < lowest)
  {
    lowest = number;
  }
  sum += number;
  ++quantity;
}
double average = (sum * 1.0) / quantity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中读取并按类别对它们进行排序 - How can I read from a file and sort them by category 如何从文件中读取两个字符串和数字数组并将它们存储在对象向量中 - How to read two strings and array of numbers from a file and store them in a vector of objects 如何从C ++中读取文件中的数字? - How can I read numbers from a file in C++? 如何从文件中读取值,然后使用它们初始化2D数组C ++ - How to read values from a file and then use them to initialize a 2D array C++ 如何从文件中读取单词,将它们分配给数组并分析其内容? - How do I read words from a file, assign them to an array and analyze its content? 如何从文件中读取文本行并将它们放入数组中 - How to read lines of text from file and put them into an array 如何从文本文件中读取整数并将其存储在数组中? - How to read integers from a text file and store them in an array? C ++如何从文件中读取数字并将其分配给数组中的相应位置? - C++ How do I read numbers from a file and assign it to a respective place in an array? 如何从文件中读取一行 12 个数字并将其存储到数组中? - How do I read a line of 12 numbers from a file and store it into an array? 如何从.csv文件中读取值并将其存储在向量中? - c++ How can I read values from a .csv file and store them in a vector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM