How to find vowels WITHOUT using array

Here is some code I have to do for class. I was wondering if you guys could tell me what i am doing wrong... it isn't wanting to compile. It sucks. It gives me a bunch of mumbo jumbo when it can't compile which looks like this:

/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h: In copy constructor âstd::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)â:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h:779: error: âstd::ios_base::ios_base(const std::ios_base&)â is private
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:55: error: within this context

So here is my actual code. We are working with functions, and I am lost. I would ask my teacher, but this is a late assignment and he is not in school at the moment. Laugh all you want at my hardship while you help :P.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

void PrintMenu(); //The Menu to be printed out
void IntSelect(int&, char); //To decide if a menu option was selected
void OpenFile(ifstream&, string&); //Opening a file
void countVow(int, char&, ifstream);
void countCon(int, char, ifstream, char);//Counting vowels and consonants

int main()
{
int num1, num2, num3;
float fnum1, fnum2, fnum3;
string filename;
bool endLoop;
char ch, ch2, ch3;

ifstream inFile;

endLoop = false;
while (!endLoop)
{
PrintMenu ();
IntSelect(num1, ch);
cout << num1 << endl;
cin.ignore (2000, '\n');

if (num1 == 1 || num1 == 2)
{
OpenFile(inFile, filename);
if (num1 == 1)
{
countVow(num2, ch2, inFile);
}
else
countCon(num3, ch3, inFile, ch2);
}
else if (num1 == 0)
{
endLoop = true;
}
else
cout << string(10, '*') << " Invalid Selection " << string(10, '*') << endl;
cout << "====> Invalid integer value entered\n";
cout << "====> Please enter 0, 1 or 2.\n";
cout << string(41, '*') << endl << endl;


}


return 0;
}


void PrintMenu() //Print the Menu
{
cout << string(15,'*') << " Options " << string(15, '*') << endl;
cout << "0. Exit Program \n";
cout << "1. Count number of vowels \n";
cout << "2. Count number of consonants \n";
cout << string(41, '*') << endl << endl;
cout << "Input your selection: ";

}

void IntSelect(int& num1, char ch) //Function called to determine if and what integer was selected
{
cin >> num1;

while (!cin)
{
//invalid character entered
cin.clear(); //clears the input stream
cin >> ch; //reads character
cout << ch << endl; //outputs character
cin.ignore (2000, '\n');
cout << string(10, '*') << " Invalid Selection " << string(10, '*') << endl;
cout << "====> Invalid character entered!!\n";
cout << "====> Please enter 0, 1 or 2.\n";
cout << string(41, '*') << endl << endl;
PrintMenu();
cin >> num1;
}
cout << num1 << endl;
cin.ignore (2000, '\n');
}


void OpenFile(ifstream& inFile, string& filename) //Function called to open our filestream
{
cout << "Enter the name of the input file now: ";
cin >> filename;
cout << filename << endl;

inFile.open(filename.c_str()); // Attempt to open file for input

while (!inFile) // Test status of input file
{
cout << endl << string(11,'*') << " File Open Error " << string(11,'*') << endl;
cout << "==> Input file failed to open properly!!\n";
cout << "==> Attempted to open file: " << filename << endl;
cout << "==> Selected operation has been cancelled\n";
cout << string(41,'*')<< endl << endl;


inFile.clear();

cout << "Enter the name of the input file: ";
cin >> filename;
cout << filename << endl;

inFile.open(filename.c_str()); // Attempt to open file for input again.
}



}


void countVow(int num2, char& ch2, ifstream inFile) //Function called to count vowels
{
num2 = 0;
inFile >> ch2;
if (!inFile)
{
cout << string(12, '*') << " File is Empty " << string(12, '*') << endl;
cout << "==> Input File is Empty" << endl;
cout << "==> No information to process" << endl;
cout << string(41, '*') << endl;
}

while (inFile)
{
if (ch2 == 'A' || 'a' || 'E' || 'e' || 'I' || 'i' || 'O' || 'o' || 'U' || 'u')
{
num2++;
}
}
cout << "Counting Number of Vowels" << endl;
cout << string(25, '-') << endl;
cout << "There are " << num2 << " vowels in the file" << endl;
cout << "There are ";


inFile.close();
}

void countCon(int num3, char ch3, ifstream inFile, int ch2) //Function called to count consonants
{
num3 = 0;
inFile >> ch3;
if (!inFile)
{
cout << string(12, '*') << " File is Empty " << string(12, '*') << endl;
cout << "==> Input File is Empty" << endl;
cout << "==> No information to process" << endl;
cout << string(41, '*') << endl;
}
else
while (inFile)
{
if (!ch2 && isalpha(ch3))
{
num3++;
}
}


inFile.close();

}
PASS. STREAMS. BY. REFERENCE.
Also, please use [code]your code here...[/code]. It makes it easier for people to help you if you use this format.
Last edited on
In the future please post with code tages. There is a menu to the right of your screen, click the button that looks like this <>. Then paste your code between the two tags.


Please note i didn't fix your code. I did however, make it so your code would compile. The rest is up to you.

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
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

void PrintMenu(); //The Menu to be printed out
void IntSelect(int&, char); //To decide if a menu option was selected
void OpenFile(ifstream&, string&); //Opening a file
void countVow(int, char&, ifstream&);
void countCon(int num3, char ch3, ifstream& inFile, int ch2);//Counting vowels and consonants

int main()
{
	int num1 = 0, num2, num3;
	float fnum1, fnum2, fnum3;
	string filename;
	bool endLoop = false;
	char ch = 'a', ch2, ch3;
	

	ifstream inFile;

	while (!endLoop)
	{
		PrintMenu();
		IntSelect(num1, ch);
		cout << num1 << endl;   
		cin.ignore (2000, '\n');

		if (num1 == 1 || num1 == 2)
		{
			OpenFile(inFile, filename);
			if (num1 == 1)
			{
				countVow(num2, ch2, inFile);
			}
			else;	
					countCon(num3, ch3, inFile, ch2);
		}
		 else if (num1 == 0)
		{
			endLoop = true;
		}
		else
			cout << string(10, '*') << " Invalid Selection " << string(10, '*') << endl;
		cout << "====> Invalid integer value entered\n";
		cout << "====> Please enter 0, 1 or 2.\n";
		cout << string(41, '*') << endl << endl;


	}


	return 0;
}


void PrintMenu() //Print the Menu
{
	cout << string(15,'*') << " Options " << string(15, '*') << endl;
	cout << "0. Exit Program \n";
	cout << "1. Count number of vowels \n";
	cout << "2. Count number of consonants \n";
	cout << string(41, '*') << endl << endl;
	cout << "Input your selection: ";

}

void IntSelect(int& num1, char ch) //Function called to determine if and what integer was selected
{
	cin >> num1;

	while (!num1) //cin isn't valid
	{
		//invalid character entered
		cin.clear(); //clears the input stream
		cin >> ch; //reads character
		cout << ch << endl; //outputs character
		cin.ignore (2000, '\n');
		cout << string(10, '*') << " Invalid Selection " << string(10, '*') << endl;
		cout << "====> Invalid character entered!!\n";
		cout << "====> Please enter 0, 1 or 2.\n";
		cout << string(41, '*') << endl << endl;
		PrintMenu(); 
		cin >> num1;
	}
	cout << num1 << endl;
	cin.ignore (2000, '\n');
}


void OpenFile(ifstream& inFile, string& filename) //Function called to open our filestream
{
	cout << "Enter the name of the input file now: ";
	cin >> filename;
	cout << filename << endl;

	inFile.open(filename.c_str()); // Attempt to open file for input

	while (inFile.fail()) // Test status of input file
	{
		cout << endl << string(11,'*') << " File Open Error " << string(11,'*') << endl;
		cout << "==> Input file failed to open properly!!\n";
		cout << "==> Attempted to open file: " << filename << endl;
		cout << "==> Selected operation has been cancelled\n";
		cout << string(41,'*')<< endl << endl;


		inFile.clear();

		cout << "Enter the name of the input file: ";
		cin >> filename;
		cout << filename << endl;

		inFile.open(filename.c_str()); // Attempt to open file for input again.
	}



}


void countVow(int num2, char& ch2, ifstream& inFile) //Function called to count vowels
{
	num2 = 0;
	inFile >> ch2;
	if (!inFile)
	{
		cout << string(12, '*') << " File is Empty " << string(12, '*') << endl;
		cout << "==> Input File is Empty" << endl;
		cout << "==> No information to process" << endl;
		cout << string(41, '*') << endl;
	}

	while (inFile)
	{
		if (ch2 == 'A' || 'a' || 'E' || 'e' || 'I' || 'i' || 'O' || 'o' || 'U' || 'u')
		{
			num2++;
		}
	}
	cout << "Counting Number of Vowels" << endl;
	cout << string(25, '-') << endl;
	cout << "There are " << num2 << " vowels in the file" << endl;
	cout << "There are ";


	inFile.close();
}	

void countCon(int num3, char ch3, ifstream& inFile, int ch2) //Function called to count consonants
{
	num3 = 0;
	inFile >> ch3;
	if (!inFile)
	{
		cout << string(12, '*') << " File is Empty " << string(12, '*') << endl;
		cout << "==> Input File is Empty" << endl;
		cout << "==> No information to process" << endl;
		cout << string(41, '*') << endl;
	}
	else
		while (inFile)
		{
			if (!ch2 && isalpha(ch3))
			{
				num3++;
			}
		}


		inFile.close();

} 



In the future I recommend building one function at a time. Compiling that function, debugging it if needed, and then moving on to the next function.

Last edited on
I apologize. I was trying to figure out how to make the code readable for you guys instead of that long gaggle crap. Thank you. I will definitely be building the functions one at a time.

I have also read the rules and couldn't figure out why it wasn't compiling before so I just posted the whole code. Again, thank you guys. I'll see what I can do from here. :).
This line looks like a problem:
if (ch2 == 'A' || 'a' || 'E' || 'e' || 'I' || 'i' || 'O' || 'o' || 'U' || 'u')
It may compile, but it wont work the way you want.

if (ch2 == 'A' || ch2 == 'a' || ch2 == 'E' || ch2 == 'e' /* etc. */ )

There are other problems, such as reading inFile properly.
Line 76, I suggest setting num1 to 0 before using it as an arguement in your while loop or use cin.good()

In countVow, the while loop should contain inFile >> ch2; no?

Same for the while loop in countCon

Line 154, ch2 should be char? So this condition is not very sound : line 168 if (!ch2 && isalpha(ch3)) because it will always be true for any value.

Try using an array of vowels.

gl
Topic archived. No new replies allowed.