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

  • Context: Comp Sci 
  • Thread starter Thread starter nukeman
  • Start date Start date
  • Tags Tags
    C++ Program Writing
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program that counts non-whitespace characters and digits from user input until a specified character, '&', is encountered. Participants are seeking help with coding techniques and logic to achieve this task, which is framed as a homework assignment.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant provides a basic structure for the program, outlining the need to count non-whitespace characters and digits, and mentions using the cctype library for character checks.
  • Another participant suggests that the initial count of spaces should start at zero instead of one and proposes using an overload of the getline function to change the end-of-line character.
  • Some participants express confusion about how to modify the program to count characters instead of words and inquire about swapping the end character from '\n' to '&'.
  • There are suggestions to use isalpha() and isdigit() functions from the cctype library to check for alphabetical characters and digits, respectively.
  • One participant expresses uncertainty about using arrays and requests sample code to count characters and digits, indicating they are new to C++.
  • Another participant clarifies that strings in C++ have array-like functionality and encourages using string methods instead of arrays for character counting.

Areas of Agreement / Disagreement

Participants generally agree on the need to count non-whitespace characters and digits, but there is no consensus on the best approach to implement this, as various methods and coding practices are suggested and debated.

Contextual Notes

Some participants mention limitations in their current knowledge of C++, particularly regarding arrays and library functions, which may affect their ability to implement the proposed solutions effectively.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in character manipulation and input handling in console applications.

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 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K