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

Click For Summary
The discussion focuses on writing a C++ program to count non-whitespace characters and digits from user input until a special character, '&', is encountered. The initial code attempts to count spaces instead of non-whitespace characters, and participants suggest using the cctype library functions, such as isalpha and isdigit, to correctly identify characters and digits. There are also recommendations to modify the getline function to use '&' as a delimiter and to start the non-space count at zero. Overall, the conversation emphasizes the importance of correctly implementing character counting logic and utilizing appropriate library functions for the task.
nukeman
Messages
651
Reaction score
0
Need help writing C++ program that counts non-whitespace characters...?

Homework Statement



Here are instructions...

Code:
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.
$

Homework Equations


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:
Physics news on Phys.org


nukeman said:
nextChar = msg.at(i); // gets a character

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.
 


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?
 


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.
 


"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...
 


nukeman said:

Homework Statement



Here are instructions...

Code:
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.
$


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;
}

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.
 


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

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


nukeman said:
"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...

Code:
[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;

}
nukeman said:
We have not even go to arrays yet, so I can't put it into that.

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

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:


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

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

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K