I need help fixing my program!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charmeda103
    New Member
    • Oct 2008
    • 36

    I need help fixing my program!

    I need help with my program its not reading information from my file



    here is the code i have:
    how do i fix it to make it read the file.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cmath>
    
    
    
    using namespace std;
    
    int main()
    
    {
    	int numberOfPassengers;
    	int month;
    	int day;
    	int year;
    	char junkChar;
    	string lastName;
    	string firstName;
    	int theNumberOfBags;
    	int weightOfBags;
    	int lengthOfBags;
    	int heightOfBags;
    
    
    
    	
    	
    
    
    
    	ifstream inFile;
    	inFile.open("F:\\data3.txt");
    	
    
    	for (int i=1; i<=numberOfPassengers; i++)
    	{
    		inFile >> month;
    		inFile >> junkChar;
    		inFile >> day;
    		inFile >> junkChar;
    		inFile >> year;
    		inFile >> lastName;
    		inFile >> firstName;
    		inFile >> theNumberOfBags;
    		inFile >> weightOfBags;
    		inFile >> lengthOfBags;
    		inFile >> heightOfBags;
    
    	}
    
    
    
    
    
    	
    	
    	
    
    		inFile.close();
    
    		return 0;
    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Can you please post the file you are reading from so that I can test your program? Also, the closing tag has to be a [/CODE], not just another [CODE]. Thanks.
    Edit:
    Ah, I think I see what's wrong. numberOfPasseng ers is uninitialized. You probably want to give it some value in your program, or read it from the file.
    Hope this helps.

    Comment

    • charmeda103
      New Member
      • Oct 2008
      • 36

      #3
      sorry about that,Ok i add the [/code]

      thanks

      Comment

      • charmeda103
        New Member
        • Oct 2008
        • 36

        #4
        hi,
        i tried reading numberofpasseng ers but still not working,
        i tried using a different way by using a while loop and it still wont work.


        my while loop looks like this:


        Code:
        #include <iostream>
        #include <iomanip>
        #include <string>
        #include <fstream>
        #include <cmath>
        
        
        
        using namespace std;
        
        int main()
        {
        
        	int month;
        	int day;
        	int year;
        	char junkchar;
        	string lastName;
        	string firstName;
        	int theNumberOfBags;
        	int weightOfBags;
        	int lenghtOfBags;
        	int heightOfBags;
        
        
        
        	
        	
        
        
        
        	ifstream inFile;
        	inFile.open("F:\\data3.txt");
        
        
        	while (!infile.eof());
        	{
        		inFile >> month;
        		inFile >> junkChar;
        		inFile >> day;
        		inFile >> junkChar;
        		inFile >> year;
        		inFile >> lastName;
        		inFile >> firstName;
        		inFile >> theNumberOfBags;
        		inFile >> weightOfBags;
        		inFile >> lengthOfBags;
        		inFile >> heightOfBags;
        
        	}

        Comment

        • charmeda103
          New Member
          • Oct 2008
          • 36

          #5
          Originally posted by boxfish
          Can you please post the file you are reading from so that I can test your program? Also, the closing tag has to be a [/CODE], not just another [CODE]. Thanks.
          Edit:
          Ah, I think I see what's wrong. numberOfPasseng ers is uninitialized. You probably want to give it some value in your program, or read it from the file.
          Hope this helps.


          [Quote]
          hi i tried writing numberofpasseng ers to the file and still wouldnt work, so i tried a while to read the file in that worked but I realzied that i need to read the file one information at one time and not all has one. then I got nothing. can you please see what I am doing wrong in the program.


          here is what my program looks like switching from the for loop to using the while loop"

          Code:
          
          #include <iostream> 
          #include <iomanip> 
          #include <string> 
          #include <fstream> 
          #include <cmath> 
            
            
            
          using namespace std; 
            
          int main() 
          { 
            
              int month; 
              int day; 
              int year; 
              char junkchar; 
              string lastName; 
              string firstName; 
              int theNumberOfBags; 
              int weightOfBags; 
              int lenghtOfBags; 
              int heightOfBags; 
            
            
            
            
            
            
            
            
              ifstream inFile; 
              inFile.open("F:\\data3.txt"); 
            
              while (!infile.eof()); 
              { 
                  inFile >> month; 
                  inFile >> junkChar; 
                  inFile >> day; 
                  inFile >> junkChar; 
                  inFile >> year; 
                  inFile >> lastName; 
                  inFile >> firstName; 
                  inFile >> theNumberOfBags; 
                  inFile >> weightOfBags; 
                  inFile >> lengthOfBags; 
                  inFile >> heightOfBags; 
          
            
              } 
          
          
          return 0;
          
          }

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            So, what do you mean by won't work? Do you mean that you printed out the values that these variables contain, and they are uninitialized? I don't see a reason for a loop. You don't have an array of passengers, so you can't store more than one set of passenger information. If you have to store data from a lot of passengers, I suggest making a struct passenger, and then having an array or vector of those. But first things first; in what way does it not work?
            Edit:
            To make the quote tag work, it has to end with [/QUOTE]. Thanks.

            Comment

            • charmeda103
              New Member
              • Oct 2008
              • 36

              #7
              i have to read information from a file and each line has its own variable.
              when I run the program all its says is press any button to continue.
              when the program runs the output has to look like this

              01/05/2009 passenger1 2 21.5 24.2 18 6 30 26 20.5 7.5
              11/16/2009 passenger2 3 50.8 32 26 9.3 12.4 15 21.2 5.5 42.6 23.1 35 7
              05/28/2009 passenger3 2 31.5 22.6 20 6.8 22.4 28 30 8.5
              07/25/2009 passenger4 1 71.3 30.6 28 10.2
              Each of the informatin in the file has to read separtley when writtng the code to the program.
              i hope that makes better sense to what the program is supposed do.

              Comment

              • boxfish
                Recognized Expert Contributor
                • Mar 2008
                • 469

                #8
                Okay, now I see why you have that loop, and why you don't need an array. So can we please see the code where you print all this information out? Thanks.

                Comment

                • charmeda103
                  New Member
                  • Oct 2008
                  • 36

                  #9
                  ok here is the code, This is what i have so far. the code keeps on repeating and not stoping. i have to the ouput look like this:
                  01/05/2009 passenger1 2 21.5 24.2 18 6 30 26 20.5 7.5
                  11/16/2009 passenger2 3 50.8 32 26 9.3 12.4 15 21.2 5.5 42.6 23.1 35 7
                  05/28/2009 passenger3 2 31.5 22.6 20 6.8 22.4 28 30 8.5
                  07/25/2009 passenger4 1 71.3 30.6 28 10.2




                  Code:
                  #include <iostream>
                  #include <iomanip>
                  #include <string>
                  #include <fstream>
                  #include <cmath>
                  
                  
                  
                  using namespace std;
                  
                  int main()
                  {
                  	
                  	string dateForPassenger1;
                  	string bagInfoForPassenger1;
                  	int monthForPassenger1;
                  	int dayForPassenger1;
                  	int yearForPassenger1;
                  	char junkChar;
                  	string lastNameForPassenger1;
                  	string firstNameForPassenger1;
                  	int numberOfBagsForPassenger1;
                  	int weightOfBagsForPassenger1;
                  	int lengthOfBagsForPassenger1;
                  	int heightOfBagsForPassenger1;
                  
                  	
                  	
                  	
                  
                  
                  	
                  
                  	ifstream inFile;
                  	inFile.open("F:\\data3.txt");
                  	//ofstream outFile;
                  	//outFile.open("F:\\charges.txt");
                  	
                  	
                  	while (!inFile.eof())
                  	{
                  		if (dateForPassenger1=="")
                  		{
                  			inFile >> monthForPassenger1 >> junkChar >> dayForPassenger1 >> junkChar >> yearForPassenger1;
                  			inFile >> lastNameForPassenger1 >> firstNameForPassenger1;
                  			
                  			cout << monthForPassenger1 << junkChar << dayForPassenger1 << junkChar << yearForPassenger1;
                  			cout << lastNameForPassenger1 << firstNameForPassenger1; 
                  		}
                  		else 
                  		{
                  			cout << "write out date only once";
                  		}
                  
                  		if (bagInfoForPassenger1=="")
                  		{
                  			inFile >> numberOfBagsForPassenger1 >>weightOfBagsForPassenger1 >>lengthOfBagsForPassenger1  >> heightOfBagsForPassenger1;
                  			cout <<numberOfBagsForPassenger1 << weightOfBagsForPassenger1 << lengthOfBagsForPassenger1 << heightOfBagsForPassenger1 << endl;
                  		}
                  		else 
                  		{
                  			cout << "write out bag information only once";
                  		}
                  		inFile.close();
                  
                  	}
                  		
                  		
                  
                  		inFile.close();
                  
                  		return 0;
                  }

                  Comment

                  • boxfish
                    Recognized Expert Contributor
                    • Mar 2008
                    • 469

                    #10
                    You have put inFile.close() inside of your while loop as well as outside. I'm sure you don't want to be closing the file with every passenger. Get rid of the close in the while loop.
                    Hope this helps.

                    Comment

                    • vj6940
                      New Member
                      • Oct 2008
                      • 2

                      #11
                      hi
                      use getline function to get whole line from file using while loop
                      above while loop use seekg() function
                      then print the line

                      Comment

                      Working...