Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C program help

  1. Nov 22, 2007 #1
    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 != '\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);
    }
     
  2. jcsd
  3. Nov 22, 2007 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Would you care to be a little more specific?
    What does it do?
    What would you like it to do?
     
  4. Nov 22, 2007 #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.
     
  5. Nov 22, 2007 #4

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  6. Nov 22, 2007 #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?
     
  7. Nov 22, 2007 #6

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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?
     
  8. Nov 22, 2007 #7
    "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.
     
  9. Nov 22, 2007 #8
    akieft, what mgb means is that you should post your code like this:

    Code (Text):
    Put code here
     
  10. Nov 22, 2007 #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.
     
  11. Nov 22, 2007 #10
    Code (Text):
    #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);
    }
     
     
  12. Nov 22, 2007 #11

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    How do you get the CODE blocks to show as text, I tried all the escaping chars I could think of !
     
  13. Nov 22, 2007 #12
    any ideas how this will work. If fgets only gets one line, how would you read multiple lines?
     
  14. Nov 22, 2007 #13

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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 (Text):

    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 (Text):

    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 (Text):

      if ( score > 70 )
      { ........
      }
      else if ( score > 50 )
      {
         ........
      }
      else if ( score > 20 )
      {
         ......
      }
      else
      {
        ......
       }
     
     
    Last edited: Nov 22, 2007
  15. Nov 22, 2007 #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!
     
  16. Nov 22, 2007 #15
    also....if i do the for loops they way you are suggesting i would only need one for loop correct?
     
  17. Nov 22, 2007 #16

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    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)
     
  18. Nov 22, 2007 #17
    how would you compensate for the words at the end of the sentence?
     
  19. Nov 22, 2007 #18
    I just use HTML entities...

    &#91;code&#93;

    Will become

    Code (Text):


    (And for the record I made the &s in this post with &#38;s...)
     
  20. Nov 23, 2007 #19
    so i tried the program again and it can be built, runs but with no output.
     
  21. Nov 23, 2007 #20

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    If you don't have a debugger the first step is to put lots of
    Code (Text):
    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: Nov 23, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C program help
  1. Help with a C Program (Replies: 2)

  2. C program help (Replies: 3)

  3. Help in C programming (Replies: 1)

Loading...