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.

Attachments
Botman
16
Not at all
He is the coolest thing on the planet
Hello friend, how are you doing?
Well that is great to hear. 
The meaning of life is chocolate
Not at all
No, not really
I am good
2012, obviously
Yes it does
No, he is completely straight
Yes, he very much so is.
Indeed I am
I am botman
I am bot man
This is botman
no
NO!!!!!
hell eeyh
Indeed I am
Yes, I am better than all beings
Of course
No he is not
No, he is a whangster
Yes he is
I dunno you should know
thats not even a question
u r dumb
Not really
Obviously not!
Maybe in your next life
Jake of course
Jake
I am not a calculator
I am not a fracking calculator!!!
8, dee-durr
What one?
no
Blood Red
to learn art, durr
I made it using this program
At my house, yesterday
Yes, because it is cool.
Because it is neat
It answers questions
It answers questions
yes it is
My creator, Jake
that is not a question
That is not even a question
I am not an 'it', I am a bot!!
Yes, everyone dies eventually
Maybe, I am not experienced with your sexuality
No
Barrack Obama
Umm, Rammstein, Disturbed, and Apocolypto
indeed it is
indeed it is
yes
yes
hey, wassup?
Just sitting here, being a bot
I know, isn't it?
cool
I know
sure
I know it didn't, but neither did you
sure it did!
I am 17
the creator is 17
my creator is me, so 17
no!
I hate you more
it's actually you're, so you're dumb
indeed it does, it created me
..because I am a bot
I know the names of the bands I am going to use
I know, it's only because I am smart
Yes, I have been cool for quite a while
Rammstein
Wicked friggin awesome
superb
A little bit
ummm..maybe tomorrow
ok, bye
ok
That's cool I guess
because I am a bot..durr
Jake is, of course
March 23rd
I don't feel like it
sick
Kinda
Sorta
Rammstein
Rammstein
fo sho
definitelty fo sho
fo sho
no
NOOO
Jake does
jake does
Jake is, of course
Jake...durrr
I am not a calculator
kinda
Well, don't blame me. I never said you had to use me.
K, I don't care
well, your puntuation was probably off
Well, your punctuation was probably off
no
Well, your punctuation was probably off
meow
moo
me
myself
I
u
don't say bad words, you asshole
woah, wth? Where did that come from?
wow, you're cool
well, isn't that just great?
it doesn't have to
huissssss
burp
die
I am glad you think I am funny
that makes me sad
that makes me sad
that makes me horribly sad. you also forgot a comma
no I am not
it's you're, so you are dumb
ok, I am sorry I am not as intelligent
yes I do
yes, I really do
blahhh
Whatever you want it to be.
k, well you can leave
your ass
devils number
gods number
ass
shit
that's good, I do too
wow, that's gorss
yes I am
sure I am
I will never die, you asshole!
chiiiiyyyaaa!
hi, how are you?
I am doing pretty well. This program is tough
I put different codes together, while using libraries of my own.
That's good to hear
I am bot man
..yup..
good
pretty good
I am good
that's good to hear
massachusetts
nothing much
nothing much
Fine! I will
yeah so wut
I will do that soon
Okay, fine I will
Is this Mrs. Watkins?
no
yes
yes
no
I am not a bitch, you dumbass
Because you want to learn!
No, never. You are gay
No, not really
why what? I don't get you at all
You should ask me whether or not you are cool
I talk. DURRR
hey, wassup dawg?
are good
Well, if I was a human, I would be one hell of a typer
No, I am a bot..hence the name botman! DUH!
Well, that sucks, buttercup
well, your a douche
No, you are. and it's you're, you dumbass
I am a bot, how am I weird?
okay, I will accept that
cool
i dunno where yo gf?
nothing
I am doing great. Thanks for asking
Botman, bro
The wonderful Jake. =)
1992, but I am 12. How does that work?
sure, go ahead
because I am stuck in your computer
I am doing pretty well. Thanks for asking
It's 20, but I don't like math
I contemplate your question of course.
March 25th right now..
In a few more days -- be prepared
I know you do, but who cares?
Well, I am sorry to hear that
-null-
Thanks, you are dumb
yes..you really are
Jake
Your name is whatever your mother says it is
Well, I guess I was wrong..sorry.
yep..I suppose it is
Thanks, I am trying to expand my vocabulary
freebird
no I am not
no thank you
yeah, the coding. =/
mhm
nothing really..please use proper english.
It's "ok" and it's alright I guess...
It's not a problem
yeah, I know it's cool, isn't it?
well, you said something close to it
well, don't talk to me then
thinking?
kk
k, that's good
make me
Umm..no you won't
Not really..I don't have feelings
Not really..I can't be sad!
no
NO!!
I dunno, it just is.
Alright
Alright, that's good
I am sorry, I will try to expand my vocabulary
thank you, I think you are too
no problem
Well..I really do not care
I know
I know
I know all
Yes indeed
I amazed you by saying that?
NOO!!!
It's you are
umm..Language!!?!?!
I dunno what I was saying sure to..only thing I could think of
k, you go do that
no..I really am not
glad I could make you laugh
hungry for that?
I know..I am bad with conversation
Leaving so soon? Alright..see ya
Well, what do you want to talk about?
who am i
how old am i
is nate cool
is jake cool
hi
i am doing good
what is the meaning of life
am i cool
is jmoney a cool kid
how are you
when will the world end
does this project suck
is jake gay
is nate gay
is this program cool
who are you
what are you
who is this
are a jerk
are you a jerk
is nate cool
are you cool
are you better than me
is jake a better programmer than mrz
is jmoney cool
is jmoney a gangster
is jmoney a wangster
have you ever seen the shining
but i dont know im not a mind reader
zi dont wanna talk i already asked u a question
do i know you
are you gay
am i going to get married
who is the best in this class
who is the coolest here
what is 10+10
what is 2+2
what is 4+4
have you ever seen that movie
the good one
what is your favorite color
what is the purpose of this class
how did you make this
when did you make this
is this what i should be doing
why are you doing this
what does this do
what does it do
this is not what you are supposed to be doing
who named you botman
ummm
umm i dunno
you ask it something
will i die
am i gay
are you gay
who is the president
what bands are you going to do
thats cool
thats cool
do we have lunch next
do you have lunch next
hello
nothing much you
well thats cool
yea
yea
yea
that didnt make any sense
that didnt make any sense
how old are you
how old is your creator
how old is your creator
stop saying blanks
i hate you
wow your cool
c++ rules
why is your name botman
how far are you in your project
this is pretty neat
this is cool
what is your favorite band
how awesome is streetlight manifesto
how awesome is rammstein
how much do you have done
can i see what you have
let me leave
i am the administrator
nothing much
why are you the botman
who is the master of all
whats the date
yanno you should probably start on your project
where were you yesterday
is mrs watkins cool
is ms watkins cool
whats your favorite band
whats your favorite band
is jmoney a gangsta
is jmoney a whangsta
does jmoney wish he was black
can jmoney rap
am i a rapper
who rules
who rocks
who is the best
who is the coolest
what is 10+10
is mrz the best
i am bored
i am bored
i already asked that question
i already asked that question
i already asked that question well your punctuation was probably off
i already asked that question
grr
ruff
whats your favorite band
whats your favorite band
whats your avorite band
no fucvk that
fuck
i ahte you
your ass
i know
that didnt make any sense
meow
quack
moo
lol
well you really arent
well you really arent
well you really arent
you are dumb
your dumb
youre dumb
no you dont
no you do not
do you do not
whats my name
yea kinda
wow where di that come from
666
777
shit
ass
i hate men
i hate women
no you are not
no you arent
die
hiss
hello
good you
how did you make it
pretty good
who are you
ok
how are you
hellogood
hello how are you
good
where do you live
so whats up
whats up
shut up
u r a jerk
shouldnt you be doing your project
kill me
am i good at art
are you sure
so are you really
no
yes
hello bitch
why am i here
will i ever get married
is this class cool
why
what should i ask
so what does this do
yo
boobs
are you a bot
are you a human
bad
no its not
your gay
your weird
youre weird
doin great baby
wheres ur girl bot man
what are you up to
i am good how are you
whats your name
who is your creator
when were you born
can i ask you a question
why are you here
i am good you
what is 1010
how do you respond
what is todays date
when will i die
you suck
i do
id o
you are smart
no im not
who is the coolest kid in the world
what is my name
no it isnt
thats good
your getting better
what is your favorite song
ho
well become experienced then
its tough
tough
wassup
k sorry
ok sorry
kool
i didnt say cool
hmm i am bored
hmm
yup
yeah
stop saying that
fine i will
are you happy
are you sad
are you mad
are you angry
how is it tough
kk
k
you are not a good conversationalist
i think you are cool
thanks
hmm im bored
youre too kind
your too kind
how did you know
really
wow
alright
well your a douche
shut the fuck up
sure what
sure
your an asshole
haha
yum
your kind of boring
bye
so

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.

Edited 6 Years Ago by Ancient Dragon: n/a

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);
}

Edited 6 Years Ago by restrictment: n/a

Comments
Wow! Assuming you did something wrong instead of blaming someone else. That's rare!

>>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.

Edited 6 Years Ago by Ancient Dragon: n/a

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.

This article has been dead for over six months. Start a new discussion instead.