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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:45:15+00:00 2026-05-26T10:45:15+00:00

below is a code I am writing to learn about using files in C++.

  • 0

below is a code I am writing to learn about using files in C++. I have everything reading and writing correctly but I can not get my display to show the correct values because it is ignoring when I try to initialize the Total variables in the while loop.

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

int main()
{
    int customerNumber;
    double beginningBalance, purchase, payments, financeCharge, endingBalance;
    double beginningTotal, purchaseTotal, paymentsTotal, financeChargeTotal, endingTotal;
    ifstream inputFile;
    ofstream outputFile;

    inputFile.open("BeginningBalance.dat");
    outputFile.open("EndingBalance.dat");
    cout<<"Cust No | Beginning Bal | Finance Charge | Purchases | Payments | Ending Balance"<<endl;

    while (inputFile >> customerNumber)
    {
        outputFile <<customerNumber<<endl;
        inputFile >> beginningBalance;
        inputFile >> purchase;
        inputFile >> payments;

        financeCharge = beginningBalance * .01;
        endingBlanance= beginningBalance + purchase + financeCharge - payments;

        //***********************************************
        //This is where I am having trouble initializing variables.
        //***********************************************
        beginningTotal += beginningBalance; //beginningTotal not being intitialized.
        financeChargeTotal += financeCharge;
        purchaseTotal += purchase;
        paymentsTotal += payments;
        endingTotal += endingBalance;

        outputFile <<fixed<<setprecision(2)<<endingBalance<<endl;

        cout<<setw(5)<<customerNumber<<fixed<<setprecision(2)<<"        "<<beginningBalance<<"        "<<financeCharge<<"          "<<purchase<<"       "<<payments<<"       "<<endingBalance<<endl;
    }
    cout<<"Total:    "<<fixed<<setprecesion(2)<<beginningTotal<<"   "<<financeChargeTotal;
    system ("PAUSE");
    return 0;
}
  • 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-26T10:45:16+00:00Added an answer on May 26, 2026 at 10:45 am

    You do not have your variables initialized in the first place, so they don’t have any initial values. Then you add something to those variables, and the result is undefined (read garbage).

    Consider declaring them like this:

    double beginningTotal = 0, purchaseTotal = 0, paymentsTotal = 0, financeChargeTotal = 0, endingTotal = 0;
    

    … or even better – create some structure for them.

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

Sidebar

Related Questions

I'm trying to send a string via ftp using the code below but I'm
After writing the code below: #include <iostream> using namespace std; typedef struct Node {
Writing the code below without dot notation, do I have this right? from: if(self.yellowViewController.view.superview
Here is my test code below. //This correctly prints C:\Program Files from 64 app
With the below code, $quantity is assumed to be an integer but I'm not
When writing the below my code locks up on GetResponse. Why? try { WebRequest
I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
I constantly find myself writing similar code like the example below: if (object[Object Name]
The below code is very simple. I have a jQuery autocomplete bound to an
instead of writing code in the standard way: $.get('test.xml',function(){ //manipulate the code here })

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.