Reading in a char from a string

Here's the problem. In getWord(string&); I'm trying to read in a character, determine if its a nonblank or blank character and from then beginning of the string line to the next blank character (excluding nonblank characters) should equal a word. But it keeps going into an infinite loop.

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 <fstream>
#include <cstring>

using namespace std;

void border();
void printHeading();
void getLine();
int getWord(string&);
void getChar();
ifstream myIn;
const string delimiter = " .,?:;!()";


int main()
{
	border();
	printHeading();
	border();
	getChar();
	getLine();	
	return 0;
}

void printHeading()
{
	
	cout << "Welcome to the revolutionary Counter 3000! \n";	
	
}

void border()
{
	
	cout << "****************************************** \n";
	
}
void getLine()
{
	myIn.open("lab7dat.txt");
	string line;
	int lineCounter = 0, wordCounter = 0;
	while (myIn)
	{	
		getline(myIn, line);
		wordCounter = getWord(line);
		lineCounter++;
	}
	cout << "Number of lines in the file: " << lineCounter << endl;
	cout << "Number of words in the file: " << wordCounter << endl;
	myIn.close();
	
}

int getWord(string& line)
{
	
	string word;
	int length, i = 0, wordCounter = 0, counter = 1, pos = 0;
	char letter;
	length = line.length();
	myIn.get(letter);
	while(letter == ' ' || letter == '\t' || letter =='.' || letter ==',' 
			|| letter =='?' || letter ==':' || letter ==';' || 
					letter =='!' || letter =='(' || letter ==')')  
	{
		myIn.get(letter);
		counter++;
	}
	pos = counter;
	i = counter;
	
	while(i < length) 
	{
		while (letter != ' ' || letter != '\t' || letter !='\n' || letter =='.' || 
					letter ==',' || letter =='?' || letter ==':' || 
					letter ==';' || letter =='!' || letter =='(' || letter ==')')
		{
			myIn.get(letter);
			counter++;
		}
		word = line.substr(pos, counter);
		cout << word << endl;
		wordCounter++;
		pos = counter;
		i = i + counter;
		while (letter == ' ' || letter == '\t' || letter =='.' || 
					letter ==',' || letter =='?' || letter ==':' || letter ==';' || 
					letter =='!' || letter =='(' || letter ==')')
		{
			myIn.get(letter);
			if (letter == '\n')
			{
				cout << endl;
				return wordCounter;
			}
			counter++;
		}
   }
	return wordCounter;
}

void getChar()
{
	int charCounter = 0, nonBlankCounter = 0, blankCounter = 0;
	char letter;
	myIn.open("lab7dat.txt");
	while (myIn)
	{
		myIn.get(letter);
		if (letter =='.' || letter ==',' || letter =='?' || letter ==':' || letter ==';' || 
				letter =='!' || letter =='(' || letter ==')')
		{
			nonBlankCounter++;
		}
		else if (letter == ' ' || letter == '\t' || letter == '\n')
		{
			blankCounter++;
		}
		charCounter++;
	}		
	charCounter = charCounter - (nonBlankCounter + blankCounter);
	cout << "Number of characters in the file: " << charCounter << endl;
	cout << "Number of non-blank characters in the file: " << nonBlankCounter << endl;
        cout << "Number of blank characters in the file: " << blankCounter << endl;
        myIn.close();
}


Here's the output:

1413 ranger2$ a.out
****************************************** 
Welcome to the revolutionary Counter 3000! 
****************************************** 
Number of characters in the file: 48
Number of non-blank characters in the file: 6
Number of blank characters in the file: 15
(infinite loop..........)
Last edited on
There is no function named getLine that takes a reference to a string. Did you mean getWord(string &) ? In that case, you *could* just do this to get each individual word:
1
2
3
4
5
6
std::istringstream linestream (line);
std::string word;
while(linestream >> word)
{
    //process word, it will already have spaces trimmed
}
Topic archived. No new replies allowed.