473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem finding the average

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

Nov 22 '05 #1
2 2046
as*******@yahoo.com wrote:
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 <cstdlib> if anything....
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

Nov 22 '05 #2
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.

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Warner | last post by:
hey guys, I am having a bit of an issue with a project I'm doing. Basically what it will ultimately end up able to do is that it will accept set up a dynamic array based on user input and allow...
6
by: Fao | last post by:
Hi, I am in my first year of C++ in college and my professor wants me to Write a Program with multiple functions,to input two sets of user-defined data types: One type named 'Sign' declared by...
6
by: c19h28o2 | last post by:
Hi, I'm learning to use pointers with multidemensional arrays so please bear with me! Here is the problem which is from c primer plus Ch10 Programming excercise 1 Modify the rain program...
4
by: samadams_2006 | last post by:
Hello, There are various "Color Pickers" out there, where a series of colors are displayed on a grid and when the user clicks one of these colors, it is selected, and processed by the...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
3
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
4
by: =?Utf-8?B?YjF1Y2VyZWU=?= | last post by:
Hi, still very new to programming but am a little stumped by how to do this idea i have. I need to make a moving average which takes every nth value in a data series to build a running total...
0
by: shyaminf | last post by:
i learnt that it is possible to know the average load in windows using kernel32.dll. but i am not sure with the function name in kernel32.dll which gives me the average load of the system. please...
2
by: CodecSmash49 | last post by:
im currently working on a assignment, where you have 3 monkeys that are feed for 7 days. You have to use a 2D array to gather the info. display like a chart, and find the average eaten each day. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.