Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading/writing a string problem

Reply
Thread Tools

Reading/writing a string problem

 
 
TyPR124@gmail.com
Guest
Posts: n/a
 
      12-05-2007
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.
 
Reply With Quote
 
 
 
 
TyPR124@gmail.com
Guest
Posts: n/a
 
      12-05-2007
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;
}
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-05-2007
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


 
Reply With Quote
 
TyPR124@gmail.com
Guest
Posts: n/a
 
      12-05-2007
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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
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


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-07-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57