- #1
nukeman
- 655
- 0
Need help writing C++ program that counts non-whitespace characters...?
Here are instructions...
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;
}
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: