A few Coding Problems...

J

Jonathan

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
 
G

Gernot Frisch

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
 
J

John Harrison

Jonathan said:
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
 
J

Jonathan

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
 
J

Joe Laughlin

Jonathan said:
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
 
T

Thomas Matthews

Joe said:
Jonathan said:
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
 
J

Jonathan

Thomas said:
Joe said:
Jonathan said:
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!
 
J

John Harrison

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top