User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2007
Posts: 2
Reputation: Thunder2k4 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Thunder2k4 Thunder2k4 is offline Offline
Newbie Poster

Isspace counting

  #1  
Dec 7th, 2007
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:

// 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Isspace counting

  #2  
Dec 7th, 2007
The >> 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.
  1. std::string my_string;
  2. while( std::getline( input, my_string ) )
  3. {
  4. //TODO: Counting
  5. }
Last edited by Bench : Dec 7th, 2007 at 7:55 pm.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Isspace counting

  #3  
Dec 7th, 2007
Or you could just turn off the skipws flag:
while (input >> noskipws >> next)
Reply With Quote  
Join Date: Dec 2007
Posts: 2
Reputation: Thunder2k4 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Thunder2k4 Thunder2k4 is offline Offline
Newbie Poster

Re: Isspace counting

  #4  
Dec 7th, 2007
Thanks alot mate, the noskipws command worked a treat. :]
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC