By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,028 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,028 IT Pros & Developers. It's quick & easy.

I need help fixing my program!

P: 36
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.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8.  
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13.  
  14. {
  15.     int numberOfPassengers;
  16.     int month;
  17.     int day;
  18.     int year;
  19.     char junkChar;
  20.     string lastName;
  21.     string firstName;
  22.     int theNumberOfBags;
  23.     int weightOfBags;
  24.     int lengthOfBags;
  25.     int heightOfBags;
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.     ifstream inFile;
  35.     inFile.open("F:\\data3.txt");
  36.  
  37.  
  38.     for (int i=1; i<=numberOfPassengers; i++)
  39.     {
  40.         inFile >> month;
  41.         inFile >> junkChar;
  42.         inFile >> day;
  43.         inFile >> junkChar;
  44.         inFile >> year;
  45.         inFile >> lastName;
  46.         inFile >> firstName;
  47.         inFile >> theNumberOfBags;
  48.         inFile >> weightOfBags;
  49.         inFile >> lengthOfBags;
  50.         inFile >> heightOfBags;
  51.  
  52.     }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.         inFile.close();
  63.  
  64.         return 0;
  65. }
  66.  
  67.  
Oct 22 '08 #1
Share this Question
Share on Google+
10 Replies


boxfish
Expert 100+
P: 469
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. numberOfPassengers is uninitialized. You probably want to give it some value in your program, or read it from the file.
Hope this helps.
Oct 22 '08 #2

P: 36
sorry about that,Ok i add the [/code]

thanks
Oct 22 '08 #3

P: 36
hi,
i tried reading numberofpassengers 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:


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <fstream>
  6. #include <cmath>
  7.  
  8.  
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.  
  15.     int month;
  16.     int day;
  17.     int year;
  18.     char junkchar;
  19.     string lastName;
  20.     string firstName;
  21.     int theNumberOfBags;
  22.     int weightOfBags;
  23.     int lenghtOfBags;
  24.     int heightOfBags;
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.     ifstream inFile;
  34.     inFile.open("F:\\data3.txt");
  35.  
  36.  
  37.     while (!infile.eof());
  38.     {
  39.         inFile >> month;
  40.         inFile >> junkChar;
  41.         inFile >> day;
  42.         inFile >> junkChar;
  43.         inFile >> year;
  44.         inFile >> lastName;
  45.         inFile >> firstName;
  46.         inFile >> theNumberOfBags;
  47.         inFile >> weightOfBags;
  48.         inFile >> lengthOfBags;
  49.         inFile >> heightOfBags;
  50.  
  51.     }
  52.  
  53.  
  54.  
Oct 22 '08 #4

P: 36
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. numberOfPassengers 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 numberofpassengers 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"

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <iostream> 
  4. #include <iomanip> 
  5. #include <string> 
  6. #include <fstream> 
  7. #include <cmath> 
  8.  
  9.  
  10.  
  11. using namespace std; 
  12.  
  13. int main() 
  14.  
  15.     int month; 
  16.     int day; 
  17.     int year; 
  18.     char junkchar; 
  19.     string lastName; 
  20.     string firstName; 
  21.     int theNumberOfBags; 
  22.     int weightOfBags; 
  23.     int lenghtOfBags; 
  24.     int heightOfBags; 
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.     ifstream inFile; 
  34.     inFile.open("F:\\data3.txt"); 
  35.  
  36.     while (!infile.eof()); 
  37.     { 
  38.         inFile >> month; 
  39.         inFile >> junkChar; 
  40.         inFile >> day; 
  41.         inFile >> junkChar; 
  42.         inFile >> year; 
  43.         inFile >> lastName; 
  44.         inFile >> firstName; 
  45.         inFile >> theNumberOfBags; 
  46.         inFile >> weightOfBags; 
  47.         inFile >> lengthOfBags; 
  48.         inFile >> heightOfBags; 
  49.  
  50.  
  51.     } 
  52.  
  53.  
  54. return 0;
  55.  
  56. }
  57.  
  58.  
Oct 22 '08 #5

boxfish
Expert 100+
P: 469
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.
Oct 22 '08 #6

P: 36
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.
Oct 23 '08 #7

boxfish
Expert 100+
P: 469
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.
Oct 23 '08 #8

P: 36
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




Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5. #include <cmath>
  6.  
  7.  
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.  
  14.     string dateForPassenger1;
  15.     string bagInfoForPassenger1;
  16.     int monthForPassenger1;
  17.     int dayForPassenger1;
  18.     int yearForPassenger1;
  19.     char junkChar;
  20.     string lastNameForPassenger1;
  21.     string firstNameForPassenger1;
  22.     int numberOfBagsForPassenger1;
  23.     int weightOfBagsForPassenger1;
  24.     int lengthOfBagsForPassenger1;
  25.     int heightOfBagsForPassenger1;
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.     ifstream inFile;
  35.     inFile.open("F:\\data3.txt");
  36.     //ofstream outFile;
  37.     //outFile.open("F:\\charges.txt");
  38.  
  39.  
  40.     while (!inFile.eof())
  41.     {
  42.         if (dateForPassenger1=="")
  43.         {
  44.             inFile >> monthForPassenger1 >> junkChar >> dayForPassenger1 >> junkChar >> yearForPassenger1;
  45.             inFile >> lastNameForPassenger1 >> firstNameForPassenger1;
  46.  
  47.             cout << monthForPassenger1 << junkChar << dayForPassenger1 << junkChar << yearForPassenger1;
  48.             cout << lastNameForPassenger1 << firstNameForPassenger1; 
  49.         }
  50.         else 
  51.         {
  52.             cout << "write out date only once";
  53.         }
  54.  
  55.         if (bagInfoForPassenger1=="")
  56.         {
  57.             inFile >> numberOfBagsForPassenger1 >>weightOfBagsForPassenger1 >>lengthOfBagsForPassenger1  >> heightOfBagsForPassenger1;
  58.             cout <<numberOfBagsForPassenger1 << weightOfBagsForPassenger1 << lengthOfBagsForPassenger1 << heightOfBagsForPassenger1 << endl;
  59.         }
  60.         else 
  61.         {
  62.             cout << "write out bag information only once";
  63.         }
  64.         inFile.close();
  65.  
  66.     }
  67.  
  68.  
  69.  
  70.         inFile.close();
  71.  
  72.         return 0;
  73. }
  74.  
  75.  
  76.  
Oct 23 '08 #9

boxfish
Expert 100+
P: 469
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.
Oct 23 '08 #10

P: 2
hi
use getline function to get whole line from file using while loop
above while loop use seekg() function
then print the line
Oct 24 '08 #11

Post your reply

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