Afficher un message
Vieux 07/12/2007, 14h37   #6
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading/writing a string problem

On 5 Dec, 18:52, TyPR...@gmail.com wrote:

> 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

> Thanks.


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


--
Nick Keighley
  Réponse avec citation
 
Page generated in 0,05298 seconds with 9 queries