PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Reading/writing a string problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Reading/writing a string problem

Réponse
 
LinkBack Outils de la discussion
Vieux 05/12/2007, 18h52   #1
TyPR124@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Reading/writing a string problem

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.
  Réponse avec citation
Vieux 05/12/2007, 19h04   #2
TyPR124@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading/writing a string problem

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;
}
}
  Réponse avec citation
Vieux 05/12/2007, 19h07   #3
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading/writing a string problem

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


  Réponse avec citation
Vieux 05/12/2007, 19h12   #4
TyPR124@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading/writing a string problem

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.
  Réponse avec citation
Vieux 07/12/2007, 02h40   #5
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Reading/writing a string problem

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


  Réponse avec citation
Vieux 07/12/2007, 13h37   #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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20488 seconds with 14 queries