Calculating Reading Score with a C Program

In summary, the conversation is about a person seeking help with their C program to read in data from a file and calculate the number of words, vowels, and sentences in the text. They also mention using quincy 99 as their compiler and experiencing an error message. A suggestion is made to use CODE blocks when posting code for easier reading. The person also receives feedback on their code and suggestions for improvement.
  • #1
akieft
17
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
  • #2
doesnt work
Would you care to be a little more specific?
What does it do?
What would you like it to do?
 
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
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?
 
  • #7
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.
 
  • #8
akieft, what mgb means is that you should post your code like this:

Code:
Put code here
 
  • #9
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.
 
  • #31
akieft said:
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.

When you see that error, it usually means you have committed a "pointer" error. That means you are trying to use a piece of memory which you think contains data, but which actually "does not exist".

The only place I see in your code where you could have committed such an error is with your FILE *input variable. It is possible, if fopen fails, that it could return NULL. Then when you say fgets(...input) later, fgets will try to read from NULL (bad) and crash.

Try adding the line

perror("fopen");

Right after you say fopen (this will cause fopen's exact error message to be printed, if there is one), or changing your printf right there to

printf("ok %x\n", input); /*if fopen succeeded, this will print a nonzero number, if it failed it will print "ok 0"*/

(If you want to be really fancy you could to an if() test to see whether "input" is equal to 0, and if input is equal to 0 print "Beatles.txt not found!" and politely quit. Your teacher would probably prefer this. But this will not solve the problem that your program doesn't work, it will just make it not work in a more polite way...)

Most compilers come with a "debugger", which when you commit a pointer error they will stop the program in its tracks and show you the source code of the line where the error happened. It is essential you learn to use a debugger someday. Does QCC not come with one?
 
Last edited:
  • #32
not that we have been taught no...Our prof has never shown us a debugger to my knowledge neither has our TA
 

Similar threads

  • Programming and Computer Science
Replies
4
Views
710
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
837
  • Programming and Computer Science
Replies
1
Views
895
Back
Top