A few Coding Problems...

Discussion in 'C++' started by Jonathan, Jun 14, 2004.

  1. Jonathan

    Jonathan Guest

    Hey everyone! I am pretty new to C++, and I am just playing around
    with some things that I have learned so far (trying to put them to use
    before I continue on with the book). In one section of my program, I
    am trying to let the user clear parts, or a whole txt file that
    contains multiple lines. The way I wanted to do this was to read each
    line into a vector element using the getline() function. I would then
    (depending on what the user selected), either clear the vector and
    write that to the file, or delete the vector element that the user
    selects, then write that new vector to the file. Here is part of how I
    did this (try not to laugh to hard, I am pretty new to this ;-) :

    while(! ClearNotes.eof()) // loop through lines in file to get load
    vector
    {
    getline (ClearNotes, vec);
    vec.push_back("a");
    i++;
    }
    ClearNotes << clear; //empty file in case vaector is smaller than
    original
    ClearNotes.close();
    cout << "What would you like to clear?" << endl;
    cout << "a) All" << endl;
    cout << "b) Specify" << endl;
    cin >> select;
    switch (select)
    {
    case 'a' : vec.clear(); break;
    case 'b' :
    cout << "Please enter note number to clear: ";
    cin >> clear_note;
    clear_note = clear_note - 1;
    ofstream rewrite("post.txt", ios::eek:ut);
    while (! rewrite) //check file open
    {
    cout << "Error opening output file!";
    return -1;
    }
    while (j < vec.size()) //start reading values from vector
    {
    rewrite << vec[j] << endl;
    j++;
    }
    rewrite.close();
    break;
    default : cout << "Not valid selection!"; break;
    }

    The problem is that I get some errors when I compile that. Here is the
    log that it gave me adter trying to compile:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\C++\notes.cpp" -o "C:\C++\notes.exe" -g3
    -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
    -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
    -L"C:\Dev-Cpp\lib"
    C:/C++/notes.cpp: In function `int clear()':
    C:/C++/notes.cpp:141: no matching function for call to `
    std::vector<std::string, std::allocator<std::string> >::erase(int)'

    C:/Dev-Cpp/include/c++/bits/stl_vector.h:645: candidates are:

    __gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
    std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
    std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
    std::allocator<std::string>]
    C:/Dev-Cpp/include/c++/bits/stl_vector.h:668:
    __gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
    std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
    std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<_Tp*,
    std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
    std::allocator<std::string>]
    C:/C++/notes.cpp:155: jump to case label
    C:/C++/notes.cpp:142: crosses initialization of `std::eek:fstream
    rewrite'



    Internal compiler error: Error reporting routines re-entered.
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See <URL:http://www.gnu.org/software/gcc/bugs.html> for instructions.

    Execution terminated


    Any help in getting me straightened out would be greatly appreciated.
    Just trying to learn my way around this :-D Thanks a lot!!!

    -Jonathan
     
    Jonathan, Jun 14, 2004
    #1
    1. Advertising

  2. > C:/C++/notes.cpp: In function `int clear()':
    > C:/C++/notes.cpp:141: no matching function for call to `
    > std::vector<std::string, std::allocator<std::string>
    >::erase(int)'


    Post the function "clear" and mark the line 141 for us.
    -Gernot
     
    Gernot Frisch, Jun 14, 2004
    #2
    1. Advertising

  3. "Jonathan" <> wrote in message
    news:...
    > Hey everyone! I am pretty new to C++, and I am just playing around
    > with some things that I have learned so far (trying to put them to use
    > before I continue on with the book). In one section of my program, I
    > am trying to let the user clear parts, or a whole txt file that
    > contains multiple lines. The way I wanted to do this was to read each
    > line into a vector element using the getline() function. I would then
    > (depending on what the user selected), either clear the vector and
    > write that to the file, or delete the vector element that the user
    > selects, then write that new vector to the file. Here is part of how I
    > did this (try not to laugh to hard, I am pretty new to this ;-) :
    >
    > while(! ClearNotes.eof()) // loop through lines in file to get load
    > vector
    > {
    > getline (ClearNotes, vec);
    > vec.push_back("a");
    > i++;
    > }


    The end of file loop is wrong, classic newbie mistake. eof doesn't do what
    you think it does (you think it's true when you are at the end of file don't
    you). eof should be called after a read has failed, it then tells you if the
    read failed because of end of file. It does not reliably tell you that you
    are at the end of file before a read.

    Your attempt to add to the vector is wrong as well. You are assuming the
    vec exists before you push back to the vector.

    Write your loop like this

    string line;
    while (getline (ClearNotes, line))
    {
    vec.push_back(line);
    }

    > ClearNotes << clear; //empty file in case vaector is smaller than
    > original


    That does not clear the file. And in any case you don't need to clear the
    file. It will be cleared automatically when you open it for writing. Delete
    the above line.

    john
     
    John Harrison, Jun 14, 2004
    #3
  4. Jonathan

    Jonathan Guest

    >>while(! ClearNotes.eof()) // loop through lines in file to get load
    >>vector
    >> {
    >> getline (ClearNotes, vec);
    >> vec.push_back("a");
    >> i++;
    >> }

    >
    >
    > The end of file loop is wrong, classic newbie mistake. eof doesn't do what
    > you think it does (you think it's true when you are at the end of file don't
    > you). eof should be called after a read has failed, it then tells you if the
    > read failed because of end of file. It does not reliably tell you that you
    > are at the end of file before a read.
    >
    > Your attempt to add to the vector is wrong as well. You are assuming the
    > vec exists before you push back to the vector.
    >
    > Write your loop like this
    >
    > string line;
    > while (getline (ClearNotes, line))
    > {
    > vec.push_back(line);
    > }


    >>ClearNotes << clear; //empty file in case vaector is smaller than
    >>original

    >
    >
    > That does not clear the file. And in any case you don't need to clear the
    > file. It will be cleared automatically when you open it for writing. Delete
    > the above line.
    >
    > john
    >
    >
    >
    > Write your loop like this
    >
    > string line;
    > while (getline (ClearNotes, line))
    > {
    > vec.push_back(line);
    > }


    Thanks for the replys! I did the above, and it got rid of a few of the
    errors. I dont really understand how it is working though. Wouldnt that
    just keep looping, so how does it know when to stop (at the end of the
    file)?

    I am also still getting these errors:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\C++\notes.cpp" -o "C:\C++\notes.exe" -g3
    -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
    -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
    -L"C:\Dev-Cpp\lib"
    C:/C++/notes.cpp: In function `int clear()':
    C:/C++/notes.cpp:156: jump to case label
    C:/C++/notes.cpp:143: crosses initialization of `std::eek:fstream rewrite'



    Internal compiler error: Error reporting routines re-entered.
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See <URL:http://www.gnu.org/software/gcc/bugs.html> for instructions.

    Execution terminated

    Here are the associated lines that it gave:

    cout << "What would you like to clear?" << endl;
    cout << "a) All" << endl;
    cout << "b) Specify" << endl;
    cin >> select;
    switch (select)
    {
    case 'a' : vec.clear(); break;
    case 'b' :
    cout << "Please enter note number to clear: ";
    cin >> clear_note;
    clear_note = clear_note - 1;
    142- ofstream rewrite("post.txt", ios::eek:ut);
    143- while (! rewrite)
    {
    cout << "Error opening output file!";
    return -1;
    }
    while (j < vec.size())
    {
    rewrite << vec[j] << endl;
    j++;
    }
    rewrite.close();
    break;
    155- default : cout << "Not valid selection!"; break;
    156- }

    Thanks again for all of the help!!

    -Jonathan
     
    Jonathan, Jun 14, 2004
    #4
  5. Jonathan

    Joe Laughlin Guest

    Jonathan wrote:
    >>> while(! ClearNotes.eof()) // loop through lines in file
    >>> to get load vector
    >>> {
    >>> getline (ClearNotes, vec);
    >>> vec.push_back("a");
    >>> i++;
    >>> }

    >>
    >>
    >> The end of file loop is wrong, classic newbie mistake.
    >> eof doesn't do what you think it does (you think it's
    >> true when you are at the end of file don't you). eof
    >> should be called after a read has failed, it then tells
    >> you if the read failed because of end of file. It does
    >> not reliably tell you that you are at the end of file
    >> before a read.
    >>
    >> Your attempt to add to the vector is wrong as well. You
    >> are assuming the vec exists before you push back to
    >> the vector.
    >>
    >> Write your loop like this
    >>
    >> string line;
    >> while (getline (ClearNotes, line))
    >> {
    >> vec.push_back(line);
    >> }

    >
    >>> ClearNotes << clear; //empty file in case vaector is
    >>> smaller than original

    >>
    >>
    >> That does not clear the file. And in any case you don't
    >> need to clear the file. It will be cleared automatically
    >> when you open it for writing. Delete the above line.
    >>
    >> john
    >>
    >>
    >>
    > > Write your loop like this
    > >
    > > string line;
    > > while (getline (ClearNotes, line))
    > > {
    > > vec.push_back(line);
    > > }

    >
    > Thanks for the replys! I did the above, and it got rid of
    > a few of the errors. I dont really understand how it is
    > working though. Wouldnt that just keep looping, so how
    > does it know when to stop (at the end of the file)?
    >


    I assume that the function getline() returns false when it is not successful
    at reading a line of data (i.e. the end of a file).

    Joe
     
    Joe Laughlin, Jun 14, 2004
    #5
  6. Joe Laughlin wrote:
    > Jonathan wrote:
    >
    >>>>while(! ClearNotes.eof()) // loop through lines in file
    >>>>to get load vector
    >>>>{
    >>>>getline (ClearNotes, vec);
    >>>>vec.push_back("a");
    >>>>i++;
    >>>>}
    >>>
    >>>
    >>>The end of file loop is wrong, classic newbie mistake.
    >>>eof doesn't do what you think it does (you think it's
    >>>true when you are at the end of file don't you). eof
    >>>should be called after a read has failed, it then tells
    >>>you if the read failed because of end of file. It does
    >>>not reliably tell you that you are at the end of file
    >>>before a read.
    >>>
    >>>Your attempt to add to the vector is wrong as well. You
    >>>are assuming the vec exists before you push back to
    >>>the vector.
    >>>
    >>>Write your loop like this
    >>>
    >>> string line;
    >>> while (getline (ClearNotes, line))
    >>> {
    >>> vec.push_back(line);
    >>> }

    >>
    >>>>ClearNotes << clear; //empty file in case vaector is
    >>>>smaller than original
    >>>
    >>>
    >>>That does not clear the file. And in any case you don't
    >>>need to clear the file. It will be cleared automatically
    >>>when you open it for writing. Delete the above line.
    >>>
    >>>john
    >>>
    >>>
    >>>

    >>
    >> > Write your loop like this
    >> >
    >> > string line;
    >> > while (getline (ClearNotes, line))
    >> > {
    >> > vec.push_back(line);
    >> > }

    >>
    >>Thanks for the replys! I did the above, and it got rid of
    >>a few of the errors. I dont really understand how it is
    >>working though. Wouldnt that just keep looping, so how
    >>does it know when to stop (at the end of the file)?
    >>

    >
    >
    > I assume that the function getline() returns false when it is not successful
    > at reading a line of data (i.e. the end of a file).
    >
    > Joe
    >
    >


    No, actually it returns a reference to the istream. The istream
    can then be converted to a form that is tested for errors,
    e.g.:
    istream my_stream;
    if (!my_stream)
    {
    /* process any i/o errors or EOF */
    }


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Jun 14, 2004
    #6
  7. Jonathan

    Jonathan Guest

    Thomas Matthews wrote:
    > Joe Laughlin wrote:
    >
    >> Jonathan wrote:
    >>
    >>>>> while(! ClearNotes.eof()) // loop through lines in file
    >>>>> to get load vector
    >>>>> {
    >>>>> getline (ClearNotes, vec);
    >>>>> vec.push_back("a");
    >>>>> i++;
    >>>>> }
    >>>>
    >>>>
    >>>>
    >>>> The end of file loop is wrong, classic newbie mistake.
    >>>> eof doesn't do what you think it does (you think it's
    >>>> true when you are at the end of file don't you). eof
    >>>> should be called after a read has failed, it then tells
    >>>> you if the read failed because of end of file. It does
    >>>> not reliably tell you that you are at the end of file
    >>>> before a read.
    >>>>
    >>>> Your attempt to add to the vector is wrong as well. You
    >>>> are assuming the vec exists before you push back to
    >>>> the vector.
    >>>>
    >>>> Write your loop like this
    >>>>
    >>>> string line;
    >>>> while (getline (ClearNotes, line))
    >>>> {
    >>>> vec.push_back(line);
    >>>> }
    >>>
    >>>
    >>>>> ClearNotes << clear; //empty file in case vaector is
    >>>>> smaller than original
    >>>>
    >>>>
    >>>>
    >>>> That does not clear the file. And in any case you don't
    >>>> need to clear the file. It will be cleared automatically
    >>>> when you open it for writing. Delete the above line.
    >>>>
    >>>> john
    >>>>
    >>>>
    >>>>
    >>>
    >>> > Write your loop like this
    >>> >
    >>> > string line;
    >>> > while (getline (ClearNotes, line))
    >>> > {
    >>> > vec.push_back(line);
    >>> > }
    >>>
    >>> Thanks for the replys! I did the above, and it got rid of
    >>> a few of the errors. I dont really understand how it is
    >>> working though. Wouldnt that just keep looping, so how
    >>> does it know when to stop (at the end of the file)?
    >>>

    >>
    >>
    >> I assume that the function getline() returns false when it is not
    >> successful
    >> at reading a line of data (i.e. the end of a file).
    >>
    >> Joe
    >>
    >>

    >
    > No, actually it returns a reference to the istream. The istream
    > can then be converted to a form that is tested for errors,
    > e.g.:
    > istream my_stream;
    > if (!my_stream)
    > {
    > /* process any i/o errors or EOF */
    > }
    >
    >

    Ok, I figured out what the other erros where. When had my switch
    statement, I just listed the code right down. When I added brackets
    around each case with more than one line, it ran fine. Dont know why
    that fixed it, btu it did :) Thanks!
     
    Jonathan, Jun 14, 2004
    #7
  8. > >
    > >

    > Ok, I figured out what the other erros where. When had my switch
    > statement, I just listed the code right down. When I added brackets
    > around each case with more than one line, it ran fine. Dont know why
    > that fixed it, btu it did :) Thanks!


    Probably because you had variable declarations inside the switch statement.
    You aren't allow to jump over a variable declaration in C++ (unless it
    inside curly brackets).

    switch (x)
    {
    case 1:
    int y;
    break;
    case 2: // a jump to here skips the declaration of y
    break;
    }

    switch (x)
    {
    case 1:
    {
    int y;
    }
    break;
    case 2: // OK, y is inside curly brackets
    break;
    }

    john
     
    John Harrison, Jun 15, 2004
    #8
    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. Vhdl Learner

    CODING PROBLEMS

    Vhdl Learner, Dec 6, 2003, in forum: VHDL
    Replies:
    2
    Views:
    602
    Kumaran Selvaratnam
    Dec 10, 2003
  2. Replies:
    7
    Views:
    649
    Stig Brautaset
    Jul 23, 2003
  3. calmar
    Replies:
    11
    Views:
    896
    calmar
    Feb 21, 2006
  4. Murali
    Replies:
    2
    Views:
    572
    Jerry Coffin
    Mar 9, 2006
  5. Cliff
    Replies:
    10
    Views:
    489
    Jim Langston
    Jun 23, 2007
Loading...

Share This Page