473,789 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems reading values from a file and calculating the sum

36 New Member
i have to calculate a total amount of pounds for my program. i am getting each pound calculated but i have to add up all those pounds into one total.
the problem is one of the total pounds is not supposed to be calculated.

how do i do this I am struggling

OUTPUT:
Total weight21.5
Total weight30
Total weight50.8
Total weight12.4
Total weight42.6
Total weight31.5
Total weight22.4
Total weight71.3 this total cant be included in the total pounds

the total amount of weight has to come out to 211.2pounds
I cant figure how to get that amount. Any ideas will help!


here is the code that calculates the above:
Expand|Select|Wrap|Line Numbers
  1.  
  2. totalWeight=weightOfPounds;
  3.                 outFile2<< "Total weight" << totalWeight << endl;
  4.  
  5.  
Oct 27 '08 #1
11 3335
donbock
2,426 Recognized Expert Top Contributor
So ... you have a list of input values and you want to compute the sum of all these input values except for one that should be left out of the sum.

What is distinctive about the input value that doesn't go into the sum? How do you recognize which one doesn't get added in with the others? By "you", I mean you as a person -- we'll worry about how your computer program recognizes it later.

Do these input values come from typed user input, read from an input file, passed as input arguments to your function, or are they hard-coded into your program?
Oct 27 '08 #2
charmeda103
36 New Member
the input values are read in from an input file,
Oct 27 '08 #3
whodgson
542 Contributor
Is it not possible to keep a running total of the 'pounds' with an expression like
sum+=pounds;
.......in a for loop?
Oct 27 '08 #4
donbock
2,426 Recognized Expert Top Contributor
the input values are read in from an input file,
OK. What is the format of the input file ... is it simply a newline-separated list of floating-point values, is it a list of "label=value" assignments, is it something else.

And most importantly, let me repeat my earlier question. How do you know which input values do NOT go into the sum?
Oct 27 '08 #5
charmeda103
36 New Member
if have of numbers written separetly in a for loop and those numbers to totaled into one sum. how would u do that;






example:

Total weight21.5
Total weight30
Total weight50.8
Total weight12.4
Total weight42.6
Total weight31.5
Total weight22.4
Total weight71.3


the total sum=211.2


these are reading in from a input file.


Anything will help please.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <cmath>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12.     string date;
  13.     int monthForPassenger;
  14.     int dayForPassenger;
  15.     int yearForPassenger;
  16.     char junkChar;
  17.     string lastNameForPassenger;
  18.     string firstNameForPassenger;
  19.     int numberOfBagsForPassenger;
  20.     float weightOfBagsForPassenger;
  21.     float lengthOfBagsForPassenger;
  22.     float widthOfBagsForPassenger;
  23.     float heightOfBagsForPassenger;
  24.     int numberOfBagsToPassenger;
  25.     int totalCharges;
  26.  
  27.     double additional;
  28.     double additional2;
  29.     double additionalCharge;
  30.     float secondBag=25.00;
  31.     const float weightCharge=50.00;
  32.     float overSizedBag;
  33.  
  34.     const float chargeExtraForGirth=100.00;
  35.     int totalCost;
  36.     int totalNumberOfPassengers=4;
  37.     float totalWeight;
  38.     float averageWeight;
  39.     float totalAdditionalCharges;
  40.     int totalSum=0;
  41.  
  42.  
  43.  
  44.     int k;
  45.     float girth;
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.     ifstream inFile;
  55.     inFile.open("F:\\data3.txt");
  56.     ofstream outFile;
  57.     outFile.open("F:\\charges.txt");
  58.     ofstream outFile2;
  59.     outFile2.open("F:\\summary.txt");
  60.  
  61.  
  62.  
  63.     while (!inFile.eof())
  64.     {
  65.  
  66.             inFile >> monthForPassenger;
  67.             inFile >>    junkChar;
  68.             inFile >> dayForPassenger;
  69.             inFile >> junkChar;
  70.             inFile >> yearForPassenger;
  71.             inFile >> firstNameForPassenger;
  72.             inFile >> lastNameForPassenger;
  73.             outFile<<  "PASSENGER:" <<"                    " <<firstNameForPassenger << " " << lastNameForPassenger << endl;
  74.  
  75.  
  76.             outFile <<"TICKET PURCHASE DATE:" <<"          "<< monthForPassenger << '/' << dayForPassenger << '/' << yearForPassenger << endl;
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.             inFile >> numberOfBagsForPassenger;
  84.             outFile << "NUMBER OF CHECKED BAGS:"<< "                " <<numberOfBagsForPassenger << endl;
  85.  
  86.  
  87.             outFile << "TOTAL ADDITIONAL CHARGES:";
  88.  
  89.  
  90.  
  91.             for (k=1; k<=numberOfBagsForPassenger; k=k+1)
  92.             {
  93.  
  94.  
  95.                 inFile >> weightOfBagsForPassenger;
  96.                 totalWeight=weightOfBagsForPassenger;
  97.                 outFile2<< "Total weight" << totalWeight << endl;
  98.  
  99.  
  100.  
  101.  
  102.                 if (weightOfBagsForPassenger>=50&&weightOfBagsForPassenger<=70)
  103.                 {
  104.  
  105.                     totalCost=weightCharge+chargeExtraForGirth+chargeExtraForGirth+chargeExtraForGirth+secondBag;
  106.                     outFile << "           "<<'$'<< fixed<< setprecision(2) << totalCost << endl;
  107.  
  108.                     outFile << "overweight bag"  << " "<< '$' << fixed<< setprecision(2) << weightCharge << endl;
  109.  
  110.  
  111.                 }
  112.                 else
  113.                 {
  114.                     //outFile <<" no charge" <<endl;
  115.                 }
  116.                 inFile >> lengthOfBagsForPassenger;
  117.  
  118.  
  119.                 inFile >> widthOfBagsForPassenger;
  120.  
  121.  
  122.                 inFile >> heightOfBagsForPassenger;
  123.  
  124.  
  125.  
  126.  
  127.                 girth=lengthOfBagsForPassenger+widthOfBagsForPassenger+heightOfBagsForPassenger;
  128.  
  129.                 if (girth>62&&weightOfBagsForPassenger<=115&&weightOfBagsForPassenger>=50&&weightOfBagsForPassenger<=70)
  130.                 {
  131.                      overSizedBag=chargeExtraForGirth*2;
  132.                     outFile << "oversized bags" << " " << '$'<<fixed<< setprecision(2)<<overSizedBag<< endl;
  133.                 }
  134.                 else
  135.                 {
  136.                     //cout <<"dont charge extra for girth" << endl;
  137.                 }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.             }
  144.  
  145.  
  146.  
  147.                 if(girth<115&&weightOfBagsForPassenger<70)
  148.                 {
  149.  
  150.                         //    outFile <<"NO ADDITIONAL FEE" << endl;
  151.                 }
  152.                 else
  153.                 {
  154.                     outFile << endl <<"NUMBER OF REJECTED BAGS"<< "                "<<numberOfBagsForPassenger;
  155.                     outFile  << endl << " exceeded weight limit" << "         "<<numberOfBagsForPassenger<< endl;
  156.                 }
  157.  
  158.  
  159.  
  160.                if (numberOfBagsForPassenger>=3)
  161.                {
  162.                  totalCharges=numberOfBagsForPassenger%2;
  163.                  additionalCharge=totalCharges*100.00;
  164.  
  165.  
  166.  
  167.  
  168.                  additional=additionalCharge+secondBag;
  169.                 outFile<< "additional Bags"<<" "<< '$' << fixed<< setprecision(2) << additional << endl;
  170.  
  171.  
  172.  
  173.               }
  174.              else
  175.              {
  176.              }
  177.  
  178.  
  179.  
  180.  
  181.             if (numberOfBagsForPassenger>=2&&monthForPassenger>=4&&yearForPassenger>=2008||monthForPassenger==4&&yearForPassenger==2008&&dayForPassenger>=5&&girth<115&&weightOfBagsForPassenger<70)
  182.  
  183.             {
  184.  
  185.                     outFile << "         "    << '$' << fixed<< setprecision(2) << secondBag << endl;
  186.  
  187.             }
  188.             else 
  189.             {
  190.                 //secondBag=0;
  191.                 //outFile<<secondBag<< endl;
  192.             }
  193.             outFile << endl << endl;
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.     }
  202.  
  203.     outFile2 << "TOTAL NUMBER OF PASSENGERS:" << "           " << totalNumberOfPassengers <<" " << "Passengers" << endl;
  204.  
  205.     outFile2   << "TOTAL WEIGHT OF BAGS CHECKED:"<< "        "  << "pounds" << endl;
  206.     outFile2 << "TOTAL NUMBER OF CHECKED BAGS:" << "           " << "bags" << endl;
  207.     outFile2 << "AVERAGE WEIGHT OF CHECKED BAGS:" << "      " << "pounds" <<endl;
  208.     totalAdditionalCharges=secondBag+totalCost;
  209.     outFile2 << "TOTAL ADDITIONAL CHARGES:"<< "           " <<fixed<< setprecision(2)  << totalAdditionalCharges;
  210.  
  211.          inFile.close();
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.         return 0;
  221.  
  222. }
  223.  
Oct 27 '08 #6
boxfish
469 Recognized Expert Contributor
Make a variable for the total sum, set it to zero, and then every time you read in a number from the file, add it to the total.
Hope this helps.
Oct 27 '08 #7
donbock
2,426 Recognized Expert Top Contributor
In your other thread you said that some of the input values shouldn't go into the sum. Is that still the case or did I misunderstand?
Name of the other thread: "PLEASE Im having so much trouble with this calculation."

What step of the problem are you having trouble with?
... Can you read the input file?
... Can you extract the input values from the input file?
... Can you add the input values together into a sum?

Are you stuck and don't know how to start?
Did you write a program but it either won't compile or it doesn't do what you want?
Oct 27 '08 #8
donbock
2,426 Recognized Expert Top Contributor
Suppose all of the input values were in a global array like this:
double inputValues[10];

How would you compute the sum of all entries in the array?
Oct 27 '08 #9
charmeda103
36 New Member
In your other thread you said that some of the input values shouldn't go into the sum. Is that still the case or did I misunderstand?
Name of the other thread: "PLEASE Im having so much trouble with this calculation."

What step of the problem are you having trouble with?
... Can you read the input file?
... Can you extract the input values from the input file?
... Can you add the input values together into a sum?

Are you stuck and don't know how to start?
Did you write a program but it either won't compile or it doesn't do what you want?


the program is reading from the input file,
I started the program i added the program to the thread so you can see what im stuck on.

My program is readng each weight separtley but i want it to read in the sum of all the weights.I am trying to add the each indvidul weight I called this variable "weightOfBagsFo rPassenger" to come up with a total of 211.2. I cant include the last weight of 71.3 because that weight has been rejected.

I declared a new variable called totalSum=0;
from my program i cant determine how to add the total of the "weightOfBagsFo rPassenger"
into one sum.
does that make sense?
Oct 27 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2915
by: DJTB | last post by:
zodb-dev@zope.org] Hi, I'm having problems storing large amounts of objects in a ZODB. After committing changes to the database, elements are not cleared from memory. Since the number of objects I'd like to store in the ZODB is too large to fit in RAM, my program gets killed with signal 11 or signal 9... Below a minimal working (or actually: it doesn't work because of memory
8
5231
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do...
14
1855
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works, but I still get some errors when i try to compile it: stack.c: In function 'reverseFile'
11
1645
by: harold | last post by:
Dear all, Maybe I stared on the monitor for too long, because I cannot find the bug ... My script "transition_filter.py" starts with the following lines: import sys for line in sys.stdin : try :
2
3278
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
3
1741
by: katz911 | last post by:
Hello, I've encountered a strange problem which I can't seem to explain. I've written a simple C++ class that deals with a certian binary file. In one of the methods there, I wish to update a file - namely to read a certain field whose offset I know, and then write to another field, whose offset I also know. In order to read the data, I first use file.seekg(offset, ios::beg), and then a read command; I've checked the variable, and...
409
11163
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are objects that are more than 4GB long. The problem is, when you have in thousands of places
8
1529
by: psy_berpunk | last post by:
hey, i'm trying to write a simple program to read gif87a non- interlaced format with a single image-descriptor --- I am using djgpp on windows xp. Sounds simple enough, unfortunatly data in the format is arranged primarily in single-byte unsigned integers. So i've been reading them in a chars and casting them as unsigned chars into an int.
2
5843
by: steve005 | last post by:
Hi, I am writing a binary file with matlab that consists of a couple hundred double values and then reading it in with c++. The problem arises in c++ when I try and read in the files. At a specific point it stops and wont read any more values. I use this code to open it and read it: if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0 ); fread(&coneAngle,sizeof(double),1,fp); After about twenty simmilar reads it fails, returns an...
0
1175
by: Arto Viitanen | last post by:
I am using WriteXML/ReadXml pair to save DataSet for later use. DataSet is built by reading textfiles and storing their data to different DataTables. Each file contains several complex values, that are represented using a DataValue object. DataValue is an abstract class, that has subclasses that implement the value (for example; ValBlkValue represents a vector of floating values). Class DataValue implements IXmlSerializable interface and...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.