Originally posted by simple
Ok i went ahead and made some of the changes to the code...i think its getting closer. thanks again for all the help. Hopefully i set up the code tags right this time!
Yep, code tags are done correctly.
I will add comments to your code as I correct it.
Code:
#include <stdio.h>
#include <ctype.h>
int read_and_count(void);
/*
For your first examples globals are
fine, but generally you want to avoid
global variables in most situations.
It makes your code easier to read
if the variables a function uses are
actually in that function.
*/
int chr;
int word_count = 0;
int letter_count = 0;
int next_word = 0;
int main(void)
{
printf("Please type a sentince\n");
read_and_count();
/*
You don't do anything with the
return value of your function here.
You probably want to do
something with it, like store it in
a variable so you can print it
later. Otherwise, if you don't ever
plan on using the return value,
you may as well use a 'void'
for your function instead of making
it return an 'int'.
Again, you need to add a line so
your main function returns a
value. This should do:
*/
return 0;
}
int read_and_count(void)
{
while (chr != EOF; chr != isspace(chr); chr != ".")
/*
Your while( ) loop is wrong.
If you want to test for all of
these values, you will need to
use the following:
while( char != EOF && !isspace(chr) && chr != '.' )
Here, we use the "&&" expression
to test for more than one
expression. Not the ; as you
had.
Additionally, when testing for
a single character value, you
use single quotes instead of
double quotes: ' not "
*/
{
getchar(chr);
/*
This next line does nothing.
Remove it.
*/
;
printf("%c", chr);
letter_count = letter_count +1;
}
/*
'If' should be 'if'. Do not use
a capital I.
For testing truth values, you
use the == and not =. The =
is used to assign a value to a
variable. == is used to check
for equality.
*/
Take it from there and see what you can do.
Quzah.