Calculating Reading Score with a C Program

  • Thread starter Thread starter akieft
  • Start date Start date
  • Tags Tags
    Program Reading
Click For Summary

Discussion Overview

The discussion revolves around a C program intended to read text from a file and calculate the number of words, vowels, and sentences to derive a reading score. Participants are addressing issues related to the program's functionality, compiler errors, and code structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their program's goal and shares their code, indicating that it does not work as intended.
  • Another participant questions the specifics of the error message and asks for clarification on what the program is supposed to do.
  • There is a suggestion that the compiler installation might be the issue, rather than the code itself.
  • Concerns are raised about the use of fgets, which only reads one line, and whether a different function like fscanf might be more appropriate.
  • Participants suggest using a single loop to count vowels, words, and sentences instead of separate loops for each.
  • There are recommendations to include print statements for debugging purposes to track what is being read from the file.
  • One participant proposes using a while loop to read multiple lines from the file, indicating that fgets returns NULL at the end of the file.
  • Another participant points out that the current word counting logic may not correctly account for words at the end of sentences.
  • There is discussion about restructuring the conditional statements for determining the reading score.

Areas of Agreement / Disagreement

Participants express various opinions on the code's structure and functionality, with no consensus reached on the best approach to resolve the issues presented. Multiple competing views on how to handle the reading and counting logic remain evident.

Contextual Notes

There are unresolved issues regarding the handling of file input, the counting logic for words, and the proper installation of the compiler. Specific assumptions about the input file's format and content are not fully addressed.

akieft
Messages
17
Reaction score
0
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 doesn't 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 != '\0'; s++)

{switch(text)
{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);
}
 
Technology news on Phys.org
doesnt work
Would you care to be a little more specific?
What does it do?
What would you like it to do?
 
I would like it to count the number of vowels, words, and sentences in the text(which is read from a file). Right now it is giving an "qcc.exe installation error" msg, but i had someone else run it and she said it is not working. Your help is greatly appreciated.
 
Then the error is nothign to do with your program - the compiler is not installed correctly
Fix the qcc installation and try it again.

ps if you put {CODE} {/CODE} ( but with square brackets) around your program when you post here it will make it a lot easier to read.
 
ok thanks...but i have no idea how to fix the compiler. Another girl tried to run the program and she simply said it doesn't work. Do you see anything wrong with the code?
 
Can you edit your post and put CODE blocks around it - so I can see the indentation.

One obvious problem is that fgets only reads one line, is this what you intended?
 
akieft said:
ok thanks...but i have no idea how to fix the compiler. Another girl tried to run the program and she simply said it doesn't work. Do you see anything wrong with the code?

"It doesn't work" doesn't tell us anything.

For starters though, you do realize don't you that fgets reads at most one line? So if "beatles.txt" has more than one line, then only the first will get counted.

What I would recommend is (just while debugging) putting printfs into your loop, so that text[v] is being printed on each pass; this will let you see exactly what, if anything, is being read. Maybe beatles.txt does not exist or is not being read correctly on the other system.

If you're just looking for criticism, then: there is no reason to create separate for loops for vowels, words, and sentences, you could just make one loop that counts all three at once; also when you say "printf" you have to put a \n at the end of everything you printf or else everything will just get printed out on one line.
 
akieft, what mgb means is that you should post your code like this:

Code:
Put code here
 
the .txt has more than one line. Would a fscanf function work better? This is not completely finished with formatting and everything worked out.
 
  • #10
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);
}
 
  • #11
Coin said:
akieft, what mgb means is that you should post your code like this:

Code:
Put code here

How do you get the CODE blocks to show as text, I tried all the escaping chars I could think of !
 
  • #12
any ideas how this will work. If fgets only gets one line, how would you read multiple lines?
 
  • #13
akief
putting the fgets in a while loop will at least read the entire file.
fgets returns false (NULL actually) at the end of a file or on error.
Code:
while ( fgets (text, sizeof(text), input) ) 
{
  ... put the two for loops in here ...
}

As Coin said you can also count the words/sentancesa with one loop
Code:
for(v=0; text[v] != '\0'; v++)
{
     switch(text[v])
    {  
            case 'a': 
            case 'e': 
            case 'i': 
            case 'o': 
            case 'u':
                vowels++;
                break;

            case ' ': 
              words++
              break;

            case '.': 
             case '!': 
 	  case '?': 
 	  case ':': 
               sentences++;
               break;
  }
The break is important - it says, don't continue with the rest of the switch statement, just do the next entry.

Your if tree at the end is a little odd, do you know about "if else" - soemthing like this.
Code:
  if ( score > 70 )
  { ...
  }
  else if ( score > 50 )
  { 
     ...
  }
  else if ( score > 20 )
  { 
     ...
  }
  else
  {
    ...
   }
 
Last edited:
  • #14
ok i will try the if statement. i thought it had to be in ranges but your way looks good too. For the loop you said put the "two for loops in here" after the while loop...are you referring to the loops that will count the vowels, words, and sentences etc...thanks again for all your help... I am fairly new to programming and although i enjoy doing it, it sure is difficult!
 
  • #15
also...if i do the for loops they way you are suggesting i would only need one for loop correct?
 
  • #16
You don't need the ranges because they have already been taken care of by the previous 'if'. But you have to be careful to do the tests in the right order - you can't test for > 20 then > 40 because '30' would already have been done by the previous test.

The while loop runs for each line in the file - then within that line you have to count the vowels/sentances. So you need to put the loop that is scanning over the letters in a line within the while loop.

One small detail - your program doesn't correctly count words if they are at the end of a sentance. ( like the word sentance there ).

Programming is easier if you enjoy it!
Sketch out on paper what the flow is before you type it in.
Then test it by hand.
Write down a line of input, step through it on paper working out what you need to test about each input. Write down the value you expect for each variable at each step.
A big pile of paper helps!

Then it's just a trivial matter of getting the actual syntax for the language correct - the important part is understanding the algorithm (the sequence of questions to ask)
 
  • #17
how would you compensate for the words at the end of the sentence?
 
  • #18
mgb_phys said:
How do you get the CODE blocks to show as text, I tried all the escaping chars I could think of !
I just use HTML entities...

&#91;code&#93;

Will become

Code:
(And for the record I made the &s in this post with &#38;s...)
 
  • #19
so i tried the program again and it can be built, runs but with no output.
 
  • #20
If you don't have a debugger the first step is to put lots of
Code:
printf("ok1\n");
(change the number !) statements every few lines n- that at least narrows down where it fails.
You don't do any checking that eg. the file could be opened, check if "file" is not NULL immediately after the open call.
 
Last edited:
  • #21
ok so i tried to re-write the code and include printf("ok\n"); statements and before the while loop it will print the ok the printf statement after the while loop does not work. here is the code thus far:
Code:
#include <stdio.h>
#include <string.h>

int
main (void) 

{

int i, vowels, words, sentences; /* declaring variables */
 /* assigning variables a value of zero */
double score;
char text[1500];
FILE *input;
input = fopen("beatles.txt", "r");
vowels=0; words=0; sentences=0; 
printf("ok\n");  /*gets here*/

while(fgets(text,sizeof(text), input));
{ printf("ok\n");

	for(i=0; text[i] != '\0'; i++)
		{

			switch(text[i])
				{
	 				case 'a': 
	 				case 'e': 
				    case 'i': 
	 				case 'o': 
	 				case 'u':
     				vowels++;
	 				break;

					printf("vowels:%d", &vowels);


	 							case ' ': 
	 							words++;
	        					break;

								printf("words: %d", &words);


											    case '.': 
	 											case '!': 
	 											case '?': 
	 											case ':': 
     											sentences++;
												break;
												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)
          printf ("secondary education \n");
    
		  else if (score>=20)
             	printf ("College \n");
        
				  else if (score < 20)
               			printf ("expert \n");
	


fclose(input);

return(0);

}
 
  • #22
also so you know, the program gives a quincy error after it is run.
 
  • #23
akieft said:
Code:
while(fgets(text,sizeof(text), input));

This while loop has a null body! The code that follows processes one line of text, the very last one in the input file.
 
  • #24
Your problem is the semicolon on this line:

while(fgets(text,sizeof(text), input));

It's acting as a null statement. Just remove the semicolon.
 
  • #25
i removed the semi colon but the program is still giving the same error and not getting any farther than where i started before
 
  • #26
mgb_phys said:
How do you get the CODE blocks to show as text, I tried all the escaping chars I could think of !
Coin said:
I just use HTML entities...
&#91;code&#93;
Will become
Code:
(And for the record I made the &s in this post with &#38s...)[/QUOTE][/QUOTE]

HTML entities?? Yech. Try using the noparse vb Code:
[noparse][noparse][code][/noparse][/noparse]
will become
[noparse][code][/noparse]
 
  • #27
any ideas as to why this program is not properly working? does the program have to be saved to a main hard-drive or is it ok that my program is saved to a flash drive? This program is driving me nuts! I can't figure it out!
 
  • #28
here is my code so far:
Code:
#include <stdio.h>
#include <string.h>

int
main (void) 

{

int i, vowels, words, sentences; /* declaring variables */
 /* assigning variables a value of zero */
double score;
char text[1500];
FILE *input;
input = fopen("beatles.txt", "r");
vowels=0; words=0; sentences=0; 
printf("ok\n");  /*gets here*/

while(fgets(text,sizeof(text),input))
{

	for(i=0; text[i] != '\0'; i++)
		{

			switch(text[i])
				{
	 				case 'a': 
	 				case 'e': 
				    case 'i': 
	 				case 'o': 
	 				case 'u':
     				vowels++;
	 				break;

					printf("vowels:%d", &vowels);


	 							case ' ': 
	 							words++;
	        					break;

								printf("words: %d", &words);


											    case '.': 
	 											case '!': 
	 											case '?': 
	 											case ':': 
     											sentences++;
												break;
												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)
          printf ("secondary education \n");
    
		  else if (score>=20)
             	printf ("College \n");
        
				  else if (score < 20)
               			printf ("expert \n");
	


fclose(input);

return(0);

}

I would really appreciate help as this assignment is worth 10% of my grade and I honestly can not figure out what the error is with the while loop.
 
  • #29
akieft, we really can't help unless you tell us what is going wrong! It is very hard to tell you what "the error is" unless you describe the error. You say it is "not working properly", what is it doing instead of working properly?

Are you still seeing the "qcc.exe installation error"? If so I do not think anyone will be able to help you at all, because I don't think anyone here has used the Quincy C Compiler! If this is for a class, and the class was the one who provided the Quincy C Compiler, then you should be able to ask them how to compile and run a program.
 
  • #30
no the installation error is fixed. Basically the program runs, the output is ok1 and then quincy pops up an error saying that this program has quit unexpectedly and you have the option to close, debug, or send error to microsoft. If you click debug its just a bunch of numbers and I have no idea what they mean. I have a feeling it has to do with the while loop but I am unsure how to fix it.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
6K