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: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 :-D Thanks a lot!!!
-Jonathan
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: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 :-D Thanks a lot!!!
-Jonathan