# Homework Help: Need help writing C++ program that counts non-whitespace characters ?

1. Oct 1, 2011

### nukeman

Need help writing C++ program that counts non-whitespace characters....?

1. The problem statement, all variables and given/known data

Here are instructions....

Code (Text):
Write a program - stored in the file charcounter.cpp - that will read and count non-whitespace characters from the standard input stream - until some special character is entered. Let's use & for now. (This value is declared as a named constant so that it can easily be changed in only one place in the program - SO ... the & should (must??) not appear anywhere else in the program!)

Your program will also count the number of digits in the input stream, as shown below. Note that the cctype library contains a boolean function isdigit(achar) ...

(Note that your program should handle the unusual case in which the & is the first non-whitespace character.)

Your program must produce behaviour very similar to the following ...

$./a.out Enter some text - on 1 or more lines - followed by &. 1 25 cat 0g 6& There were 9 (non-whitespace) characters before the & 5 of them were digits.$

2. Relevant equations

3. The attempt at a solution

This is what I have so far. Really having trouble with this one

#include <iostream>
#include <string>
using namespace std;

int main()
{
const char END_MARK = '&';
int i, numspaces;
char nextChar;
string msg;

numspaces=1;

cout << "Enter some text - on 1 or more lines - followed by &.\n";
getline(cin, msg);

// checks each character in the string
for (i=0; i<int(msg.length()); i++)
{
nextChar = msg.at(i); // gets a character
if (isspace(msg))
numspaces++;
}
cout << "\nThere were " << numspaces << " (non-whitespace) characters before the &.";
cin.ignore();
return 0;
}

Last edited: Oct 1, 2011
2. Oct 1, 2011

### MisterX

Re: Need help writing C++ program that counts non-whitespace characters....?

This line is unnecessary; the variable nextChar does not appear anywhere else in the code.

The count of spaces should start with zero, not one (it should be obvious why).

It looks like the code you posted would result in a count of the number of spaces in a line of text (until '\n'), and not the number of spaces until '&'. There is an easy way to modify your program to fix this issue. You can use an overload of the function getline to change the character that is used to denote the end of a line. This may be done by providing a third parameter to the getline function, which is a character to be used as the delimiter.

3. Oct 1, 2011

### nukeman

Re: Need help writing C++ program that counts non-whitespace characters....?

How do I change it so it counts just characters, not words??

THanks so much for your help!

And yes, I want it to end with &, insted of \n - Cant I just swap them?

4. Oct 1, 2011

### Ivan92

Re: Need help writing C++ program that counts non-whitespace characters....?

I haven't looked through program, but if you want to count chars you would need to use the library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.

5. Oct 1, 2011

### nukeman

Re: Need help writing C++ program that counts non-whitespace characters....?

"library <cctype>. Hint hint: I would use isalpha(); and test if character is alphabetical or isdigit for numbers.

I am not too sure how to use that...

6. Oct 1, 2011

### Ivan92

Re: Need help writing C++ program that counts non-whitespace characters....?

Ok, So I kind of see what you are trying to do. What I notice is that isspace is not a member of cstring library. It is a member of cctype library. To get the line that the user entered and receive the characters, I would us cin.get() to receive the line by characters. Then I would put the characters into an array. Then I would use a for-loop to see if there are characters or spaces and increment an int by 1.

7. Oct 2, 2011

### nukeman

Re: Need help writing C++ program that counts non-whitespace characters....?

We have not even go to arrays yet, so I cant put it into that.

Can you help me with some sample code that would just count characters and digits...?? Im so lost on this one :(

8. Oct 2, 2011

### MisterX

Re: Need help writing C++ program that counts non-whitespace characters....?

Code (Text):
[B]#include <cctype>[/B]
#include <iostream>

using namespace std;
int main()
{
char c;
cin >> c;
if([B]isalpha(c)[/B])
cout << "You entered an alphabetic character." <<endl;

}

Ignore his comment about using an array. Strings have all the array functionality you might need.

Counting characters and digits is similar to counting spaces, you'd just use different functions to determine if each character is a digit or a space. You may use the functions isalpha and isdigit.

Last edited: Oct 2, 2011
9. Oct 2, 2011

### nukeman

Re: Need help writing C++ program that counts non-whitespace characters....?

MisterX, yes isalph and isdigit is what I must use, but i dont know how to wwrite it (I am VERY new to C++ )

Any help, or tutorials on how I can write this?