473,412 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

A few Coding Problems...

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::out);
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::ofstream
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
Jul 22 '05 #1
7 2377
> 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
Jul 22 '05 #2

"Jonathan" <fa*****@yahoo.com> wrote in message
news:83*************************@posting.google.co 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


Jul 22 '05 #3
>>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::ofstream 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::out);
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
Jul 22 '05 #4
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
Jul 22 '05 #5
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

Jul 22 '05 #6
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!
Jul 22 '05 #7
> >

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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mikkel christensen | last post by:
Hi there I wonder if any of you could point me in a direction where I can find some usefull information about coding standarts. I have some generel experiense in programming but I lack many...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
2
by: manning_news | last post by:
Has anyone had a problem with Access 2003 not saving their coding? I've been using 2003 for a couple of months now and just this week noticed that some coding I'd done for a database was not there...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
3
by: | last post by:
I was wondering, I don't mean to be rude at all to ask this but i'd like to know if any of you guys have had problems in relating to getting paid a fair amount when it comes to coding. ...
23
by: Simon Hengel | last post by:
Hello, we are hosting a python coding contest an we even managed to provide a price for the winner... http://pycontest.net/ The contest is coincidentally held during the 22c3 and we will be...
78
by: Robert Baer | last post by:
The homepage i have had up and seemingly working is: http://oil4lessllc.com/ However, the validator has so many complaints, and being so incompetent, i have no clue as to how to fix it all. Would...
3
by: juliehg | last post by:
Hi, I'm currently using MS Access 2000 to keep all my actors measurements, resumes and personal details on a database. To upload their details onto our website, I have built an event so when I...
0
by: pat | last post by:
CodeCheck Coding Standard's Support As a free service to our customers we offer support in developing "rule-files" for automating corporate coding standards. If you have a coding standard that...
23
by: LayneMitch via WebmasterKB.com | last post by:
Hello. It's me again with another question that should generate a lot of responses. As mentioned before, I'm working on my first site (hopefully I'll be done soon and can move on to other...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.