arrays

Hey y'all,
I need some help I am supposed to count the number of words and letter in the array and display the average amount of letters, but my program is counting the white spaces as a letter so my output is wrong. Does anyone know how to help me?

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
void words(char[]);
int main ()
{
char numOfWords [2000]; // Array declaration.
cout << "Please enter a string of words you would like to count" << endl << endl; // Prompting input.
cin.getline(numOfWords, 2000); // getting input.
words(numOfWords); // Passing input to function/ calling.
int length = strlen(numOfWords);
cout << length << endl;
double avg;
avg = length/2;
cout <<" This is the average number of words in the array " << avg << endl;

return 0;
}
void words(char numOfWords[])
{
int numWords = 0; // starting count at 0.
for (int i =0; numOfWords[i] != '\0';i++) // for loop to count number of white spaces.
{
if(numOfWords[i] == ' ' )
{
numWords++;
}
}
cout << "The number of words you entered was " << numWords +1 << endl << endl; // displaying the results.


}
Please use the code tags (<>)...

Use a loop to iterate through the characters, instead of strlen.
Pseudocode:
1
2
3
4
while the character in numOfWords isn't a null terminating
        if character is a space
                skip over it
        increment count 


1
2
3
double avg;
avg = length/2;
cout <<" This is the average number of words in the array " << avg << endl;

What are you doing here?
Last edited on
I was displaying the average but I made a mistake in my calculations I should not be dividing by two.
There exist count() function inside <algorithm> library that can help you solve your problem. I'm not saying this way is better than looping though.

You can find the referrence here : http://www.cplusplus.com/reference/algorithm/count/

If I understood your intention correctly, this might help you learn a thing or two.

Good luck,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

int  countSpaces(char x[]);

int main(){
	char 	theInput[2000];
	int 	numOfWords;
	int 	numOfLetters;
	int 	numOfSpaces;
	double 	Average;

	cout << "Please enter a string of words you would like to count" << "\n\n";
	cin.getline(theInput, 2000);

	numOfSpaces = countSpaces(theInput);
	cout << "Spaces\t\t: " << numOfSpaces << '\n';
	
	numOfWords = numOfSpaces + 1;
	cout << "Words\t\t: " << numOfWords << '\n';

	numOfLetters = strlen(theInput) - numOfSpaces;
	cout << "Letters\t\t: " << numOfLetters << '\n';

	Average = (double) numOfLetters / numOfWords;
	cout << "Letters Average\t: " << Average << '\n';
}

int countSpaces(char x[]){
	return count(x, x + strlen(x), ' ');
}
Thank You
I am still having trouble with the average. If I input " bill bill" I get 2 words 8 letters and an average of 8 letters which is wrong.

/*
Author: Truly Williams.
Programming Challenge: Chapter #12 Question #2
Description: The following program display's two random number for students to add then finds the average.
*/

#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;
void words(char[]);
int main ()
{
char numOfWords [2000]; // Array declaration.
cout << "Please enter a string of words you would like to count" << endl << endl; // Prompting input.
cin.getline(numOfWords, 2000); // getting input.
words(numOfWords); // Passing input to function/ calling.

return 0;
}
void words(char numOfWords[])
{
double numWords = 0; // starting count at 0.
for (int i =0; numOfWords[i] != '\0';i++) // for loop to count number of white spaces.
{
if(numOfWords[i] == ' ' )
{
numWords++;
}
}
cout << "The number of words you entered was " << numWords +1 << endl << endl; // displaying the results.
double length = strlen(numOfWords); // getting the string length.
cout << " The number of letters you enter was " ;
// gtting rid of white spaces.
double letters = (length-numWords);
cout << letters << endl << endl;
double avg = (letters/numWords);

//avg = ((letters)/(numWords)); // calculating average
cout <<" This is the average number of words in the array was " << avg << endl; // Displaying avg.

}
You've been asked once already: please use code tags when posting code, to make it readable.

http://www.cplusplus.com/articles/z13hAqkS/
Yes please use code tags when posting codes.

If your input isn't guaranteed to be a complete sentences or might have things like double-space, etc.
You might find strtok() useful, in your case you would want to check if every token is not empty, then it's a word.

strtok() reference = http://www.cplusplus.com/reference/cstring/strtok/
strcmp() reference = http://www.cplusplus.com/reference/cstring/strcmp/
Topic archived. No new replies allowed.