problem finding the average

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
 
I

int2str

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

So? What now? Turn off your brain and ask the big ol Internet to bail
you out?

First off, compile your program with warnings enabled. You should get a
hint on one potential problem. Then do some bench-testing, you should
see a few more problems. And then fire up your debugger of choice and
single step through your program. If you still have questions come back
and ask.

Your program is wrong not only on the average, but also on the word
count by the way.

I won't give you a solution to your problem(s), but I'll give you some
hints...
#include <iostream>
#include <string>

Apparently you're not allowed to use std::string, so why include it?
#include <iomanip>
++unused_header;

#include <ctype.h>
++unused_header;

#include <stdlib.h>

++unused_header;

Also should be said:
using namespace std;
void avgg(char[]);
int wordCount(char []);

Why use two functions for this if one could do?
int main()
{
char cstring[81];

80 chars + 0 I pressume.
int index;

Uninitialized variable. Declare it close to where it's used as well.
cout << "Written by Arshdeep Kaur, Cs102 online\n";

cout << "With help from comp.lang.c++ - subtract credits as
necessary\n";
cout << "\nEnter a string, 80 or fewer characters:\n";
cin.getline(cstring, 81);

81 > 80
No 0 termination possible...
Use a MACRO or const int instead of magic number.
cout << "\nThe number of words in that string: ";
index = wordCount(cstring);
cout << index << endl;

index??? strange name for this.
Why not just:
cout << wordCount( cstring ) << endl;

Also, you mix \n and endl pretty randomly. Understand the difference
and choose accordingly.
avgg(cstring);

Why does this function use different semantics from wordCount()?
Once again seems random to me.
return 0;
}


int wordCount(char cstring[])
{

wordCount( NULL ) == **BOOM**...
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]))

islower()? wtf?
{
index++;
}
while((isalnum(cstring[index])) || (
ispunct(cstring[index])))

So now || is an option?
Once again, random choice ....
{
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

Read the comment your wrote and look at the code you wrote.
Looks like your left hand has trouble understanding what the right hand
is trying to do...
(hint: comment line - 1)
avg = Total / word;

Compiler warning...
cout << "Average: " << avg << endl;
}

}


****Can anyone find the error

Yes:
Error on line 0: Lazy programmer.

Regards,
Andre
 
G

\/Gogineni\/

Declare avg as a float instead of integer and what exactly is you want,
average no of characters in a word.

Then you have to change the code in avgg()
avg = word / Total; (Total is word count, word is character count
without spaces)

Better be naming according the meaning of the variable rather
simplicity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top