there's something wrong with my program, it runs but it goes on forever and it doesn't display the day of the week only the letter M and that's it
this is my text file, but my program is not suppose to be restricted to this text file:

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
34252 85776 65746 93453 63534 34335 0
78352 23412 89889 88387 48021 89903 0
98098 73495 12489 84895 30583 28423 0
28942 38942 38475 83753 83729 82742 0

PLEASE HELP!!!! thank you

#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, currentWeight, currentDays, currentTruck, currentTruckWeight, result;
 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 >> currentDays; 
               }
               }
 //for loop for day 
 for (d = 0; d < 7; d++){
     outFile << "Data for " << dayOfWeek[d]<< ":" << endl; 
     outFile << "Trucks with Fines: " << endl; 
     
 //for loop for trucks    
 for (t = 0; t < truckWeight; t++) {
     currentTruckWeight = truckWeight;

     //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; g++){
           currentWeight = truckWeight; 
           
            //if current truck is less than or equal to the weight limit
            if(currentWeight <= 80000){
                             outFile << "Truck# " << currentTruck + 1 << endl;; 
                             }
     
     }
     outFile << endl; 
     } 
     }
    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 <<((currentWeight - 80000) % 100) * 50; 
} 
  
      return result; 
      }

Shouldn't a couple of your variables be declared as arrays? You're treating them as such, but it won't work the way you want.

Shouldn't a couple of your variables be declared as arrays? You're treating them as such, but it won't work the way you want.

i'm not sure how to do that since every time i declare as variables, i get errors in my loops that i know are okay

i made truckWeight into an array but i get messages like:
c++ frobids comparison between pointer and integer
invalid conversion from int to int
and invalid conversion from int [*][7] to int
can someone please suggest or point out what im doing wrong? thanks

#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[30][7] = {};  
 int currentWeight, currentDays, currentTruck, currentTruckWeight, result;
 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 >> currentDays; 
               }
               }
 //for loop for day 
 for (d = 0; d < 7; d++){
     outFile << "Data for " << dayOfWeek[d]<< ":" << endl; 
     outFile << "Trucks with Fines: " << endl; 
     
 //for loop for trucks    
 for (t = 0; t < truckWeight; t++) {
     currentTruckWeight = truckWeight[t];

     //function call
     result = weightFunction (d, t); 
     outFile <<  "Truck# " << currentTruck + 1 << "   $" << result << endl;
     
     outFile << "Trucks without fines: " << endl; 
     
     //goes through all trucks 
     for(g = 0; g < truckWeight; g++){
           currentWeight = truckWeight; 
           
            //if current truck is less than or equal to the weight limit
            if(currentWeight <= 80000){
                             outFile << "Truck# " << currentTruck + 1 << endl;; 
                             }
     
     }
     outFile << endl; 
     } 
     }
    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 <<((currentWeight - 80000) % 100) * 50; 
} 
  
      return result; 
      }

You need to think about what you're doing, not just toss anything together and pray.

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

Why are you reading 7 separate values into one single integer?

also, format your code. We can't tell if you have separate loops or nested loops. the code is very hard to follow.

And if you have errors, post them. Exactly as they are displayed to you. I know here's at least one in the code you posted and it's because you weren't careful about the last change you made.

Comments
They should pin your formatting thread to the top of the C++ section.
Nice
while (inFile){           for (n = 0; n < 7; n++){               inFile >> currentDays;                }               }

This is an infinite loop.

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

This is an infinite loop.

Only if you are reading from an infinite file.

Or if the file is empty.

It's not an infinite loop even if the file is empty. It would be wrong, but it wouldn't be an infinite loop. The problem is that the OP needs to read the data into an array, but isn't.

I took the code and tried to compile it.That's not his only problem.And i tried the while loop with an empty file and it goes on forever.But you would know better .. won't you?

I took the code and tried to compile it.That's not his only problem.And i tried the while loop with an empty file and it goes on forever.But you would know better .. won't you?

Yes, in fact, I do know better. The code you mentioned is not an infinite loop, whether it is an empty file or not. Yeah, that's not his only problem, but it's one of his main problems.

If you try to use the >> operator on an empty file, it'll set inFile false and you'll get out of the loop. The code as he posted it may in fact run forever. I don't know. But if it does, it has nothing to do with the part you posted being an infinite loop when you use an empty file or a non-empty file. On a NON-empty file, it'll likely crash before it even gets to that code since he's trying to use an array subscript on something that isn't an array earlier in the program.

Or if it got to that loop and ran into something in the text file that wasn't an integer, you could have an infinite loop due to failure of the >> operator or something, but that would have to do with the text file being incorrect or something incorrect in the earlier code.

So yes, I do know better, Try the program below on an empty file. It'll run just fine.

#include <iostream>
#include <fstream> // Header to use input and output
#include <string> 

using namespace std;

int main()
{
	ifstream inFile;
	int n, currentDays;

	inFile.open ("C://EmptyFile.txt");

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

	return 0;
}

Yeah you're right .. i've tried it with cout << "looping"; instead of inFile >> currentDays;. I guess using the stream actually validates it.My mistake

This article has been dead for over six months. Start a new discussion instead.