动态数组输出问题

发布于 2024-12-05 15:13:59 字数 1484 浏览 5 评论 0 原文

我正在做一个家庭作业,我必须使用指针将程序中的所有静态数组转换为动态数组。我很确定我正在理解这个概念,我已经进行了更改并且我的程序可以运行。问题出在我的输出结果上。我怀疑我从正在使用的文件中输入的数据不正确。这是我的问题的图片以及相关代码:

此点之后的 9 月 21 日编辑:

输出 &数据文件: 在此处输入图像描述

主要

#include "Ch9_Ex7.h"

int main()
{
    int numCandidates;
    string *allCandidates;
    int *votes;
    int index, totalVotes;
    ifstream infile;

    allCandidates = new string[1];
    votes = new int[1];


    infile.open("./Ch9_Ex7Data.txt");
    if (!infile)
    {
        cerr << "Cannot open input file. Program terminates!" << endl;
        return 1;
    }

// read number of candidates

    readVotes (infile, votes, allCandidates, numCandidates);

    //delete [] votes;
    //delete [] allCandidates;

输入功能:

#include "Ch9_Ex7.h"

void readVotes (ifstream & infile, int *&votes,
                string *&allCandidates, int & numCandidates)
{

//    read number of candidates
    infile >> numCandidates;
    infile.ignore();  // carriage return

    //delete [] votes;
    //delete [] allCandidates;

    allCandidates = new string[numCandidates];
    votes = new int[numCandidates];

    for (int index = 0; index < numCandidates; index++)
    {
        infile >> votes[index];
        infile.ignore();  // space
        getline(infile, allCandidates[index]);
    }

}

I am working on a homework assignment where I had to convert all of the static arrays in a program into dynamic arrays using pointers. I am pretty sure I am understanding the concept, I have made the changes and my program runs. The problem is with my output results. I suspect I am inputting the data incorrectly from the file I am using. Here is a pic of my issue as well as relevent code:

EDITS FROM 9/21 AFTER THIS POINT:

Output & Data file:
enter image description here

main

#include "Ch9_Ex7.h"

int main()
{
    int numCandidates;
    string *allCandidates;
    int *votes;
    int index, totalVotes;
    ifstream infile;

    allCandidates = new string[1];
    votes = new int[1];


    infile.open("./Ch9_Ex7Data.txt");
    if (!infile)
    {
        cerr << "Cannot open input file. Program terminates!" << endl;
        return 1;
    }

// read number of candidates

    readVotes (infile, votes, allCandidates, numCandidates);

    //delete [] votes;
    //delete [] allCandidates;

Input Function:

#include "Ch9_Ex7.h"

void readVotes (ifstream & infile, int *&votes,
                string *&allCandidates, int & numCandidates)
{

//    read number of candidates
    infile >> numCandidates;
    infile.ignore();  // carriage return

    //delete [] votes;
    //delete [] allCandidates;

    allCandidates = new string[numCandidates];
    votes = new int[numCandidates];

    for (int index = 0; index < numCandidates; index++)
    {
        infile >> votes[index];
        infile.ignore();  // space
        getline(infile, allCandidates[index]);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想念有你 2024-12-12 15:13:59

您正在使用以下代码创建一个由一个 char 和一个 int 组成的数组:

allCandidates = new char[1];
votes = new int[1];

我相信您的意思是:

allCandidates = new char[numCandidates];
votes = new int[numCandidates];

它创建大小为 numCandidates 的动态数组。

另外,当您输入候选人的姓名时,您可能想使用 std::string ,如下所示:(

string *allCandidates;
allCandidates = new string[numCandidates];

感谢 Ben Voigt 指出这一点)既然您输入了他们的全名,您将需要以不同的方式输入。也许使用 getline() :

getline(cin, allCandidates[i]);

响应您的编辑:

您必须将指针作为引用传递,如下所示:

void readVotes (ifstream & infile, int *&votes, string *&allCandidates, int & numCandidates)

并在 main() 中释放它们

delete[] votes;
delete[] allCandidates;

You are creating an array of one char and one int with this code:

allCandidates = new char[1];
votes = new int[1];

I believe you meant:

allCandidates = new char[numCandidates];
votes = new int[numCandidates];

which creates dynamic arrays of size numCandidates.

Also, as you are inputting names of candidates you probably wanted to use std::string like so:

string *allCandidates;
allCandidates = new string[numCandidates];

(Thanks to Ben Voigt for pointing that out) And since you're inputting their full name you'll need to input it differently. Perhaps use getline():

getline(cin, allCandidates[i]);

In response to your edit:

You will have to pass your pointers as references like so:

void readVotes (ifstream & infile, int *&votes, string *&allCandidates, int & numCandidates)

and free them in main()

delete[] votes;
delete[] allCandidates;
清秋悲枫 2024-12-12 15:13:59

首先,这绝对是糟糕的设计,所以请不要在本练习的范围之外这样做。

现在,回答问题。如果你想在某处创建一个动态对象(或数组)并将指针传递回它,你应该通过引用获取指针。您还必须将名称读入字符串,而不是单个字符。

void readVotes (std::ifstream & infile, int * & votes, std::string * & allCandidates, int & numCandidates)
{
  // read numCandidates

  votes = new int[numCandidates];
  allCandidates = new std::string[numCandidates];

  // populate
}

调用者必须记住清理:(

int main()
{
  int n;
  int * votes;
  std::string * names;

  readVotes(std::cin, votes, names, n);

  // ...

  delete[] votes;
  delete[] names;
}

在现实情况下,我会让函数返回一个 std::vector> .)

First off, this is absolutely terrible design, so please don't do this outside the scope of this exercise.

Now, on to the question. If you want to create a dynamic object (or array) somewhere and pass the pointer to it back, you should take the pointer by reference. You also have to read the names into a string, not into a single character.

void readVotes (std::ifstream & infile, int * & votes, std::string * & allCandidates, int & numCandidates)
{
  // read numCandidates

  votes = new int[numCandidates];
  allCandidates = new std::string[numCandidates];

  // populate
}

The caller has to remember to clean up:

int main()
{
  int n;
  int * votes;
  std::string * names;

  readVotes(std::cin, votes, names, n);

  // ...

  delete[] votes;
  delete[] names;
}

(In a real-world situation, I would have the function return a std::vector<std::pair<int, std::string>>.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文