|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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; } } |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
TyPR124@gmail.com wrote:
> 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 -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
TyPR124@gmail.com wrote:
> 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 -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|