0

whats wrong with my code? this is the part that doest work

float L = (letters / words) * 100; and float S = (sentences / words) * 100;

when I plug in letters, words, and sentences into the formula it gives the wrong answer.

#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <ctype.h>
#include <math.h>

int main(void)
{
char ch[500];

int i;
int ii; 
int iii;

int LettersWspace = 0;
int letters = 0;
int words = 1;
int sentences = 0;
int index;



string text = get_string("Enter Text: ");

 for(i = 0; text[i] != '\0'; i++)
 {
LettersWspace++;
 }
for( i = 0; i < LettersWspace; i++)
 {
if (text[i] == 32)
    letters++;
  }
  letters = LettersWspace - letters;

  ii = 0;
{
  if (text[ii] == ' ')
{
    words++;
}
ii++;
}

while (ii <= text[ii]);
   

iii = 0;
do
    {
if (text[iii] == '?' || text[iii] == '!' || text[iii] == '.')
{
    sentences++;
}
iii++;
}

while (iii <= text[iii]);

float L = (letters / words) * 100;

float S = (sentences / words) * 100;

return (index = 0.0588 * L - 0.296 * S - 15.8);


printf("%i\n", (int) sentences);

return 0;

    }
Obsidian
  • 3,719
  • 8
  • 17
  • 30
itzdre96
  • 1
  • 4
  • 1
    Firstly the indentation is wrong :( – MikeCAT Jun 26 '21 at 22:35
  • 1
    "doesn't work" is never a good problem description. Please give the exact input, expected result and actual result. Also, what debugging have you done? Use a debugger to step through the code. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Jun 26 '21 at 22:37
  • 1
    `while (ii <= text[ii]);` and `while (iii <= text[iii]);` without `do` are also wrong. They will run infinite or zero times. – MikeCAT Jun 26 '21 at 22:37
  • 1
    @MikeCAT I think those `while` lines have a `do` before them ... but the indentation makes it hard to see. (Well, one does.) – Adrian Mole Jun 26 '21 at 22:38
  • @AdrianMole Oops, `while (iii <= text[iii]);` actually has `do`. but `while (ii <= text[ii]);` doesn't. – MikeCAT Jun 26 '21 at 22:40
  • Yeah. One do doesn't do; two dos will do. – Adrian Mole Jun 26 '21 at 22:41
  • when i plug letters, words, and sentences into the Coleman-Liau index it gives me the wrong answer – itzdre96 Jun 26 '21 at 22:44
  • i know the indentation is wrong i had to change it just to post it on here but the part that doesnt work is this > float L = (letters / words) * 100; float S = (sentences / words) * 100; – itzdre96 Jun 26 '21 at 22:46
  • so what am i doing wrong? thats the part thats not working but i dont understand – itzdre96 Jun 26 '21 at 22:49
  • https://cs50.harvard.edu/x/2021/psets/2/readability/ Read the hints section. – Retired Ninja Jun 26 '21 at 22:55
  • so i read the hint section and i casted the division as a float but now when i try to print L to see the value it doesnt print anything at all.. – itzdre96 Jun 26 '21 at 23:07
  • Same thing with S, when i print it to see the value there is no value it doesnt print anything at all – itzdre96 Jun 26 '21 at 23:13
  • what other way can i create the S and L variables with the formula im using? – itzdre96 Jun 26 '21 at 23:14
  • So when i use the example on the cs50 course "Congratulations! Today is your day. You're off to Great Places! You're off and away!" it gives me a integer number of 55 – itzdre96 Jun 27 '21 at 00:15
  • Yet on the course it says "Congratulations! Today is your day. You're off to Great Places! You're off and away!" should print out a grade level of 3 so the S and L are still having issues. – itzdre96 Jun 27 '21 at 00:17
  • that worked thank you so much, can you explain that solution so i understand and i think theres only a issue with rounding the index now because some of them give the correct output and some are off by 1 – itzdre96 Jun 27 '21 at 00:56
  • i tried to round the index like this " round(index = 0.0588 * L - 0.296 * S - 15.8); " and i get a error saying " readability.c:67:4: error: ignoring return value of function declared with const attribute [-Werror,-Wunused-value] round(index = 0.0588 * L - 0.296 * S - 15.8); ^~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " – itzdre96 Jun 27 '21 at 00:57
  • " You seem to be calling round on line 67 of readability.c but aren't using its return value. Did you mean to assign it to a variable? " – itzdre96 Jun 27 '21 at 01:06
  • i tried return round(index = 0.0588 * L - 0.296 * S - 15.8); and now it wont print anything – itzdre96 Jun 27 '21 at 01:09
  • Looks like you're trying to declare a function on line 67 of readability.c. Be sure, when declaring a function, to specify its return type just before its name. – itzdre96 Jun 27 '21 at 01:17
  • There are some syntax errors in your code. please pass the compiler first. – Milo Chen Jun 27 '21 at 08:58
  • Most of your class mates fail because of using integer division where float values are needed. – Yunnosch Jun 27 '21 at 22:02

0 Answers0