2 Replies - 11617 Views - Last Post: 13 November 2007 - 01:02 PM Rate Topic: -----

#1 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Funciton to Count Characters in C++

Posted 13 November 2007 - 10:36 AM

I am attempting to do several things within one program and I got everything except counting characters and whitespaces. Can some one tell me if I am headed in the right direction.



// Program reads from one file and writes it to another. 
#include <ctype.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
using ::isdigit; 
using ::tolower;
using ::isalpha;
using namespace std;

void add_plus_plus(ifstream& in_stream, ofstream& out_stream);


int main( )
{
ifstream fin;
ofstream fout;
char ch;
int count;

cout << "Begin editing files.\n";
fin.open("E:\\textin.dat");
if (fin.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open("E:\\textOut.dat");
if (fout.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}

add_plus_plus(fin, fout);


fin.close( );
fout.close( );
cout << "End of editing files.\n";
system ("pause");
}
void add_plus_plus(ifstream& in_stream, ofstream& out_stream)

{
	char next, ch;
  
	in_stream.get(next);
	while (! in_stream.eof( ))
	{
	if  (next == '-')
		 ch = ' ';
	  else if (isupper(next))
			ch = tolower(next);
	  else if (isdigit(next))
			ch = '*';
	  else ch = next;

	 out_stream << ch;
	 in_stream.get(next);  
	}
{
	 chars['a']. 
while (! in_stream.eof( ))
if (isalnum(ch))
   cout << letters++;

	 out_stream << ch;
	 in_stream.get(next);  
}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Funciton to Count Characters in C++

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: Funciton to Count Characters in C++

Posted 13 November 2007 - 12:31 PM

Hi cooplis,

You were on track for a bit there then you went off track with that whole second while loop. Remember, you already reached the end of file with the first loop, so unless you close and reopen the file or adjust the internal pointer of the file, your second while loop test of EOF would always be true. I am also not sure what chars['a']. was suppose to be. That makes no sense.

Here is a correct working version of your add_plus_plus() function which should count and print the count of the letters. You had everything you needed in the first loop, you just needed to increment a counter there.

void add_plus_plus(ifstream& in_stream, ofstream& out_stream)
{
	char next, ch;
	int letters = 0; // Don't forget to create a letters variable.
  
	in_stream.get(next);
	while (!in_stream.eof( ))
	{
		if  (next == '-')
			ch = ' ';
		else if (isupper(next))
			ch = tolower(next);
		else if (isdigit(next))
			ch = '*';
		else ch = next;

		out_stream << ch;

		// Increment your count here while you are reading
		letters++;
		in_stream.get(next);  
	}

	// Print out the number of letters counted.
	cout << "Number of letters: " << letters << endl; 
}



I have commented the places where I have changed so you can see what I did. Hope this meets with your satisfaction and that you learn something from it.

Enjoy!

"At DIC we be letter counting code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Funciton to Count Characters in C++

Posted 13 November 2007 - 01:02 PM

Thank you sooo much I have been trying for days and that was sooo simple. I am taking a class but the teacher never told us this way. I am practicing getting it to read the spaces now.

Again thank you ever so much!!!!!! :D


View PostMartyr2, on 13 Nov, 2007 - 12:31 PM, said:

Hi cooplis,

You were on track for a bit there then you went off track with that whole second while loop. Remember, you already reached the end of file with the first loop, so unless you close and reopen the file or adjust the internal pointer of the file, your second while loop test of EOF would always be true. I am also not sure what chars['a']. was suppose to be. That makes no sense.

Here is a correct working version of your add_plus_plus() function which should count and print the count of the letters. You had everything you needed in the first loop, you just needed to increment a counter there.

void add_plus_plus(ifstream& in_stream, ofstream& out_stream)
{
	char next, ch;
	int letters = 0; // Don't forget to create a letters variable.
  
	in_stream.get(next);
	while (!in_stream.eof( ))
	{
		if  (next == '-')
			ch = ' ';
		else if (isupper(next))
			ch = tolower(next);
		else if (isdigit(next))
			ch = '*';
		else ch = next;

		out_stream << ch;

		// Increment your count here while you are reading
		letters++;
		in_stream.get(next);  
	}

	// Print out the number of letters counted.
	cout << "Number of letters: " << letters << endl; 
}



I have commented the places where I have changed so you can see what I did. Hope this meets with your satisfaction and that you learn something from it.

Enjoy!

"At DIC we be letter counting code ninjas!" :snap:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1