Simple Program just stops

Hi, I wrote a simple program for class and its made to run with input using linux redirection, the problem is that it just stops about after about the 20th line, pretty much after my variable declarations and first cout statement. I tested it and it never even gets to my first loop, just looking for any help or ideas. It does do the first cout command and shows me the first string in my file and then nothing after that, doesnt even do cout << count2.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
double vowelcount(string);
string convert(string);
double consonantcount(string);

int main()
{
string word1;
int i = 0;
int vowels = 0; 
int consonants = 0;
double wordcount = 0;
double count1 = 0;
double count2 = 0; 
cin >> word1;
cout << word1 << endl;
cout << count2;
while (!cin.eof())
{
	cout << "test";
	wordcount++;
	i++;
	vowels = vowels + vowelcount(word1);
	consonants = consonants + consonantcount(word1);
	count1 = count1 + word1.length();
	word1 = convert(word1);
	if (i % 10 != 0)
		cout << word1 << " ";
	if (i % 10 == 0)
		cout << word1 << endl;
	count2 = count2 + word1.length();
	cin >> word1;
}
cout << "Word count: " << wordcount << endl;
cout << "Vowel count: " << vowels << endl;
cout << "Consonant count: " << consonants << endl;
}

string convert(string text)
{
	int last;
	char firstch, lastch;
	last = text.length();
	firstch = text[0];
	lastch = text[last];
	while (!isalnum(firstch))
		text[0] = ' ';
	while (!isalnum(lastch))
		text[last] = ' ';
	if (text.length() % 2 != 0)
	{	
		int n;
		n = (text.length() / 2) + 1;
		if (!isupper(text[n]))
			text[n] = toupper(text[n]);
		else
			if (!islower(text[n]))
			text[n] = tolower(text[n]);
	}
	
	return text;
}

double vowelcount(string text)
{
int length;
double vowelcount=0;
length = text.length();
for (int i=0; i<=length; i++)
	{
	if (isalpha(text[i]))
	{
		text[i] = toupper(text[i]);
		if (text[i] == 'A' || text[i] == 'E' || text[i] == 'I'||
			text[i] == 'O' || text[i] == 'U')
			vowelcount++;
	}
	}
return vowelcount;	
}

double consonantcount(string text)
{
int length;
double concount=0;
length = text.length();
for (int i=0; i <= length; i++)
	{
	if (isalpha(text[i]))
	{
		text[i] = toupper(text[i]);
		if (text[i] != 'A' && text[i] != 'E' && text[i] != 'I' &&
			text[i] != 'O' && text[i] != 'U')
			concount++;
	}
	}
return concount;	
}
Last edited on
1
2
	while (!isalnum(firstch)) //infinite loop
		text[0] = ' ';
¿what were you trying to do there?

Also, keep in mind that array index goes from 0 to n-1. Accessing out of bounds is an error, as you are doing in line 53
I was trying to create a loop that would check to see if the first character of the string was alphanumeric, and if it was, to remove it from the string, then check what would be the new first character and do the same. As for line 53, I would just have to change that value to (last - 1)?
The goal of the assignment was to take words from a file, count consonants, vowels, words and characters, remove any non-alphanumeric characters from the beginning and end of the word, and then take any words with odd number of letters, and change the case of the letter in the middle of the word. Then at the end display the words in lines of 10 words per line, and then display the total vowels, consonants, characters, words and average word length before and after any changes.
Topic archived. No new replies allowed.