Well, I had my program running perfectly, and I was quite pleased with the results. However, after I made the text file a bit big, the program just stopped working all together. (255 lines of text) so I 'suppose' it is a bandwidth problem..however 255 lines of text is like nothing, and I don't understand why it would render the program useless.

Any help on fixing the issue will be appreciated. Here is the coding:

#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{
	char user[255]="";
	do{
	int j=0,l=0;
	char newuser[255]="";
	cout << "Talk: ";
	cin.getline (user,255);
	for(int x=0;x<strlen(user);x++)
	{
		if ( user[x] >= 'A' && user[x] <= 'Z' )
			user[x] = static_cast<char> ( user[x] + 'a' - 'A' );
		if (ispunct(user[x])==0) 
		{
			newuser[j]=user[x];
			j++;
		}
	}
	for(int x=0;x<255;x++)
		user[x]=newuser[x];
	char question[255][255], answer[255][255], userans[255];
	int ans=0, x=0;
	ifstream fromfile("questions.txt");
	for(x=1;x<265;x++)
	{
		fromfile.getline(question[x], 255);
		if(strcmp (question[x], user) == 0)
			ans = x;
	}
	fromfile.close();
	ifstream tofile("answers.txt");
	for(x=1;x<265;x++)
		tofile.getline(answer[x], 255);
	tofile.close();
	if(ans==0)
	{
		cout << "I am sorry, but I do not know. What would your answer be?: ";
		cin.getline(userans,255);
		ofstream intofile("answers.txt",ios::app);
		intofile << endl << userans;
		intofile.close();
		ofstream tofile("questions.txt",ios::app);
		tofile << endl << user;
		tofile.close();
	}
	else
		cout << "Botman says: " << answer[ans] << endl;
	}while(strcmp ("let me leave", user)!= 0);
}

And the text files are below.

Recommended Answers

All 5 Replies

lines 26 and 36: >> for(x=1;x<265;x++)

Why are you hard coding the number 265 here? Just read the file until end-of-file is reachec

while( x < 255 && fromfile.getline(question[x], 255) )
{
  ++x;
}

You would be better off using an array of std::string objects instead of those two 2d character arrays, assuming you are allowed to use them.

The loop at lines 28-33: Why read the entire file just to find the answer to the question? Once the question is found (line 32) there is no point reading any more lines from the file. And why are you storing the read lines in memory? No point doing that either if you don't need to use them later in the program.

so I 'suppose' it is a bandwidth problem..

Bandwidth has nothing to do with it. You need to use more precise terms.

however 255 lines of text is like nothing

Could be nothing, could be something. If you reserved space for 254 lines, it would be something.

and I don't understand why it would render the program useless.

Define "useless".

Any help on fixing the issue will be appreciated.

What exactly is the issue? Or rather, what is the symptom? Nailing down the exact behavior is important.

Error messages? Does it "crash"? Where?

lines 26 and 36: >> for(x=1;x<265;x++)

Why are you hard coding the number 265 here? Just read the file until end-of-file is reachec

while( x < 255 && fromfile.getline(question[x], 255) )
{
  ++x;
}

You would be better off using an array of std::string objects instead of those two 2d character arrays, assuming you are allowed to use them.

The loop at lines 28-33: Why read the entire file just to find the answer to the question? Once the question is found (line 32) there is no point reading any more lines from the file. And why are you storing the read lines in memory? No point doing that either if you don't need to use them later in the program.

Well, it is not class work. I had one semester of 'high school' programming and that is all the experience I have had. I liked the class a lot, so I decided to just make a project of my own and try to figure it out as I go along. I find it is the easiest way for me to learn..from my mistakes. (50% of the stuff in this program I was not taught in class.)

Anyway, thanks for showing me the easier way to read to the end of the file. =)
And as for the other stuff, I dunno why I did it or how to correct it.

Edit:
actually, I tested out your code, and it didn't seem to work..not sure why. I probably implemented it wrong. Here is what I tried to do:

#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{
	char user[255]="";
	do{
	int j=0,l=0;
	char newuser[255]="";
	cout << "Talk: ";
	cin.getline (user,255);
	for(int x=0;x<strlen(user);x++)
	{
		if ( user[x] >= 'A' && user[x] <= 'Z' )
			user[x] = static_cast<char> ( user[x] + 'a' - 'A' );
		if (ispunct(user[x])==0) 
		{
			newuser[j]=user[x];
			j++;
		}
	}
	for(int x=0;x<255;x++)
		user[x]=newuser[x];
	char question[255][255], answer[255][255], userans[255];
	int ans=0, x=0;
	ifstream fromfile("questions.txt");
	while( x < 255 && fromfile.getline(question[x], 255) )
	{
		fromfile.getline(question[x], 255);
		if(strcmp (question[x], user) == 0)
			ans = x;
		x++;
	}
	fromfile.close();
	ifstream tofile("answers.txt");
	while( x < 255 && fromfile.getline(question[x], 255) )
	{
		tofile.getline(answer[x], 255);
		x++;
	}
	tofile.close();
	if(ans==0)
	{
		cout << "I am sorry, but I do not know. What would your answer be?: ";
		cin.getline(userans,255);
		ofstream intofile("answers.txt",ios::app);
		intofile << endl << userans;
		intofile.close();
		ofstream tofile("questions.txt",ios::app);
		tofile << endl << user;
		tofile.close();
	}
	else
		cout << "Botman says: " << answer[ans] << endl;
	}while(strcmp ("let me leave", user)!= 0);
}
commented: Wow! Assuming you did something wrong instead of blaming someone else. That's rare! +11

>>user[x] = static_cast<char> ( user[x] + 'a' - 'A' );

There is a lot easier way to do that too. user[x] = tolower(user[x]); line 30: That is reading the file twice (see line 28). Delete line 30.

Should be

...
x = 0;
ifstream tofile("answers.txt");
while( x < 255 && fromfile.getline(question[x], 255) )
...

Also, it is not quite good turn to call fromfile.getline() when you have closed it with fromfile.close() on line 35.

I think it will be better to reproject your code from the start, removing '255' constant from everywhere. Hardcoded constant always limits you, so you will rewrite your code everytime you want to enlarge your app's features.
And things like

char question[255][255]

make your code to be a good shelter for buffer overflow issues. Think about code safety now while you are learning C/C++, it will save hours of your time in the future. Consider std::string for now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.