#Sentence tracking Code

12 messages · Page 1 of 1 (latest)

fiery olive
#

int sentences(char ch, int sentenceCount) {
static bool insideSentence = false; // Tracks if we are inside a sentence
static bool hasLetter = false; // Tracks if at least one letter was found

if (std::isalpha(ch)) { // If it is a letter
hasLetter = true; // Mark that a letter was found
insideSentence = true; // We are inside a sentence
}
if (ch == '.' || ch == '?' || ch == '!') {
if (insideSentence && hasLetter) {
sentenceCount++;
}
}
insideSentence = false; // Reset sentence tracking
hasLetter = false; // Reset letter tracking
}
else {
insideSentence = false; // Reseting all other characters
hasLetter = false;
wordLength = 0;
allUpper = true;
}

return sentenceCount;
}

amber fern
#

!code

spiral shoreBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
spiral shoreBOT
#
int sentences(char ch, int sentenceCount) {
  static bool insideSentence = false;  // Tracks if we are inside a sentence
  static bool hasLetter = false;  // Tracks if at least one letter was found

  if (std::isalpha(ch)) {   // If it is a letter
    hasLetter = true;       // Mark that a letter was found
    insideSentence = true;  // We are inside a sentence
  }
  if (ch == '.' || ch == '?' || ch == '!') {
    if (insideSentence && hasLetter) {
      sentenceCount++;
    }
  }
  insideSentence = false;  // Reset sentence tracking
  hasLetter = false;       // Reset letter tracking
}
else {
  insideSentence = false;  // Reseting all other characters
  hasLetter = false;
  wordLength = 0;
  allUpper = true;
}

return sentenceCount;
}
Judithrex
#
How to Ask a Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What to Post

State your problem clearly and provide all necessary details:

  • the relevant portion of your code, or all of it
  • the expected output
  • the actual output (or the full error)
    :trophy: Gold Standard: Minimal Reproducible Example
Where to Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:

  • Compiler Explorer for most C and C++ snippets
  • OnlineGDB for interaction, debugging
    :no_entry: Do not post screenshots, let alone photos of your screen!
amber fern
#

Sorry I thought this was #cpp-help-text

#

Would be nice if you included int main function, so we can see how you’re calling this function and using it

fiery olive
#
#include <iostream>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <cstring>
int sentences(char ch, int sentenceCount) {
  static bool insideSentence = false;  // Tracks if we are inside a sentence
  static bool hasLetter = false;  // Tracks if at least one letter was found

  if (std::isalpha(ch)) {   // If it is a letter
    hasLetter = true;       // Mark that a letter was found
    insideSentence = true;  // We are inside a sentence
  }
  if (ch == '.' || ch == '?' || ch == '!') {
    if (insideSentence && hasLetter) {
      sentenceCount++;
    }
  }
  insideSentence = false;  // Reset sentence tracking
  hasLetter = false;       // Reset letter tracking
}
else {
  insideSentence = false;  // Reseting all other characters
  hasLetter = false;
  wordLength = 0;
  allUpper = true;
}

return sentenceCount;
}

int main() {
  char ch;
  int SentenceCount = 0;

  while (std::cin.get(ch)) {   
    SentenceCount = sentences(ch, SentenceCount);

  }

  std::cout << SentenceCount << " Sentences" << std::endl;
}
worthy patioBOT
#
Compiler Output
<source>: In function 'int sentences(char, int)':
<source>:5:12: error: 'isalpha' is not a member of 'std'
    5 |   if (std::isalpha(ch)) {   // If it is a letter
      |            ^~~~~~~
<source>:16:1: warning: no return statement in function returning non-void [-Wreturn-type]
   16 | }
      | ^
<source>: At global scope:
<source>:17:1: error: expected unqualified-id before 'else'
   17 | else {
      | ^~~~
<source>:24:1: error: expected unqualified-id before 'return'
   24 | return sentenceCount;
      | ^~~~~~
<source>:25:1: error: expected declaration before '}' token
   25 | }
      | ^
<source>: In function 'int main()':
<source>:31:15: error: 'cin' is not a member of 'std'
   31 |   while (std::cin.get(ch)) {
      |               ^~~
<source>:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | int sentences(char ch, int sentenceCount) {
<source>:36:8: error: 'cout' is not a
amber fern
#

There are still a bunch of errors to fix before your code can get reviewed.

  • Pay attention to where you place things like opening and closing braces and if and else statements.
  • In your else statement you have 2 variables you haven't declared yet but you try to use them