mainly i have problem with the loops
it's suppose to be a program that reads in data from a text file (not necessarily this one, but something like it):

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
34252 85776 65746 93453 63534 34335 0
20444 73462 83575 23552 23753 23532 0
84743 32853 23522 23452 85347 78988 0
89092 67342 87989 74389 94857 37478 0

the program will separate the data in the row (0 on Sunday b/c there will be no work that day) and there can be up to 30 lines of data(representing the amount of trucks)
if weight is over 80,000 then every 100 pounds after that is charged $50

also, can someone suggest how i could add an if statement that if it's 0 (Sunday) maybe an output statement for that day that says "no work today" or something like that?
sorry if i'm asking for too much, but i've been trying to figure out this thing for a while with little progress so i thought maybe someone more experienced could help me with the whole thing since bits and pieces are a little mess

also, i know it is much easier to use vectors, but i'm really trying to do this w/out vectors this one time (:

#include <iostream>
#include <fstream>

using namespace std; 
int weightFunction(int, int);
int main(){

 //open input file
 ifstream inFile; 
 inFile.open("Trucks.txt");
 if(!inFile){
             cout<<"Can't open file";
             return 1; 
             } 
             
  //write to file 
 ofstream outFile; 
 outFile.open("TruckFines.txt");
              
 //variables             
 string dayOfWeek;
 int truckWeight = 0, currentTruckWeight;  
 int i, n, d/* day # */,t/* truck #*/, g;
 
 //for loop for inFile 
 for (i = 0; i < 7; i++){
     inFile >> dayOfWeek[i];
     }
 while (inFile){
           for (n = 0; n < 7; n++){
               inFile >> truckWeight; 
               }
               }
 //for loop for day 
 for (d = 0; d < 7; d++){
     outFile << "Statistics for " << dayOfWeek[d]<< ":" << endl; 
     outFile << "Trucks with Fines: " << endl; 
     
 for (t = 0; t < truckWeight; t++) {
     currentTruckWeight = truckWeight[t];

          //function call
     result = weightFunction (d, t); 
     outFile <<  "Truck# " << currentTruck + 1 << "   $" << result << endl;
     
     outFile << endl; 
     outFile << "Trucks without fines: " << endl; 
     
     //goes through all trucks 
     for(g = 0; g < truckWeight.size(); g++){
           currentWeight = truckWeight[g]; 
           
            //if current truck is less than or equal to the weight limit
            if(currentWeight <= 80000){
                             outFile << "Truck# " << currentTruck + 1 << endl;; 
                             }e
     
     }//end for
     outFile << endl; 
     }//end for 
     
     //close files 
     inFile.close(); 
     outFIle.close();
     
    system("pause");
    return 0; 
}

//function for overweight limit
 int weightFunction(int d, int t){
 
 //variables 
 int currentDay, currentTruck, currentWeight, result; 
 
 //if current truck over limit (limit is 80,000)   
 if (d > 80000){
  result << "Truck# " << currentTruck + 1 << "   $" << ((currentWeight - 80000) % 100) * 50 << endl; 
} 
  
      return result; 
      }

Recommended Answers

All 2 Replies

while (inFile){
           for (n = 0; n < 7; n++){
               inFile >> truckWeight; 
               }

You're losing all your data here. You read in about 100 numbers and overwrite the last number each time. Seems to me that truckWeight should be a two dimensional array since you have a two-dimensional grid. Right now it is an integer. You can't store 100 different numbers in one integer, so you either have to change its type or you have to change the program flow so that you don't read the file in all at once.

Thread closed, forum flooding is not welcome. Please read the rules. Other thread can be found here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.