0

Hey guys:

I'm having trouble breaking out of a loop that takes input from a file. I can only break it if I make a code that specifies how many inputs there are and makes it stop when it reaches the max input. I think it has to do with me using these in class default constructors, but this is how it has to be done for my assignment. This is a program that simulates a messaging system.

Basically .eof(), .fail(), none of that is working


This is my first class:

MailSystem::MailSystem() // class constructor that creates a vector full of all message objects
{
	ifstream fin; // declaring fin
	fin.open("mail.txt"); // this is the text file
	// int count = 0; // this count is used to break the loop with cheat code below

	while (!fin.eof())
	{
		// if (count == 20) break;  /* when this is uncommented, it works, because there are only 20 messages in the text file. */
		Message createMessageInstance(fin); // creates instance and passes fin to class below
		systemMessages.push_back(createMessageInstance); /* this is a vector previously declared and it pushes back all the messages one by one */
		// count++; // increment count to cheat and bypass
	}
}

This is the class that the first class refers to, the objects here create individual objects with variables containing components of a message:

Message::Message(istream& fin) // this is the class constructor that creates individual message objects through initialization
{
	fin >> messageNum;  // takes input into all of these variables
	getline(fin, status);
	getline(fin, sender);
	getline(fin, recipient);
	getline(fin, subject);

	while (message != "EOF")  // this last code gets string for message body
	{
		messageBody.append(message);
		if (messageBody != "")
		{
			messageBody.append("\n");		
		}
		getline(fin, message);		
}

And this is the textfile that fin reads from, you don't really have to read this. First number is message number, second char is status (R for read, N for New), second line is sender, 3rd line is recipient, 4th line is subject, 5th line until "EOF" is the body of the message. Just skip to the bottom, don't bother reading:

1 R
Jemma
Harry
What were you thinking?
Harry,

You seriously need to stop claiming your paperclip orders under
the heading art supplies.

Jemma
EOF
2 R
Jemma
Jason
I really really want to talk to you about those expense claims!
Subject says it all...

Jemma
EOF
3 R
Jason
Jemma
New copies in your mailbox.
EOF
4 R
Bob
Harry
True Story
Hi Harry,

Dell is selling computers with key loggers.  Apparently the RCMP
aked them to!

Bob
EOF
5 R
Harry
Jemma
Art is Art
It's not like I actually use them for work though...
EOF
6 R
Harry
Jason
True Story!
Dell is selling computers with key loggers.  Apparently the RCMP
aked them to!
EOF
7 R
Bob
Jemma
I can’t believe I ate the whole thing.
Hi Jemma,

Your cake was delicious.  I actually ate the whole thing last night.
What was your secret ingredient?  You promised you’d tell me after I
ate it.

Bob
EOF
8 R
Jemma
Jason
Expense Report
Jason,

The expense report you filled out was missing a 356B attachment.
I'm sure you just forgot it.  Please bring it over to my office
when you can.

Jemma
EOF
9 R
Jason
Harry
Not True
Hi Harry,

According to snopes.com, that isn't true.

Jason
EOF
10 R
Jason
Jemma
Expense Report, Attempt 2
Hi Jemma,

Sorry about that, I thought that the 67FGH report cover included
that information...  The new version is in your mailbox.

Jason
EOF
11 R
Harry
Bob
Not True
Actually, Bob, according to Snopes that isn't true.
EOF
12 R
Bob
Jason
Dinner Thursday
Hi Jason,

I don’t think we can make it to dinner on Thursday‐‐‐Julia’s knees
aren’t what they used to be.  Maybe next week?

Bob
EOF
13 R
Jemma
Bob
Secret Ingredient
Bob,

The secret ingredient is love...  just kidding.  It's really tuna!
I was doubtful myself until I tried it.

Jemma
EOF
14 R
Jason
Jon Jacob Jingleheimer Schmidt
Challenge!
I challenge you to a duel.
EOF
15 R
Bob
Jemma
What!?!
Hi Jemma,

You've got to be kidding, right?

Bob
EOF
16 N
Jemma
Bob
Expense Report
Bob,

Did you turn in your expense report?  I'm not seeing it in my inbox.

Jemma
EOF
17 N
Jon Jacob Jingleheimer Schmidt
Jason
Oh, Really...
You're on!
EOF
18 N
Jemma
Bob
Seriously.

EOF
19 N
Bob
Harry
Snopes.com
Hi Harry,

Thanks for the information about snopes.com, I hadn't heard of that site
before.

Bob
EOF
20 N
Jason
Bob
Dinner Next Week
Hi Bob,

Of course I knew that about Julia, I’m sorry that I didn’t think about
that ahead of time.  How about next Wednesday you come over for
Mexican, and we’ll skip the football?

Jason
EOF

There are 20 messages in total. When I use the cheatcode that breaks the loop after 20 iterations, the whole code works perfectly, but I can't keep the code, because I need to implement a function that can compose more messages and writes them to file.

Everything gets read in perfectly, but it doesn't know when to stop and I get an infinite loop on run where it just hangs before finishing the vector. Sorry for the long post, please help. I really can't figure this out...

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by firstPerson
0
ifstream iFile("text.txt"); //open file
  /****/
  whlie(!(iFile.eof() || iFile) //while there is stuff to read
 {
    // Read content.

 }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.