Jump to content
  • Advertisement
Sign in to follow this  
wicked357

C++ Reading input file bombs...

This topic is 3158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My program keeps bombing instead of reading the rest of the input file, and I am having a hard time figuring out why, any ideas? Here is my input.txt file
main()
{
double salary;
salary = salary * 100;
}
Here is my cpp file:
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
	//input object
	ifstream source;

	//variable to store line
	string myline;
	string token = "";

	//boolean value 
	bool done = false;

	int pos = 0;

	//source.open(argv[0]);
	source.open("c:\\input.txt");
	if(!source.is_open())
	{
		cout << "cant open file!";
		return 0;
	}

	//Read til end of file
	while(!source.eof())
	{
		//get line from input file
		getline(source, myline);

		//do... while until line is read
		do
		{
			char c = myline[pos++];
			if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
			{
				token = token + c;
			}
			else if(c == '(' || c == ')' || c == '{' || c == '}' || c == ';' || c == '=' || c == '+')
			{
				cout << c;
				cout << endl;
			}
			else
			{
				cout << token << endl;
				token = "";
			}
		}while(!done);
	}

	//close input file
	source.close();

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Off the top of my head it looks like your issue is that you check for eof() before the call to getline() instead of after.

Share this post


Link to post
Share on other sites
What I can see that is wrong:

*Where do you set done to true?
*You don't reset pos

So it keeps writing stuff to the console because you are in a endless loop.

So add:

if (pos >= myline.length())
{
done = true;
}



In your loop trying to parse the token. Once again don't forget to reset done to false before you enter enter the parsing loop again.

Share this post


Link to post
Share on other sites
I did all the suggestions mentioned and it still bombs out and actually reads less then it did before, before it would output:

(
)
main
...then it bombed

now it outputs:

(
)
... then bombs


I changed the first while loop to a do... while, and I added that piece of if statement in the second now do... while loop for outputting the tokens to the console.

This is what I have now:


//Read til end of file
do
{
done = false;
//get line from input file
getline(source, myline);

//do... while until line is read
do
{
char c = myline[pos++];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
token = token + c;
}
else if(c == '(' || c == ')' || c == '{' || c == '}' || c == ';' || c == '=' || c == '+')
{
cout << c;
cout << endl;
}
else
{
cout << token << endl;
token = "";
}
if (pos >= myline.length())
{
done = true;
}
}while(!done);
}while(!source.eof());

Share this post


Link to post
Share on other sites
You're still not checking for eof() after the getline() call. If getline() fails then you still try to process the line.

Share this post


Link to post
Share on other sites
Maybe so but shouldn't it bomb out then after reading to the end of the file and trying to process and empty line? I added a if statement to check for eof before I getline and process the second do... while loop, I think that should solve that issue but I still have the same exact one, it is bombing out after ( and ) and not displaying anything else.

New code:

//Read til end of file
do
{
done = false;
if(!source.eof())
{
//get line from input file
getline(source, myline);
//do... while until line is read
do
{
char c = myline[pos++];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
token = token + c;
}
else if(c == '(' || c == ')' || c == '{' || c == '}' || c == ';' || c == '=' || c == '+')
{
cout << c;
cout << endl;
}
else
{
cout << token << endl;
token = "";
}
if (pos >= myline.length())
{
done = true;
}
}while(!done);
}
}while(!source.eof());


Share this post


Link to post
Share on other sites
Are you still checking for EOF before actually reading anything? C++ streams don't set EOF to true until after an attempt to read is made, so:

while(!eof()){
getline(source,line);
// ... do whatever ...
}




doesn't work, as, should getline() fail, nothing is done until after the loop has completed.

Have you tried changing it to something along the lines of:

while(true){
getline(source,line);
if(eof()) break;
// ... do whatever ...
}



Or, just try moving your "getline" call to before the "if(source.eof())" line.

Share this post


Link to post
Share on other sites
Awww... okay! I switched a few things around and I keep forgetting about setting pos to 0 after the line is read. Ok now I just have a few minor display problems (not errors). my output now looks like this:

(
)
{
maindouble
;
salarysalary
=

salary
+




;
Press any key to continue . . .

the output should look like this:

main
(
)
{
double
salary
;
salary
=
salary
+
100
;
}

I am going to keep messing around with this to try and get it to output correctly, but if you have anymore suggestions please feel free to let me know. I think this is just a long the lines of the else if statements if I am wrong let me know.

Here is my code now:

//Read til end of file
do
{
done = false;
//get line from input file
getline(source, myline);
if(!source.eof())
{
//do... while until line is read
do
{
char c = myline[pos++];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
token = token + c;
}
else if(c == '(' || c == ')' || c == '{' || c == '}' || c == ';' || c == '=' || c == '+')
{
cout << c;
cout << endl;
}
else
{
cout << token << endl;
token = "";
}
if (pos >= myline.length())
{
done = true;
pos = 0;
}
}while(!done);
}
}while(!source.eof());

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!