Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A few Coding Problems...

Reply
Thread Tools

A few Coding Problems...

 
 
Jonathan
Guest
Posts: n/a
 
      06-14-2004
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[i]);
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: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: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 Thanks a lot!!!

-Jonathan
 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      06-14-2004
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-14-2004

"Jonathan" <> wrote in message
news: m...
> 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[i]);
> 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[i] 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




 
Reply With Quote
 
Jonathan
Guest
Posts: n/a
 
      06-14-2004
>>while(! ClearNotes.eof()) // loop through lines in file to get load
>>vector
>> {
>> getline (ClearNotes, vec[i]);
>> 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[i] 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: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: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
 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      06-14-2004
Jonathan wrote:
>>> while(! ClearNotes.eof()) // loop through lines in file
>>> to get load vector
>>> {
>>> getline (ClearNotes, vec[i]);
>>> 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[i] 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


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-14-2004
Joe Laughlin wrote:
> Jonathan wrote:
>
>>>>while(! ClearNotes.eof()) // loop through lines in file
>>>>to get load vector
>>>>{
>>>>getline (ClearNotes, vec[i]);
>>>>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[i] 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

 
Reply With Quote
 
Jonathan
Guest
Posts: n/a
 
      06-14-2004
Thomas Matthews wrote:
> Joe Laughlin wrote:
>
>> Jonathan wrote:
>>
>>>>> while(! ClearNotes.eof()) // loop through lines in file
>>>>> to get load vector
>>>>> {
>>>>> getline (ClearNotes, vec[i]);
>>>>> 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[i] 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!
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-15-2004
> >
> >

> 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


 
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
Internet connects for a few seconds then disconnects for a few sec Cody Wireless Networking 2 06-28-2009 08:24 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
general coding issues - coding style... calmar Python 11 02-21-2006 10:36 AM
a few wireless questions.... =?Utf-8?B?Tmlo?= Wireless Networking 1 12-30-2004 05:25 AM
Why do favicons disappear after a few weeks? hectorcorrector Firefox 3 01-24-2004 05:53 AM



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