A
ashmangat
I'm trying to claculate the average number of characters per words but
having some problems
Every time i run this program, i get 0 as the average
#include <iostream>
#include <string>
#include <iomanip>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
void avgg(char[]);
int wordCount(char []);
int main()
{
char cstring[81];
int index;
cout << "Written by Arshdeep Kaur, Cs102 online\n";
cout << "\nEnter a string, 80 or fewer characters:\n";
cin.getline(cstring, 81);
cout << "\nThe number of words in that string: ";
index = wordCount(cstring);
cout << index << endl;
avgg(cstring);
return 0;
}
int wordCount(char cstring[])
{
int index = 0;
int word = 0;
while (cstring[index] != '\0')
{
if (isspace(cstring[index]))
{
while (isspace(cstring[index]))
{
index++;
}
}
if (isalnum(cstring[index]))
{
word++;
while(islower(cstring[index]))
{
index++;
}
while((isalnum(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}
index++;
}
return word;
}
void avgg( char cstring[])
{
int index = 0, avg = 0;
int word = 0;
if(cstring[word] != '\0')
{
while (cstring[word] != '\0')
{
if (isspace(cstring[word]))
{
while (isspace(cstring[word]))
{
word++;
}
}
if (isalnum(cstring[word]))
{
index++;
while(islower(cstring[word]))
{
word++;
}
while((isalnum(cstring[word])) || (
ispunct(cstring[word])))
{
word++;
}
}
word++;
}
float Total = 0.0;
Total += index; // Keep a running total
avg = Total / word;
cout << "Average: " << avg << endl;
}
}
****Can anyone find the error
having some problems
Every time i run this program, i get 0 as the average
#include <iostream>
#include <string>
#include <iomanip>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
void avgg(char[]);
int wordCount(char []);
int main()
{
char cstring[81];
int index;
cout << "Written by Arshdeep Kaur, Cs102 online\n";
cout << "\nEnter a string, 80 or fewer characters:\n";
cin.getline(cstring, 81);
cout << "\nThe number of words in that string: ";
index = wordCount(cstring);
cout << index << endl;
avgg(cstring);
return 0;
}
int wordCount(char cstring[])
{
int index = 0;
int word = 0;
while (cstring[index] != '\0')
{
if (isspace(cstring[index]))
{
while (isspace(cstring[index]))
{
index++;
}
}
if (isalnum(cstring[index]))
{
word++;
while(islower(cstring[index]))
{
index++;
}
while((isalnum(cstring[index])) || (
ispunct(cstring[index])))
{
index++;
}
}
index++;
}
return word;
}
void avgg( char cstring[])
{
int index = 0, avg = 0;
int word = 0;
if(cstring[word] != '\0')
{
while (cstring[word] != '\0')
{
if (isspace(cstring[word]))
{
while (isspace(cstring[word]))
{
word++;
}
}
if (isalnum(cstring[word]))
{
index++;
while(islower(cstring[word]))
{
word++;
}
while((isalnum(cstring[word])) || (
ispunct(cstring[word])))
{
word++;
}
}
word++;
}
float Total = 0.0;
Total += index; // Keep a running total
avg = Total / word;
cout << "Average: " << avg << endl;
}
}
****Can anyone find the error