-
c programming help
Hello,
I am writing a c program to read in data from a created file. This program has to calculate how many words, vowels and sentences from the text and use it to calculate a reading score. The score aspect is an easy part to program. I have made many attempts at writing this program and this is where i am at right now and it still doesnt work. Just so you know I am using quincy 99 as my compiler. Thanks in advance for the input.
Here is my code:
#include <stdio.h>
#include <string.h>
int
main (void)
{
int v, vowels, w, words, s, sentences;
double score;
char text[1500];
FILE *input;
input = fopen("beatles.txt", "r");
fgets (text, sizeof(text), input);
/* to count vowels*/
for(v=0; text[v] != '\0'; v++)
{switch(text[v])
{case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
}
}
printf("vowels:%d", &vowels);
/* to count words */
for(w=0; text[w] != '\0'; w++)
{switch(text[w])
{case ' ':
words++;
}
}
printf("words: %d", &words);
/* to count for sentences */
for(s=0; text[s] != '\0'; s++)
{switch(text[s])
{case '.':
case '!':
case '?':
case ':':
sentences++;
}
}
printf("sentences:%d", &sentences);
/* to calculate FRES score */
score = 206.835 - ((1.015) * (words/sentences)) - ((84.6) * (vowels/words));
if (score > 70)
printf ("A elementary education \n");
else
if (score >=50 || score<=70)
printf ("secondary education \n");
else
if (score>=20 || score<=50)
printf ("College \n");
else
if (score < 20)
printf ("expert \n");
return(0);
}
-
Code:
#include <stdio.h>
#include <string.h>
int
main (void)
{
int v, vowels, w, words, s, sentences;
double score;
char text[1500];
FILE *input;
input = fopen("beatles.txt", "r");
fgets (text, sizeof(text), input);
/* to count vowels*/
for(v=0; text[v] != '\0'; v++)
{switch(text[v])
{case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
}
}
printf("vowels:%d", &vowels);
/* to count words */
for(w=0; text[w] != '\0'; w++)
{switch(text[w])
{case ' ':
words++;
}
}
printf("words: %d", &words);
/* to count for sentences */
for(s=0; text[s] != '\0'; s++)
{switch(text[s])
{case '.':
case '!':
case '?':
case ':':
sentences++;
}
}
printf("sentences:%d", &sentences);
/* to calculate FRES score */
score = 206.835 - ((1.015) * (words/sentences)) - ((84.6) * (vowels/words));
if (score > 70)
printf ("A elementary education \n");
else
if (score >=50 || score<=70)
printf ("secondary education \n");
else
if (score>=20 || score<=50)
printf ("College \n");
else
if (score < 20)
printf ("expert \n");
return(0);
}
-
Quote:
Originally Posted by
akieft
Code:
int v, vowels, w, words, s, sentences;
...
vowels++;
You've declared vowels as a variable, and then you increment it. What's wrong with that picture? What will the value of variable vowels be the first time it is incremented? What about the other variables?
Code:
printf("vowels:%d", &vowels);
Do you want to print the value contained in variable vowels or the address of variable vowels?
Fix those two things, then you can fix the logic errors.
Todd
-
I'm wondering how you managed to post all that code with all those braces, without tripping the code checking tool.