Reading/writing a string problem

Discussion in 'C++' started by TyPR124@gmail.com, Dec 5, 2007.

  1. Guest

    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.
     
    , Dec 5, 2007
    #1
    1. Advertising

  2. Guest

    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;
    }
    }
     
    , Dec 5, 2007
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Dec 5, 2007
    #3
  4. Guest

    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.
     
    , Dec 5, 2007
    #4
  5. 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
     
    Victor Bazarov, Dec 7, 2007
    #5
  6. On 5 Dec, 18:52, 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
     
    Nick Keighley, Dec 7, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pieter Hulshoff
    Replies:
    0
    Views:
    1,066
    Pieter Hulshoff
    Aug 13, 2003
  2. Chris Pruett
    Replies:
    0
    Views:
    1,441
    Chris Pruett
    Aug 14, 2003
  3. Keith Dewell
    Replies:
    7
    Views:
    501
    Keith Dewell
    Oct 1, 2003
  4. Per
    Replies:
    4
    Views:
    377
    James Kanze
    Dec 3, 2008
  5. Leslaw Bieniasz
    Replies:
    2
    Views:
    1,084
    Thomas J. Gritzan
    Jan 15, 2010
Loading...

Share This Page