Count Only ".", "?", and "!" from a text file.

I am trying to create a function where it will count each ending punctuation such as periods, question marks, and exclamation marks from a text file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int countSentences(Counts& counts, char line[])
{
counts.countSentences=0;
int i=0;
char p=0;
while (line[i] !='\0')
{
    if((p == '.')||(p == '?')||(p == '!'))
    {
        counts.countSentences++;
    }
}
i++;
p++;
}
The variable line is a line of text (a string). You are trying to loop through the characters in the string in lines 6 - 12, but you don't increment your counter until after the loop. Try moving line 13 to before line 12. This will allow i to change each time line 6 is called.

Next, you are comparing p to 3 different character constants. Unfortunately, p is given the value 0 in line 5 and never assigned a new value. I think you want to make the following assignment before line 8:

p = line[i];

There is no need to increment p (line 14).
I tried out your recommendation and at least its not causing an premature stop. The only problem i'm having now is that it's not incriminating.
please post your updated code.
Okay, give me a few mins.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<iostream>
#include<cctype>
#include<cstring>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include<string.h>

using namespace std;

//single structure that contains all functions + the line itself
struct Counts
{
  int countChars;
  int countWords;
  int countSentences;
};

//function names
int countChars(Counts&, char[]);
int countWords(Counts&, char[]);
int countSentences(Counts&, char[]);
void printRprt(Counts&);  

int main()
{
  //open input file (C-style)
  FILE *fp;
  fp = fopen("inp1.txt", "r");
  //error message
  if(fp == NULL)
  {
    printf("Could not open input file\n");
    exit(0);
  }

  //declare variables
  Counts counts;
  int i;
  char line[80];
  char delims[] = " ,\t\n"; //delimeters: space, ',', tab, return

  //get lines (max 80) from file
  //a line is a group of characters terminated by a \n
  //Call to the functions
  while(fgets(line, 80, fp))
  {
    printf(line);
    countChars(counts, line);
    countWords(counts, line);
    countSentences(counts, line);
    printRprt(counts);
  }
}

/*
 * Function name: countChars
 * Output: An integer
 * Description: Counts how many characters are on the given line.
*/
int countChars(Counts& counts, char line[])
{
  counts.countChars=0;
  int i=0; 
  while (line[i] !='\0')
  {
   {
   counts.countChars++;
   }
  i++;
  }
}
int countWords(Counts& counts, char line[])
{
  counts.countWords=0;
  char c;
  int i=0;
  while (line[i]!='\0')
  {
    c= line[i];
    if (isspace(c))
    {
    counts.countWords++;
    }
  i++;
  }
}
/*
int countWords(Counts& counts, char line[])
{
counts.countWords=1;
int i=0;
char c;  
while (line[i]!='\0')
    {
        if(c == ' ')
        {
            counts.countWords++;
        }
    }
  i++;
}*/

int countSentences(Counts& counts, char line[])
{
counts.countSentences=0;
int i=0;
char p=0;
while (line[i] !='\0')
{
    if((p == '.')||(p == '?')||(p == '!'))
    {
        counts.countSentences++;
    }
} 
i++;
}
/*
 * Function name: printReport
 * Output: Results of all the functions.
 * Description: Prints a report of all the functions.
*/
void printRprt(Counts& counts)
{
  cout <<"Total characters: " << counts.countChars <<"\nTotal words: " << counts.countWords<< "\nPunctuation:" <<endl;// counts.countSentence<< endl;
}
Last edited on
You didn't read my full post.

Next, you are comparing p to 3 different character constants. Unfortunately, p is given the value 0 in line 5 and never assigned a new value. I think you want to make the following assignment before line 8:

p = line[i];
Topic archived. No new replies allowed.