Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在这个赋值中遇到问题,我们需要使用指针和数组来查找文本文件中某些数字的平均值、中值和模式_C++ - Fatal编程技术网

C++ 在这个赋值中遇到问题,我们需要使用指针和数组来查找文本文件中某些数字的平均值、中值和模式

C++ 在这个赋值中遇到问题,我们需要使用指针和数组来查找文本文件中某些数字的平均值、中值和模式,c++,C++,我在尝试从文本文件读取值时遇到一些问题。文本文件如下所示: Murray Brandl 3 Christal Delamater 4 Zetta Kinlaw 7 Elia Roy 3 Delmer Bibb 4 Joannie Nevers 4 Roselle Gose 10 Jonathan Basnett 0 Marcel Earwood 12 Marina Newton 2 Magdalen Stephan 3 Deane Leach 5 Mariana Crosley 6 Darby

我在尝试从文本文件读取值时遇到一些问题。文本文件如下所示:

Murray Brandl 3 
Christal Delamater 4
Zetta Kinlaw 7
Elia Roy 3
Delmer Bibb 4
Joannie Nevers 4
Roselle Gose 10
Jonathan Basnett 0
Marcel Earwood 12
Marina Newton 2
Magdalen Stephan 3
Deane Leach 5
Mariana Crosley 6
Darby Froman 5
Shonda Kyzer 4
Ilana Netto 4
Candida Magnani 1
Laurena Stiverson 2
Elouise Muir 4
Rene Holiday 2
我们需要在使用指针和数组时将这些名称和值读入变量。我遇到一些错误,例如:

引发异常:读取访问冲突。 \u Pnext为0xfdfe01。发生“

我不知道这意味着什么,也不知道去哪里解决它。下面您可以看到我迄今为止的尝试,但我只能使用averageMovie函数,因为我无法正确读取文本文件。如果你能帮我或者给我指出正确的方向,我将非常感激

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include "Chapter 10 Movie Statistics.h"

using namespace std;

void averageMovie(int [], int);

int main()
{
    ifstream infile;
    infile.open("MovieStatistics.txt");

    int numOfStudents = 0;
    string first, last, line;
    int movies;

    int *numMovies;
    string *names;

    numMovies = new int[numOfStudents];
    names = new string[numOfStudents];

    if (!infile)
    {
        cout << "Error opening file";
    }
    else
    {
        while (getline(infile, line))
        {
            numOfStudents++;

            istringstream ss(line);

            ss >> first >> last >> movies;
        }

        for (int i = 0; i < numOfStudents; i++)
        {
            names[i] = first + last;
            numMovies[i] = movies;
        }
    }


    cout << "The number of students in the file is: " << numOfStudents << endl << endl;

    averageMovie(numMovies, numOfStudents);

    return 0;
}

void averageMovie(int array[], int size)
{
    int total = 0,
        average;

    for (int i = 0; i < size; i++)
    {
        total += array[i];
    }

    average = total / size;

    cout << "The average number of movies watched is: " << average;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括“第10章电影统计.h”
使用名称空间std;
无效平均值(int[],int);
int main()
{
河流充填;
infle.open(“MovieStatistics.txt”);
int numOfStudents=0;
第一行,最后一行;
国际电影;
int*numMovies;
字符串*名称;
numMovies=新整数[nummofstudens];
名称=新字符串[numOfStudents];
如果(!infle)
{
cout>第一部>>最后一部>>电影;
}
for(int i=0;inumOfStudents
没有什么区别;太晚了

你所看到的是彻底破坏计算机内存的技术结果

选项:

  • 选择一个数字(例如100)并使用该数字;最多使用100个插槽;不过,在超过限制之前,请停止程序

  • 预先计算实际需要的数量(文件中每行一个,对吗?),然后分配数组

  • 使用自行展开的数组,即向量(尽管我打赌您的赋值不允许这样做)


  • 创建
    numMovies
    names
    时,
    numfostudents
    的值是多少?您知道程序不会在循环后追溯回去并重新执行这些分配吗?不要使用
    new
    分配数组,请使用
    std::vector
    。每次你在代码中看到一个指针,就认为有什么地方出错了。你试图一次做太多的新事情。如果需要使用数组,请使用数组(整数),直到可以将元素放入数组并读取它们时不会出现错误。在您尝试将文件中的数字读入数组之前,请先执行此操作。播放字符串,并将
    字符串*
    存储在数组中;在您尝试将其与文件输入匹配之前,请使其正常工作。不要费心计算平均值,直到所有这些都完美地工作为止。@Beta:永远不要将
    字符串*
    存储在数组中。@LightnessRacesinOrbit:我没有计划,但是--为什么不呢(除了美学)?哦,哇,多简单的错误啊。真不敢相信我之前没有意识到这一点。无论如何,谢谢你的帮助!
    int numOfStudents = 0;
    // [snip]
    int *numMovies;
    string *names;
    // [snip]
    numMovies = new int[numOfStudents];
    names = new string[numOfStudents];