-
counting vowels
After this program reads in a file, I want it to count all the vowels in the file and then output how many vowels there are. Right now it runs without any errors, but it always gives the number of vowels as 0. I'm guessing I need to add a for loop or something somewhere in the function?
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isVowel(char ch);
int main()
{
int vowel = 0;
int total = 0;
ifstream inFile;
char inChar = ' ';
string inputFile;
cout << "Enter the name of the text file: ";
cin >> inputFile;
inFile.open(inputFile.data());
if (!inFile.is_open())
{
cout << "input file did not open properly" << endl;
return 1;
}
for (;;)
{
if (inFile.eof())
{
break;
}
inFile.get(inChar);
cout << inChar;
}
isVowel(ch);
cout << " The number of vowels is " << vowel << endl;
return 0;
}
bool isVowel(char ch)
{
int vowel = 0;
switch(ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U': case 'Y':
case 'a': case 'e':
case 'i': case 'o':
case 'u': case 'y':
return true;
vowel++;
default:
return false;
}
}
-
Well the int vowel you increment in the function is not the same int vowel declared in main(), which remains untouched.
Try a simple
if ( isVowel(ch) ) vowel++;
-
i found it difficult to follow some of your code
so i wrote a simple one real quick and if you can follow it maybe you will see something to help you out.
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
FILE * myfile;
myfile = fopen("myvowelfile.txt","r");
int myVowel;
int numofvowels = 0;
myVowel = fgetc(myfile);
while(myVowel != EOF)
{
if(myVowel == 'A' || myVowel == 'a')
{
numofvowels++;
}
if(myVowel == 'E' || myVowel == 'e')
{
numofvowels++;
}
if(myVowel == 'I' || myVowel == 'i')
{
numofvowels++;
}
if(myVowel == 'O' || myVowel == 'o')
{
numofvowels++;
}
if(myVowel == 'U' || myVowel == 'u')
{
numofvowels++;
}
if(myVowel == 'Y' || myVowel == 'y')
{
numofvowels++;
}
myVowel = fgetc(myfile);
}
cout << "You have " << numofvowels << " Vowels in your file!" << endl;
getch();
return 0;
}
-
i want the function to count the number of vowels and the main part to print out the results. tried if ( isVowel(ch) ) vowel++; but it still prints out 0.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isVowel(char ch);
char ch;
int main()
{
int vowel = 0;
ifstream inFile;
char inChar = ' ';
string inputFile;
cout << "Enter the name of the text file: ";
cin >> inputFile;
inFile.open(inputFile.data());
if (!inFile.is_open())
{
cout << "input file did not open properly" << endl;
return 1;
}
for (;;)
{
if (inFile.eof())
{
break;
}
inFile.get(inChar);
cout << inChar;
}
isVowel(ch);
cout << "The number of vowels is " << vowel << endl;
return 0;
}
bool isVowel(char ch)
{
int vowel = 0;
switch(ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U': case 'Y':
case 'a': case 'e':
case 'i': case 'o':
case 'u': case 'y':
return true;
default:
return false;
}
if (isVowel(ch)) vowel++;
}
-
See if you can follow this.
Code:
#include <iostream>
using namespace std;
// Function returns true if vowel
bool is_vowel ( char the_char )
{
// Check for any case of vowels
switch ( the_char ) {
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
return true;
default: return false;
}
}
int main ()
{
char the_string [] = "Hello World";
int vowel = 0;
// Loop for the length of the string to check all of the characters
for ( int loop = 0; loop < strlen ( the_string ); loop++ ) {
// If there is a vowel increase the counter
if ( is_vowel ( the_string [loop] ) ) vowel++;
}
// Output: There are 3 vowels in "Hello World"
cout << "There are " << vowel << " vowels in \"" << the_string << "\"";
return 0;
}
Your main roblem I believe was the scope of your variables. The only counter you increased was trapped in the function.
-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isVowel(char ch);
int main()
{
int vowel = 0;
ifstream inFile;
char inChar = ' ';
string inputFile;
cout << "Enter the name of the text file: ";
cin >> inputFile;
cin.ignore(1);//ignore the \n
inFile.open(inputFile.c_str());
if (!inFile.is_open())
{
cout << "input file did not open properly" << endl;
cin.get();
return 1;
}
for (;;)
{
if (inFile.eof())
{
break;
}
inFile.get(inChar);
if(isVowel(inChar)) vowel++;//put it here
cout << inChar;
}
cout << "The number of vowels is " << vowel << endl;
cin.get();//give the user the time to read output
return 0;
}
bool isVowel(char ch)
{
switch(ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U': case 'Y':
case 'a': case 'e':
case 'i': case 'o':
case 'u': case 'y':
return true;
break;
default:
return false;
break;
}
}
-
I don't know where to begin. You need to work a little bit on your understanding of program flow. A program needs more than all of the parts to work, the parts must be put together in the right way to work together.
First, you have a loop that reads in the file one char at a time. Inside that loop, all you do is output that char. The variable inChar only holds the value until the next time through the loop when it is overwritten with the next value read in. So really, you are reading in the entire file and doing nothing with it. The point of the loop would be to read in each character, and check each character if it is a vowel, and if it is, add one to the count of the number of vowels.
The problem is that you don't call the isVowel function until after the entire file has been read in. That function will not be called inside the for loop unless you put it inside the for loop.
Now, inside isVowel function, you have a switch statement. The code will always execute something from the switch statement, because you have a default case that handles everything that isn't a vowel. That means that every time the code in the function is executed, either the return true; or the return false; will be executed. That means that your function will always return and be finished there. The extra line of code: if (isVowel(ch)) vowel++; will never be executed.
The key is to view the program as a whole in a logical order. Imagine you are the computer, and you are doing the things the program tells you to do. This is how your current program would look:
- Create a char variable named ch.
- Start the main function.
- Create an int variable named vowel and give it a value of 0.
- Create an ifstream variable called infile, a char variable called inChar (initialized to ' ') and a string variable called inputFile.
- Ask the user for the name of the file.
- Read in the first word entered by the user and store it in the variable inputFile.
- Open the file using the ifstream inFile. (Note: that should be c_str(), not data()).
- If the file fails to open, then output an error message and exit the main function. Program DONE. Otherwise continue.
- Do the following steps forever:
- If the input file has reached eof, break out of the forever loop. (Note: you should check for eof after you try to read in a character but before you try to use that character).
- Otherwise get the next character from the file.
- Output the character to the screen.
- Continue the loop.
- Call the isVowel function. Pass it the data that is in the ch variable (which is currently the nul character because the ch variable refers to the global variable created in step 1).
- In the isVowel function, create a variable called vowel and initialize it to 0.
- Look at the character passed in. If it is a vowel, return true. Otherwise return false.
- Exit the function isVowel with either true or false (in this case false because the variable ch will always be not a vowel).
- Back inside the main function, ignore the true or false returned from the isVowel function.
- Output the value of the vowel variable created in step 3 (which is different than the one created in step 11). That value has not changed, and so it will always be 0.
- Exit the main function, returning a value of 0.
- Program done, tell the system that the program exited and returned a value of 0.
Hopefully you can see why it always outputted 0. Now look at the solutions provided by others above and see if you can follow the program flow they provided, and see where it is different and more effective than yours.
Good Luck!
-
Try this:
1)create a char array that holds all the characters in the file, call it "ch" or something.
2)create an integer which is equal to the number of characters in the file:
while (!.eof)
{
int b = 0;
b++;
}
//then
int score;
for (int i = 0, i<b, i++)
{
if (ch[i] == "a" || ch[i] == "e" || ch[i]=="i" || ch[i] == "o" || ch[i] == "u")
{
score++;
}
if (score >0)
{
return score;
}
elseif (score==0)
{
cout<<"No vowels"<<endl;
return 0;
}
This is a usefull trick I discovered while making a program that prints prime numbers.
Hope this helps
-
One question: Why has nobody using switch...case used the tolower() function to save you from having to trap lower and upper case letters? Is it a taboo, or just something nobody does?
-
To be honest it never crossed my mind.
I suppose that would be ok. I am now also wondering if it is a no-no...
-
> tried if ( isVowel(ch) ) vowel++; but it still prints out 0.
Because you chose the wrong one
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isVowel(char ch);
//char ch;
int main()
{
int vowel = 0;
ifstream inFile;
char inChar = ' ';
string inputFile;
cout << "Enter the name of the text file: ";
cin >> inputFile;
inFile.open(inputFile.data());
if (!inFile.is_open())
{
cout << "input file did not open properly" << endl;
return 1;
}
for (;;)
{
if (inFile.eof())
{
break;
}
inFile.get(inChar);
cout << inChar;
if (isVowel(inChar)) vowel++; // HERE!!!!!!
}
// HUH??? isVowel(ch);
cout << "The number of vowels is " << vowel << endl;
return 0;
}
bool isVowel(char ch)
{
int vowel = 0;
switch(ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U': case 'Y':
case 'a': case 'e':
case 'i': case 'o':
case 'u': case 'y':
return true;
default:
return false;
}
// NOT HERE!!!!
// if (isVowel(ch)) vowel++;
}