Is there a really simple way to write this out to a file?

Input for this program should come from the file data3.txt. Each line of the file contains the following information, in the given order, and separated by a single blank:
• The first name of the passenger
• The last name of the passenger
• A single character indicating whether the passenger is flying economy (E) or business (B) class
• An indication of the status of the passenger
o RP – regular passenger
o MP – military passenger on personal flight
o MO – military passenger traveling on orders
• An indication of the membership in the airlines patronage club
o M1, M2, M3, or M4 – a member at levels 1, 2, 3, 4 respectively
o NO – not a member
• An integer n indicating he number of bags the passenger wishes to check
• n sets of four numbers indicating the weight, length, width, and height of each of the n bags.
Thus, the line
Mark Spitz E RP NO 2 21.5 24.2 18 6 30 26 20.5 7.5
indicates that Mark Spitz is a regular passenger travelling economy class and is not a member of the mileage club. He is checking two bags: the first weighs 21.5 lbs., and is 24.2 in. long, 18 in. wide and 6 in. high; the second weighs 30 lbs., and is 26 in. long, 20.5 in. wide and 7.5 in. high
Do you want to write it to a file (like your question title suggests) or read it from a file (like your post suggests)?

To read it from a file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdafx.h>
#include <fstream>
#include <string.h>
//#include all other headers here

int main()
{
ifstream infile("C:\\data3.txt"); //Or what ever the file path is, with doubled '\'
int k=0;
string FileContents;
	while(infile)
	{
		infile.get(k);
		FileContents=FileContents+k;
	}

//Parse the string here

exit(0);
}


To write it to a file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <string.h>
//#include any other headers
using namespace std;

int main ()
{
class Passenger {
    public:
    string FName, LName; //and etc.
  }

  //Declare Passengers here
  Passenger P1;
  P1.FName="John";
  P1.LName="Smith";
  //and etc.
  Passenger P2;
  P2.FName="Mary";
  P2.LName="Smith";
  //and etc.

  ofstream myfile;
  myfile.open ("C:\\data3.txt"); //Same file path stuff as above
  myfile << P1.FName << " " << P1.LName; //and etc.
  myfile << endl;
  myfile << P2.FName << " " << P1.LName; //and etc.
  myfile.close();
  exit(0);
}


Hope that's helpful! (and that it works!)

Numeri
@Numeri your technique for reading from the file is archaic and overcomplicated ;) C++ allows much simpler ways of doing file I/O.
The program I'm working on is a lot bigger than that part but I was having a hard time with it. I'm reading information from a file I read INTO the program. Then I'm taking the information and outputing it into two separate files. Each file requiring different things.
@made in silence: what have you got so far in terms of reading from the file? (tip: don't use what Numeri posted)
All I have so far is:

// Erin McDermott, Project 3, Purpose is to produce two seperate reports,the first should contain an itemized receipt
//listing any additional charges for each customer,# of bags checked and the number of unaccepted bags(and why)
//The second should have overall statistics for the customers checked including weight of all checked bags, total number of checked bags,
//the total number of passengers, average weight of the checked bags, and the total additional charges.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()

ifstream infile;
infile.open (data3.txt);

You're missing the opening and closing braces for main.

Also, you can open a file in the same line as you declare the file:
1
2
3
{
    std::ifstream Input ("name of file.txt");
}
I knew I forgot something. Okay, so what's a better way to write what Numeri has? It does seem a bit complicated but any help whatsoever right now is greatly appreciated.
Let's look at your problem. Firstly, we now we'll need to store multiple data sets. But what do you store them in? I recommend using a std::vector.

What will it be a list of? Well, each data set has multiple values, so I recommend a struct. Each data set also contains sub-data sets 9the baggage) so you know the struct will need a vector of those as well, which involves another struct.

We also have some things which can only be one of a few values, so I would use enumerations for those.

Here's how I would set it up:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct Passenger
{
    std::string firstname, lastname;
    enum FlightClass
    {
        Economy,
        Business
    };
    FlightClass flghtclass;
    enum MilitaryStatus
    {
        Regular,
        MilitaryPersonal,
        MilitaryOrders
    };
    MilitaryStatus military;
    enum Membership
    {
        M1, M2, M3, M4,
        NotMember
    };
    Membership membership;
    struct Baggage
    {
        double weight, length, width, height;
    };
    std::vector<Baggage> baggage;
};
I want to make sure you understand this structure before I show you the file input part.
Last edited on
Let me send you a private message so you can see the entire "Question" I'm attempting (badly) to answer.
I'm not sure what the rest of the question has to do with the input, does it have restrictions on what programming techniques you are allowed to use?
It gives a more detailed description and a few limitations on what the bags (and people) can and can't do. It's a beginning level class and the teacher apparently does not get that. I know I need a while loop, two output files, an input file and some math in there. I have no idea what enumeration and structure and vectors are. Never gone over them
OK, so what you're saying is, you don't need to store the input because you deal with is right away and move on? If so, then ignore my last post.

This is all you need:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <string>

int main()
{
    std::ifstream in ("data3.txt");
    std::string fname, lname, fclass, military, membership;
    unsigned int n;
    while(in >> fname >> lname >> fclass >> military >> membership >> n)
    { //^yes, this is really how you would write it
        double weight, l, w, h;
        for(unsigned int i = 0; i < n; ++i)
        {
            in >> weight >> l >> w >> h;

            //...deal with baggage...
        }

        //...deal with passenger...
    }
}
okay that makes sense! yay. But what do you mean when you say deal with passenger?
Ah, yes, ignore that - I did not realize at the time that your assignment only deals with the baggage. I thought there was something to deal with the passenger but not any specific baggage - I was wrong.
Oh haha okay that makes sense! Yeah it's confusing :/

How would I figure out this part?

In general, limitations on luggage are based on weight and size, where size is measured by the sum of the three dimensions – length plus width plus height. The following are one carrier’s rules concerning baggage:
Charges:
• The cost of the first checked bag is $25.
• The cost of the second checked bag is $35.
• The cost of any additional checked bag(s) is $100 per bag.
Limitations:
• A bag over 50 lbs. is considered overweight and, if it can be checked, incurs a fee over and above the checked bag fee. An overweight charge of $100 is assessed if it weighs up to 70 lbs. If its weight exceeds 70 lbs. but is less than 100 lbs., the additional fee is $200. Bags over 100 lbs. cannot be checked.
• A bag is considered oversized if it exceeds 62 linear inches. The oversize fee (also added to the checked bag fee) is $100, as long as it does not exceed 115 linear inches. Bags larger than 115 linear inches cannot be checked.
Other considerations:
• Active military on personal travel have no charge on up to 3 bags, which can be up to 70 lbs. and 62 linear inches each. If on orders and travelling in economy, they can have up to 4 bags for free which can be up to 70 lbs. and 62 linear inches each. If they are on orders and travelling in business class, up to 5 bags are free and each can be up to 70 lbs. and 115 linear inches each.
• Anyone traveling in business class can have bags up to 70 lbs. without incurring an overweight fee.
• A traveler who is a member of the airline’s club at the lowest level is allowed one checked bag up to 50 lbs. with no fee; if above the lowest level, up to 3 bags free up to 70 lbs. each.



I've thought about something along the lines of like:

b1 = 25;
b2 = 35;
bp = 100;

and then

inches = 62;
overweight = 70;
1
2
3
4
5
6
7
8
if
 inches <= 62;
bag is okay;

else
inches > 62 = overweight;

{
You should create a cost variable, then in each if statement you check each condition and add to the cost if that condition is met. Eventually the correct costs will be added.
something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
b1 = 25;
b2 = 35;
bp = 100;

string b1+b2+bp;

if
weight < 50 == check bag;
else
weight > 50 == overweight bag+bp;

overweight charge = 100;

weight > 70 = overweight charge;

weight > 70 < 115 = 200;
weight > 100 = not checked;

oversized bag = 62 inches;
inches < 62 = oversize;
inches > 62= check;
check bag + overweight charge = 100 
if 
oversized bag > 115 inches;
else 
oversized bag > 155 = not checked;


I sure hope that that is not actual C++ you're trying to write.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
unsigned int cost = 0;
if(i == 0) //first bag
{
    cost += 25;
}
else if(i == 1) //second bag
{
    cost += 35;
}
else //additional bags
{
    cost += 100;
}

if(weight > 50.0)
{
    cost += 100;
}
if(weight > 70.0) //not 'else if'
{
    cost += 100; //total 200, previous if was true
}
if(weight > 100.0) //not 'else if'
{
    //bag cannot be checked, cost is not relevant
}

if(max(max(w, l), h) > 62.0)
{
    cost += 100;
}
if(max(max(w, l), h) > 115.0) //not 'else if'
{
    //bag cannot be checked, cost is not relevant
}

//... 
I will let you figure out the rest ;)
There is a reason I have a 57% in this class...and I'm so tired lol I can hopefully figure out how to finish this tomorrow, if not I'll post more questions here
Topic archived. No new replies allowed.