Thread: c programming help

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    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);
    }

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    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);
    }

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by akieft View Post
    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:&#37;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
    Last edited by Dino; 11-22-2007 at 09:43 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm wondering how you managed to post all that code with all those braces, without tripping the code checking tool.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed