473,668 Members | 2,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple noobish question

I am having a problem with a small bit of code. I can't figure out why
it won't work. Here:
//code:
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <string>

using namespace std;

int main()
{
ifstream file_in;
ofstream file_out;
string word_in;
int old_total = 0;
int new_total = 0;
int x = 0;

file_in.open("w ords.list");
file_out.open(" words.out");

while ( !file_in.eof() )
{
getline(file_in , word_in);
old_total++;
int good = 0;
for ( int i = 0; i < word_in.size(); i++ )
{
if ( isalpha( word_in[i] ) != 0)
{
continue;
}
else
{
x = 1;
break;
}
}
if ( x == 0 )
{
file_out << word_in << endl;
new_total++;
}
else
{
continue;
}
}
cout << old_total << " lines in the original file." << endl;
cout << new_total << " lines in new file." << endl;
cout << old_total - new_total << " lines of difference." << endl;

return 0;
}
//code
All this is supposed to do is run through a list of words from one
file and put the one that only have the standard 26 letters of the
English alphabet into another file. It works fine until it hits the
first word with a non-english character, at which time it finishes and
exits. The output is this:

72411 lines in the original file.
589 lines in new file.
71822 lines of difference.

Obviously the new file should have a whole lot more lines than that.
At line 590 is the word Asunción. It is such a minor thing but it is
driving me up a wall none the less. Any comments would be much
appreciated.

Thanks

May 22 '07 #1
4 1319
th************* @gmail.com wrote:
I am having a problem with a small bit of code. I can't figure out why
it won't work. Here:
//code:
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <string>

using namespace std;

int main()
{
ifstream file_in;
ofstream file_out;
string word_in;
int old_total = 0;
int new_total = 0;
int x = 0;
^^^^^^^^^^^^

Drop this declaration of 'x'.
>
file_in.open("w ords.list");
file_out.open(" words.out");

while ( !file_in.eof() )
{
getline(file_in , word_in);
old_total++;
int good = 0;
I believe here should be the 'x', not 'good'.
for ( int i = 0; i < word_in.size(); i++ )
{
if ( isalpha( word_in[i] ) != 0)
{
continue;
}
else
{
x = 1;
break;
}
}
if ( x == 0 )
Since 'x' is never reset to 0, you're always falling into the
'continue' part.
{
file_out << word_in << endl;
new_total++;
}
else
{
continue;
}
}
cout << old_total << " lines in the original file." << endl;
cout << new_total << " lines in new file." << endl;
cout << old_total - new_total << " lines of difference." << endl;

return 0;
}
//code
All this is supposed to do is run through a list of words from one
file and put the one that only have the standard 26 letters of the
English alphabet into another file. It works fine until it hits the
first word with a non-english character, at which time it finishes and
exits. The output is this:

72411 lines in the original file.
589 lines in new file.
71822 lines of difference.

Obviously the new file should have a whole lot more lines than that.
At line 590 is the word Asunción. It is such a minor thing but it is
driving me up a wall none the less. Any comments would be much
appreciated.
Use tracing to see how your code executes. Or a debugger.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 22 '07 #2
* th************* @gmail.com:
I am having a problem with a small bit of code. I can't figure out why
it won't work. Here:
//code:
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <string>

using namespace std;

int main()
{
ifstream file_in;
ofstream file_out;
string word_in;
This is really used to get a line: misleading name.

int old_total = 0;
int new_total = 0;
int x = 0;
The variable x is used as a boolean.

Declare it as 'bool'.

Use the values 'true' and 'false' instead of '1' and '0'.
file_in.open("w ords.list");
file_out.open(" words.out");
Here you should check for failure opening the files.

while ( !file_in.eof() )
For the first iteration there has not yet been any attempt to read from
the file, and so the eof flag cannot have been set yet, and so it's
meaningless to check it here.

{
getline(file_in , word_in);
Here you should check whether getline fails, and not proceed if getline
fails (which can be due to end of file).

old_total++;
int good = 0;
Here 'x' should be set to '0' (or rather, 'false').

for ( int i = 0; i < word_in.size(); i++ )
{
if ( isalpha( word_in[i] ) != 0)
{
continue;
}
else
{
x = 1;
break;
}
}
The loop above would better be expressed as a separate function.

if ( x == 0 )
{
file_out << word_in << endl;
new_total++;
}
else
{
continue;
}
The else-part is superfluous.

}
cout << old_total << " lines in the original file." << endl;
cout << new_total << " lines in new file." << endl;
cout << old_total - new_total << " lines of difference." << endl;

return 0;
}
//code
All this is supposed to do is run through a list of words from one
file and put the one that only have the standard 26 letters of the
English alphabet into another file. It works fine until it hits the
first word with a non-english character, at which time it finishes and
exits. The output is this:

72411 lines in the original file.
589 lines in new file.
71822 lines of difference.

Obviously the new file should have a whole lot more lines than that.
At line 590 is the word Asunción. It is such a minor thing but it is
driving me up a wall none the less. Any comments would be much
appreciated.
See above.

By the way, please convert tab characters to spaces before posting code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '07 #3
<th************ *@gmail.comwrot e:

I am having a problem with a small bit of code. I can't figure out why
it won't work. Here:
//code:
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <string>

using namespace std;

int main()
{
ifstream file_in;
ofstream file_out;
string word_in;
int old_total = 0;
int new_total = 0;
int x = 0;

file_in.open("w ords.list");
file_out.open(" words.out");

while ( !file_in.eof() )
{
getline(file_in , word_in);
old_total++;
int good = 0;
for ( int i = 0; i < word_in.size(); i++ )
{
if ( isalpha( word_in[i] ) != 0)
{
continue;
}
else
{
x = 1;
break;

The character you mention is going to cause that break to exit the loop.
ISTM that this may be the first non-ASCII char in the file and this break
may not be what you want.
}
}
if ( x == 0 )
{
file_out << word_in << endl;
new_total++;
}
else
{
continue;
}
}
cout << old_total << " lines in the original file." << endl;
cout << new_total << " lines in new file." << endl;
cout << old_total - new_total << " lines of difference." << endl;

return 0;
}
//code
All this is supposed to do is run through a list of words from one
file and put the one that only have the standard 26 letters of the
English alphabet into another file. It works fine until it hits the
first word with a non-english character, at which time it finishes and
exits. The output is this:

72411 lines in the original file.
589 lines in new file.
71822 lines of difference.

Obviously the new file should have a whole lot more lines than that.
At line 590 is the word Asunción. It is such a minor thing but it is
driving me up a wall none the less. Any comments would be much
appreciated.

Thanks
May 22 '07 #4
On May 22, 6:45 pm, theronnights... @gmail.com wrote:
I am having a problem with a small bit of code. I can't figure out why
it won't work. Here:
There are a couple of problems.
//code:
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <string>
using namespace std;
int main()
{
ifstream file_in;
ofstream file_out;
string word_in;
int old_total = 0;
int new_total = 0;
int x = 0;
file_in.open("w ords.list");
file_out.open(" words.out");
while ( !file_in.eof() )
This line is definitely wrong. The *only* time it is useful to
check eof() is after a read has failed, to know if it failed
because you were at eof, or because of some other failure.

The standard idiom here would be:

while ( std::getline( file_in, word_in ) ) // ...

In production code, it's generally considered a good idea to
check bad() and eof() *after* the loop: bad() means you had a
hard read error, and !eof() means that there was a format error
in the file---it shouldn't happen with getline().
{
getline(file_in , word_in);
old_total++;
int good = 0;
for ( int i = 0; i < word_in.size(); i++ )
{
if ( isalpha( word_in[i] ) != 0)
This is undefined behavior. You can't legally call the single
argument version of isalpha with a char.

If I were writing this code, I'd use std::find_if, and a
predicate using the std::ctype facet. But I do this sort of
thing often enough that I've got the necessary predicate in my
tool box. For a simple loop, I'd write something like:

for ( std::string::co nst_iterator it = word_in.begin() ;
it != word_in.end()
&& isalpha( static_cast< unsigned char >( *it ) );
++ it ) {
}
if ( it == word_in.end() ) {
file_out << word_in << std::endl ;
++ new_total ;
}

Note that in general, the use of continue or break in a loop is
a sign of poor programming practice. Especially in this case:
you are using a standard algorithm, called linear search, and it
has a more or less standard implementation, easily recognized
and verified by anyone familiar with the idioms of the language.
{
continue;
}
else
{
x = 1;
break;
}
}
if ( x == 0 )
{
file_out << word_in << endl;
new_total++;
}
else
{
continue;
}
}
cout << old_total << " lines in the original file." << endl;
cout << new_total << " lines in new file." << endl;
cout << old_total - new_total << " lines of difference." << endl;
return 0;}
//code
All this is supposed to do is run through a list of words from one
file and put the one that only have the standard 26 letters of the
English alphabet into another file. It works fine until it hits the
first word with a non-english character, at which time it finishes and
exits. The output is this:
72411 lines in the original file.
589 lines in new file.
71822 lines of difference.
Obviously the new file should have a whole lot more lines than that.
At line 590 is the word Asunción. It is such a minor thing but it is
driving me up a wall none the less. Any comments would be much
appreciated.
Well, there are two obvious problems. The first is that once
you've set x, you never reset it, so it is non-zero for the rest
of the program. Of course, to begin with, it shouldn't be an
int, but a bool, with true and false. And since it is only used
within the loop, it should be declared (and initialized) within
the loop, and not outside. But in the end, if you're writing
idiomatic C++, there's no need for it what so ever.

The second problem is that you are calling isalpha with a char.
If char is signed on your implementation (it usually is), then
it is almost certain that the encoding for ó results in a
negative value, which in turn results in undefined behavior when
calling the single parameter version of isalpha. If you use
this version, you absolutely *must* cast the char to unsigned
char. Otherwise, it's undefined behavior. (With most
implementations , you'll get random results---some accented
characters may return true.)

Note too that whether isalpha( 'ó' ) returns true or false
depends on the locale. By default, here, you are in "C" locale,
where it is guaranteed to return false, but it's something you
generally want to be careful with; at least where I live, real
programs very rarely leave the default locale "C". This is
another advantage of using the std::ctype facet---you can
specify any locale you want at the call site, thus avoiding any
risk of the global locale not being the one you want.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #5

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

Similar topics

5
1722
by: gsv2com | last post by:
Maybe I'm tired, but I'm having a small problem with a date function I'm writing. Total noobish I know, but this is just going beyond me for some reason... What I want to happen is send a date (such as June 31, an invalid date) and have the function turn it into a real date by subtraction. In other words, the function would turn the date to June 30, run itself again, and if June 30 is a real date, it'd return it back to the program. ...
3
3685
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
7
2281
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request that page like http://machinename/dir1/hellp.aspx instead of running that page it starts downloding ...whats missing here ....why the aspx engine not running the page....
4
118808
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a character into a number?????? In Oracle, it is:
14
2976
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
3514
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
2
1056
by: shrynn | last post by:
Hey guys, This is a noobish question I guess but here goes: I am attempting to recalc some of our Payroll data from Detail. I am summing the total dollar figure from each deduction code for the Month of November so I can update other tables. My script looks like this: SELECT prdh_ded, sum(prdh_empe_amt) AS TotalDed FROM prdedhis WHERE prdh_emp = 87016 AND prdh_chk_date BETWEEN '11/01/2007' AND '11/30/2007' GROUP BY prdh_ded,...
10
2125
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
17
5807
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8374
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.