Reading/writing a string problem

T

TyPR124

I have a file that I open and take all the lines in it and store it in
file[x] where x is the line number. There should be a line that starts
with <title> and ends with </title> and can have anything in between.

for (int a=chan.start; a<=item[1].start; a++) {
if (checkLine(file[a], "<title>")) {
for (int b=7; b<=file[a].length(); b++) {
if ( (file[a].substr(b)=="</title>") || (file[a].substr(b,
file[a].length()-1), "</title>") ) {
chan.title=file[a].substr(7, b-1);
break;
}
else if (b==file[a].length()) {
cout << endl << "What is the Channel Title?" << endl;
getline(cin, chan.title);
file[a] = "<title>" + chan.title + "</title>";
break;
}
}
break;
}
else if (a==item[1].start) {
cout << "What is the Channel Title?" << endl;
getline(cin, chan.title);
insertLine(a, "<title>" + chan.title + "</title>");
break;
}

The problem is that it either deletes or adds to what chan.title
should be. Like if chan.title should be "eltit" it might say that its
"eltit</ti" or something like that. And it deletes/adds randomly
depending on the length of the title... so what is wrong with it?
Thanks.
 
T

TyPR124

Sorry, some of my code got messed up, heres what it should be

for (int a=chan.start; a<=item[1].start; a++) {
if (checkLine(file[a], "<title>")) {
for (int b=7; b<=file[a].length(); b++) {
if ( (file[a].substr(b)=="</title>") || (file[a].substr(b,
file[a].length()-1) == "</title>") ) {
chan.title=file[a].substr(7, b-1);
break;
}
else if (b==file[a].length()) {
cout << endl << "What is the Channel Title?" << endl;
getline(cin, chan.title);
file[a] = "<title>" + chan.title + "</title>";
break;
}
}
break;
}
else if (a==item[1].start) {
cout << "What is the Channel Title?" << endl;
getline(cin, chan.title);
insertLine(a, "<title>" + chan.title + "</title>");
break;
}
}
 
V

Victor Bazarov

Sorry, some of my code got messed up, heres what it should be

for (int a=chan.start; a<=item[1].start; a++) {
if (checkLine(file[a], "<title>")) {
for (int b=7; b<=file[a].length(); b++) {
if ( (file[a].substr(b)=="</title>") || (file[a].substr(b,
file[a].length()-1) == "</title>") ) {
chan.title=file[a].substr(7, b-1);
break;
}
else if (b==file[a].length()) {
cout << endl << "What is the Channel Title?" << endl;
getline(cin, chan.title);
file[a] = "<title>" + chan.title + "</title>";
break;
}
}
break;
}
else if (a==item[1].start) {
cout << "What is the Channel Title?" << endl;
getline(cin, chan.title);
insertLine(a, "<title>" + chan.title + "</title>");
break;
}
}

What do you expect from us when you don't provide declarations of
objects 'chan', 'item', nor functions 'checkLine', 'insertLine'?

V
 
T

TyPR124

I have all those declared earlier in the code, as well as opening/
reading the file, and a lot of other things. And I'm pretty sure the
problem is in that bit of code somewhere because I've tested
everything else and it works exactly as it should.
 
V

Victor Bazarov

I have all those declared earlier in the code, as well as opening/
reading the file, and a lot of other things. And I'm pretty sure the
problem is in that bit of code somewhere because I've tested
everything else and it works exactly as it should.

Imagine you bring your car to a mechanic and say, "Something is wrong
with it. I am pretty sure everything I do with it is fine, but it
just doesn't work. Can you fix it?" The mechanic says, "So, what is
it exactly do you do with it?", and your reply is, "I am not going to
tell you because it's perfectly alright, *I* know". What will the
mechanic tell you after that?

V
 
N

Nick Keighley

I have a file that I open and take all the lines in it and store it in
file[x] where x is the line number. There should be a line that starts
with <title> and ends with </title> and can have anything in between.

for (int a=chan.start; a<=item[1].start; a++) {
if (checkLine(file[a], "<title>")) {
for (int b=7; b<=file[a].length(); b++) {
if ( (file[a].substr(b)=="</title>") || (file[a].substr(b,
file[a].length()-1), "</title>") ) {
chan.title=file[a].substr(7, b-1);

what does substr() do? What is the value of b at this point?
What do you expect to happen?

break;
}
else if (b==file[a].length()) {
cout << endl << "What is the Channel Title?" << endl;
getline(cin, chan.title);
file[a] = "<title>" + chan.title + "</title>";
break;
}
}
break;}

else if (a==item[1].start) {
cout << "What is the Channel Title?" << endl;
getline(cin, chan.title);
insertLine(a, "<title>" + chan.title + "</title>");
break;

}

The problem is that it either deletes or adds to what chan.title
should be. Like if chan.title should be "eltit" it might say that its
"eltit</ti" or something like that.

"it might..." and "something like..." are not expressions that should
normally be used when describing a program's behaviour

And it deletes/adds randomly
depending on the length of the title... so what is wrong with it?

its crap

1. you use magic numbers like "7"
2. you almost certainly use global data
3. you propably have a "using namespace std"
3. you may not know what substr() does
4. the layout is horrid


you need to learn some basic debugging skills
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top