Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:37:23+00:00 2026-05-25T16:37:23+00:00

I am working on a homework assignment where I had to convert all of

  • 0

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]);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:37:23+00:00Added an answer on May 25, 2026 at 4:37 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a homework assignment in which I'm required to use char arrays
I'm working on a homework assignment and I ran into a little snag. I'm
I'm working on a homework assignment to modify code given by my professor using
I'm working on a homework assignment in Perl CGI using the CGI.pm module. In
I'm working on a homework assignment translating a C program we wrote to MIPS.
Ok, so I am working on a homework assignment, and I am using SWING
I'm working on Polynomial Transform for a homework assignment. I'm using a document from
I'm working on a homework assignment for an image shrinking program in C++. My
I'm working on a homework assignment for Java, where a program is supposed to
I'm working on a homework assignment that requires me to write a program that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.