•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,679 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,227 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 643 | Replies: 3
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Hi, im currently trying to write a program which counts the characters in an input file. I need it to count the total characters, the non-blank characters and the alphabetic characters. And then show the statistics of each.
I have both the total characters and alphabetic characters working correctly but for some reason I cannot get it to count the non-blank characters using the isspace command. I used breakpoints to determine that the code is not reading the blank spaces from the input file.
Could anyone please tell me where i'm going wrong?
Here is my code:
Thanks,
Thunder
I have both the total characters and alphabetic characters working correctly but for some reason I cannot get it to count the non-blank characters using the isspace command. I used breakpoints to determine that the code is not reading the blank spaces from the input file.
Could anyone please tell me where i'm going wrong?
Here is my code:
// LabReport9Q1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
void countchars(ifstream& input, ofstream& output);
int _tmain(int argc, _TCHAR* argv[])
{
ifstream input_stream;
input_stream.open("input.dat");
if (input_stream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
ofstream output_stream;
output_stream.open("output.dat");
if (output_stream.fail())
{
cout << "Output file opening failed. \n";
exit(1);
}
countchars(input_stream, output_stream);
return 0;
}
void countchars(ifstream& input, ofstream& output)
{
char next;
double blanks = 0, totalnonblanks, alpha = 0, numerics = 0, total;
while (input >> next)
{
if (isalpha(next))
{
alpha++;
}
if (isdigit(next))
{
numerics++;
}
if (isspace(next))
{
blanks++;
}
}
total = alpha + numerics + blanks;
cout << "The total number of character occurences is: " << total << endl;
totalnonblanks = alpha + numerics;
cout << "The total number of non-blank characters is: " << totalnonblanks << endl;
cout << "The total number of alphabetic characters is: " << alpha << endl;
}Thanks,
Thunder
The
One alternative could be to capture each line of input from the file individually as a string, using the getline function.
>> operator skips over whitespace automatically.One alternative could be to capture each line of input from the file individually as a string, using the getline function.
CPP Syntax (Toggle Plain Text)
std::string my_string; while( std::getline( input, my_string ) ) { //TODO: Counting }
Last edited by Bench : Dec 7th, 2007 at 7:55 pm.
¿umop apisdn upside down? •
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- <b>TextFileStats.java need help!</b> (Java)
- Another file I/O question (C++)
- judtifiying text (C)
- word count in borland c++ ?? (C++)
- Word Counting (C++)
- Homework Help/Feedback (C++)
Other Threads in the C++ Forum
- Previous Thread: Which C++ to use?
- Next Thread: The linked list display problem



Linear Mode