Program not progressing

When I type in the file I want to use to proceed in checking for characters, words, and sentences it won't proceed and I am forced to end it with ctrl + c on. It is happening around

1
2
3
4
5
6
while(!in_stream.eof() && chars < MAX_Paragraph_chars)//while not end of file and not over max size of p fill p
        {
              in_stream.get(p[chars]);
              chars++;
        }


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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <iomanip>

using namespace std;

const int MAX_WORD_CHARS = 16;//longest word = 15 chars
const int MAX_WORDS = 200;//longest paragraph=200 words
const int MAX_Paragraph_chars = 3000;// 15*200
const int MAX_SENTENCES = 25; //max number of sentences in paragraph
const int MAX_SENTENCE_CHARS = 200;//max number chars in a sentence;
const int NUM_TO_BE_VERBS = 5;
const char TO_BE_VERBS[NUM_TO_BE_VERBS][MAX_WORD_CHARS]={"am","are","is","was","were"};//to be verbs
const char BE[] = "be";
const char TO[] = "to";
const int NUM_CONJUNCTIONS = 7;//number of conjunctions
//conjuctions with the commas
const char CONJUCTIONS[NUM_CONJUNCTIONS][MAX_WORD_CHARS]={"for","and","nor","but","or","yet","so"};//conjunctions
char p[MAX_Paragraph_chars +1];//max paragraph characters plus one for null
char tp[MAX_WORDS][MAX_WORD_CHARS];//2d array defined by max amount of words and max amount of characters per word
char file[15];//max characters per file
char empty;//to store if file is empty
int chars=0;
int end=0;
//functions
int tokenizeParagraph( char p[],char tp[][MAX_WORD_CHARS]);
double sentences(char p[]);
int tobe(char tp[][MAX_WORD_CHARS],int max_index);
int conjunctions(char tp[][MAX_WORD_CHARS],int max_index);



int main()
{
cout<<"Please enter your file name."<< endl;;//Prompt for file name
cin >> file;//read in you file name

ifstream in_stream;//declare input stream
in_stream.open(file);//open the stream to the file

if (in_stream.fail())//if file fails to open  read out error and exit
        {cout << "The file "<< file << " does not exist."<< endl;
                exit(1);}


in_stream >> empty;//read file into char to check if file is empty
 if(in_stream.eof())//if file is empty putback char
        {cout <<"The file "<< file <<" is empty."<< endl;//readout error
         in_stream.putback(empty);//not needed
         exit(1);}
in_stream.putback(empty);//putback char even if file is empty.

while(! in_stream.eof() && chars < MAX_Paragraph_chars)//while not end of file and not over max size of p fill p
        {in_stream.get(p[chars]);

        chars++;}

in_stream.close();//close file

double num_sentences=sentences(p);//stores the number of sentences
int words=tokenizeParagraph(p,tp);//stores the number of words returned from the function
int toobe=tobe(tp,words);//stores the number of tobe variables
int complex_sents=0;//variable for the number of compplex sentences
complex_sents=conjunctions(tp,words);//stores the variable of complex sentences
double avg;
avg=words/num_sentences;//calculates the avrage number of words per sentences
int simple_sent=num_sentences-complex_sents;//calculates the number of simple sentences
cout <<"Number of characters: "<< end-1 <<endl;//Readout the appropriate values in the correct forms
cout <<"Number of words: "<< words << endl;
cout <<"Number of sentences: " << num_sentences << endl;
cout << fixed << showpoint;
cout <<"Average number words in a sentence: "<<setprecision(1)<<avg  <<endl;
cout <<"Number of to be verbs: "<< toobe << endl;
cout <<"Number of simple sentences: "<<simple_sent<< endl;
//cout <<"Number of complex sentences: "<<complex_sents<<endl;


return 0;
}



int tokenizeParagraph( char p[],char tp[][MAX_WORD_CHARS])//takes an array of chars and breaks it up into a 2-d array of chars by whitespace
{
int i=0;
char* cPtr;
cPtr = strtok(p, " \n\t");//removes new lines whitespace and tab characters from the paragraph array.
while( cPtr != NULL)
        {
        strcpy( tp[i],cPtr);
        i++;
        cPtr=strtok( NULL, " \n\t");
        }
return(i);
}



double sentences (char p[])//counts teh number of sentences in the paragraph and returns the value
{

double sents=0;
while (p[end] != '\0')//while not at the null character look for the punctuation and add to the counter if it matches
{
        if (p[end] =='.'|| p[end] =='!' || p[end] == '?')
        {       sents++;}

end++;
}
return sents;
}

int tobe (char tp[][MAX_WORD_CHARS],int max_index)//counts the number of tobe verbs in the paragraph and returns it
{
int reg_tobe=0;
int index=0;
while (index <= max_index) //if any of teh words in the 2d array match those below add one to the counter check every index of the 2d array
        {
        if(strcmp(tp[index], "am")==0 || strcmp(tp[index],"is")==0 || strcmp(tp[index],"are")==0 || strcmp(tp[index],"was")==0 || strcmp(tp[index],"were")==0)
                {reg_tobe++;}
        if(strcmp(tp[index], "am.")==0 || strcmp(tp[index],"is.")==0 || strcmp(tp[index],"are.")==0 || strcmp(tp[index],"was.")==0 || strcmp(tp[index],"were.")==0)
                {reg_tobe++;}
        if(strcmp(tp[index], "Am")==0 || strcmp(tp[index],"Is")==0 || strcmp(tp[index],"Are")==0 || strcmp(tp[index],"Was")==0 || strcmp(tp[index],"Were")==0)
                {reg_tobe++;}
        index++;
        }
{
int index=0;
while (index < max_index)//checking for the sequence of to be in 2d array
        {
        if(strcmp(tp[index],"to")==0 && strcmp(tp[index+1],"be")==0)
                {reg_tobe++;}
        if(strcmp(tp[index],"To")==0 && strcmp(tp[index+1],"be")==0)
                {reg_tobe++;}
        if(strcmp(tp[index],"to")==0 && strcmp(tp[index+1],"be.")==0)
                {reg_tobe++;}
        index++;
        }
return reg_tobe;
}
}
int conjunctions (char tp[][MAX_WORD_CHARS],int max_index)// checks for conjunction sentences and returns the value
{
int index=0;
int count=0;
while (index < max_index)//while not at max index check for conjunctions and add to counter if it matches
        {
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"and")==0)
                {count++;}
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"but")==0)
                {count++;}
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"nor")==0)
                {count++;}
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"for")==0)
                {count++;}
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"or")==0)
                {count++;}
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"so")==0)
                {count++;}
                if((tp[index][strlen(tp[index])-1]==',')&& strcmp(tp[index+1],"yet")==0)
                {count++;}
        }
}
Last edited on
Have you tried stepping through it in a debugger, so that you can see exactly where it's hanging, and examine the state of the memory to see why?
Topic archived. No new replies allowed.