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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:28:19+00:00 2026-05-27T02:28:19+00:00

Here is my problem, I’m trying to read in data from a file beginningbalance.dat

  • 0

Here is my problem, I’m trying to read in data from a file "beginningbalance.dat", containing the following:

111
100.00
200.00
50.00
222
200.00
300.00
100.00

What I want to do is read in 111 for customer_number[0] and 222 for customer_number[1]. For new_balance[0], I want to add the values of the 3 numbers under 111 and new_balance[1] the numbers under 222.

#include <iostream>
#include <fstream>
using namespace std;



int main()
{
  int count = 0;
  double val1, val2, val3, sum;
  int customer_number[2];//Array for holding customer number from file.
  double new_balance[2];//Array for holding added values from file.

  ifstream beginning_balance;
  beginning_balance.open("beginningbalance.dat");



  while(beginning_balance)
    {
        beginning_balance >> customer_number[count];
        beginning_balance >> val1;
        beginning_balance >> val2;
        beginning_balance >> val3;

        sum = val1+val2+val3;

        new_balance[count] = sum; //Supposed to read in customer id number then 3 values from files, then starts itself over and reads a new customer number for the new array element.
        count++;                   //
    }

  cout<<val1<<" "<<val2<<" "<<val3<<"\n\n";//Testing values being read in

  cout<<"Customer Number #"<<customer_number[0]<<endl;//Should read Customer Number # 111
  cout<<"New Balance $"<<new_balance[0]<<endl;
  cout<<"Customer Number #"<<customer_number[1]<<endl;//Should read Customer Number # 222
  cout<<"New Balance $"<<new_balance[1]<<endl;



  system("pause");
  return 0;
}

My output is

100  -858993460 -858993460

Customer Number #111
New Balance $-1.71799e+009
Customer Number #0
New Balance $5.48413e-322
Press any key to continue...

{I had my val1,val2,val3,and sum set to int when it should have been something to allow floating point numbers, I also only allowed 1 element for each array.}

  • 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-27T02:28:20+00:00Added an answer on May 27, 2026 at 2:28 am

    This might not be the whole bug, but it’s at least part of it:

    int customer_number[1];//Array for holding customer number from file.
    double new_balance[1];//Array for holding added values from file.
    

    You’ve declared these [1] which means “1 element with index 0”. If you want indexes 0 and 1 then you should use [2]. By overrunning your array bounds you’re corrupting the memory of other variables nearby, which probably explains the bad balance values. (But maybe not the bad val2 and val3 values.)

    However since your code allows you to read in an arbitrary number of data sets you should consider using an stl::vector or similar for these, or probably an stl::vector of a structure / class that contains the customer number and balance as a single object.

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

Sidebar

Related Questions

I have an interesting problem here I've been trying to solve for the last
I'm trying to solve the problem here but I don't know why my code
The problem here is pretty self-evident from the resulting JSON array at the bottom.
everyone I got problem here I need to update a plist data in a
Here's the problem: In C# I'm getting information from a legacy ACCESS database. .NET
I have a problem here. I have about 13K rows of data that I
Here is problem I'm trying to solve. It's for logistics company. I got all
Unusual problem here: I have an app that uses a text file which contains
My problem here is the following : i am developing a website where Lecturers
Weird problem here, I'm trying to use a global function to update my settings

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.