+ 1

What's wrong with this code

This is a code coach activity #include<iostream> #include<cstring> #include<math.h> using namespace std ; int main(){ cout<<"hello world"<<endl<<endl; char s[100]; int count=1; cin.getline(s,100); for(int i=0;i<strlen(s);i++) { if(s[i+1]==' ') { count++; } } cout<<"total number of words are "<<count<<endl; int avgLetter=strlen(s)/count; cout<<" the average letter is: "<<ceil(avgLetter)<<endl; return 0; }

30th May 2022, 12:26 AM
Solaz
Solaz - avatar
3 Respostas
+ 1
Solaz Maybe you should change the data type of avgLetter from int to float? ceil does nothing to int. also, remove the hello world cout, as well as the other couts. just cout the ceil(avgLetter).
30th May 2022, 3:58 AM
Bob_Li
Bob_Li - avatar
0
Save this code in Code Playground as public, then edit your question and include a link to your saved code in the question description. This way, we can test it. Also, pls include in your question an explanation of your difficulties. This way, we know what you need an answer to.
30th May 2022, 2:53 AM
Emerson Prado
Emerson Prado - avatar
0
Solaz why didn't you just use string? it is more versatile. also, there is a condition in the problem that states remove all punctuation. You did not provide for that. Possibly problematic on other tests...
30th May 2022, 5:34 AM
Bob_Li
Bob_Li - avatar