Reputation: 9
1- I don't know what's wrong with my code, this is ## help ## readability CS50 pset2. 2- The problem seems to be in float grade (string grade) function. 3- I am a beginner and it's my second time coding in C so if I missed something important please tell me, or if you have any tips that make my code looks better, thank you!
//Libraries
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//Functions
int count_letters(string letter);
int count_words(string word);
int count_sentences(string sentence);
float grade(string grade);
int main(void)
{
string par = get_string("text: ");
}
int count_letters(string letter)
{
int letters = 0;
for (int i = 0, n = strlen(letter); i < n; i++)
{
if (isalpha(letter[i]))
letters++;
}
return letters;
}
int count_words(string word)
{
int words = 1;
int spaces = 0;
int nword;
nword = strlen(word);
for (int i = 0; i < nword; i++)
{
if (isspace(word[i]))
spaces++;
words = spaces + 1;
}
return words;
}
int count_sentences(string sentence)
{
int sentences = 0;
int nsentence;
nsentence = strlen(sentence);
for (int i = 0; i < nsentence; i++)
{
if (sentence[i] == '.' || sentence[i] == '!' || sentence[i] == '?')
sentences++;
}
return sentences;
}
float grade(string grade)
{
//here comes my problem. variables like letters, words, and sentences are "undeclared identifier"
float x = 0.0588 * (100 * letters / words) - 0.296 * (100 * sentences / words) - 15.8;
if (x < 16 && x >= 0)
{
printf("Grade %i\n", (int) round(x));
}
else if (x >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Before Grade 1");
}
}
Upvotes: -2
Views: 472
Reputation: 11
Try this Out!!
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int count_words(string text);
int count_letters(string text);
int count_sentences(string text);
int Coleman_index(int c_w, int c_l, int c_s);
void classify(int index);
int main(void)
{
string text = get_string("Text: ");
/*
1. read the data
2. count the number of words, letters, sentences;
3. While we count the number of words for every 100 words we will be finding L, S and round off L, S
4. We will use the following formula: index = 0.0588 * L - 0.296 * S - 15.8
5. From that we get the class estimation
*/
// Counting the letters, words, and sentences
int c_w = count_words(text);
int c_l = count_letters(text);
int c_s = count_sentences(text);
// Calculating the class with Coleman-Liau index
int index = Coleman_index(c_w, c_l, c_s);
// Printing the result
classify(index);
}
int count_words(string text)
{
int c = 0;
bool in_word = false;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isspace(text[i]))
{
in_word = false; // We are not in a word anymore
}
else
{
if (!in_word)
{
c++; // We found a new word
in_word = true; // We are now in a word
}
}
}
return c;
}
int count_letters(string text)
{
int c = 0;
for (int i = 0; i < strlen(text); i++)
{
if (isalpha(text[i]))
{
c++; // Count only alphabetic characters
}
}
return c;
}
int count_sentences(string text)
{
int c = 0;
for (int i = 0; i < strlen(text); i++)
{
if (text[i] == '.' || text[i] == '?' || text[i] == '!')
{
c++;
}
}
return c;
}
int Coleman_index(int c_w, int c_l, int c_s)
{
float L = (float)c_l / c_w * 100; // Average letters per 100 words
float S = (float)c_s / c_w * 100; // Average sentences per 100 words
int index = round(0.0588 * L - 0.296 * S - 15.8);
return index;
}
void classify(int index)
{
if (index < 1)
{
printf("Before Grade 1\n");
}
else if (index >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %d\n", index);
}
}
Upvotes: 0
Reputation: 123568
The compiler is telling you exactly what the problem is - you haven't declared letters
, words
, or sentences
in the grade
function.
The declarations of those variables in other functions are local to those functions - they are not visible from within the grade
function.
You have a couple of bigger issues, though. For one thing, none of your functions are being called from anywhere - main
reads a string and then immediately exits without doing anything else. For another, grade
doesn't have a way of knowing what values should be in letters
, words
, or sentences
.
What you probably want to do is call your counting functions from within grade
like so:
float grade( string s )
{
int letters = count_letters( s );
int words = count_words( s );
int sentences = count_sentences( s );
float x = ...;
...
}
and then call grade
from main
as
int main( void )
{
string par = get_string( "Text: " );
grade( par );
}
Alternately, you could call each of the counting functions from main
and pass the results as arguments to grade
:
int main( void )
{
string par = get_string( "Text: " );
int letters = count_letters( par );
int words = count_words( par );
int sentences = count_sentences( par );
grade( letters, words, sentences );
}
in which case both the declaration and definition of grade
should be
float grade( int, int, int ); // declaration
float grade( int letters, int words, int sentences ) // definition
{ // since they are declared as arguments,
float x = ...; // there's no need to declare them in the body of the function
}
Now, here's a question - do you intend for anyone to use the value of x
after grade
exits? If so, then you need to add a
return x;
to the end of grade
.
If not, then change the function's return type from float
to void
in both the declaration and definition:
void grade( string );
void grade( string s ) { ... } // definition
Upvotes: 0