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 8541381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:49:07+00:00 2026-06-11T11:49:07+00:00

I’m having an issue of getting garbage out of my array after I pass

  • 0

I’m having an issue of getting garbage out of my array after I pass it to a function. Is this the correct way to pass it to a function that will just print it out?

while(infile){
        infile >> batch;
        infile >> amount;
        cout << batch << "     " << amount<< endl;
        totalChocolates[batch-1]+=amount;   
        chocolateBatches[batch-1]++;

    }

    //header();
    print(totalChocolates,chocolateBatches);

    system("pause");
    return 0;

void print(int *total,int *batch)
{
    for(int i = 0;i<size;i++)
    {
        cout << i+1 << "     "  << batch[i] << "     " << total[i] << endl;

    }
}

OK I pulled out the function and i’m still getting garbage.

while(infile>> batch >> amount){
        //infile >> batch;
        //infile >> amount;
        cout << batch << "     " << amount<< endl;
        totalChocolates[batch-1]+=amount;   
        chocolateBatches[batch-1]++;

    }
    for(int i = 0;i<size;i++)
    {
        cout << i+1 << "     "  << chocolateBatches[i] << "     " << totalChocolates[i] << endl;

    }

It returns

1     64
8     907
18     1133
9     636
3     1134
16     1101
6     313
7     791
7     1130
1     1221
20     332
22     697
6     807
2     36
2     747
16     1219
3     859
18     639
18     312
7     1079
10     1074
5     678
18     59
1     -858993418     -858960818
2     -858993429     -858971910
3     -858993421     -858963346
4     -858993431     -858970692
5     -858993427     -858963901
6     -858993416     -858961011
7     -858993417     -858960812
8     -858993421     -858961874
9     -858993417     -858960079
10     -858993418     -858960161
11     -858993422     -858963562
12     -858993428     -858970812
13     -858993460     -858993460
14     -858993427     -858967859
15     -858993422     -858964267
16     -858993418     -858954549
17     -858993423     -858966453
18     -858993423     -858964725
19     -858993460     -858993460
20     -858993433     -858973375
21     -858993419     -858963644
22     -858993426     -858968664
23     -858993456     -858993060
24     -858993460     -858993460
25     -858993455     -858990222
Press any key to continue . . .
  • 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-06-11T11:49:08+00:00Added an answer on June 11, 2026 at 11:49 am

    Pretty much, yes. The issue is, you are not properly using the size of the array, so you are probably just running into the uninitialized space at the end. Also pass a size into the print function so you don’t run off the end.

    EDIT: Although do take heed of Kerrek’s warning in your question comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.