Good day: There have been similar posts to my question...but i don't see what I'm doing wrong. Our professor wanted us to use a switch/case statement, we started transversing a string....The assisgnment : Write a program that will read in a one line phrase from a text file named “vowels.txt”.
Output, to the screen and printer, the phrase and then the number of vowels in the phrase.
Example:

The slippery eel found no help from an outside source .
The number of vowels is 18.

Below is what I've got so far:

#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;


int main()
{
	ifstream inFile;
	inFile.open ("vowels.txt");

	string sentence;//sentence from inFile
	

	char ch;
	int i;
	int length = 0;
	
	while (getline(inFile,sentence))
	{
	cout<<sentence<<endl;
	}

	int numz = sentence.length();//number of vowels

	for (int i = 0; i < numz; ++i)
	{
		ch = toupper (sentence [i]);
		if (isalpha (ch) && (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'))
		{
			numz++;
		}
	}
	

	cout<<"The number of vowels: "<<numz<<endl;

	inFile.close();
	return 0;
}

It only counts the number of vowls in the last line read. Move the closing brace at line 27 down to line 39 so that the program will count the number of vowles in every line. Also in line 34 is it not necessary to use isalpha because checking for each vowel letter will do that as well.

>>Our professor wanted us to use a switch/case statement
Then replace that if statement with a switch statement

switch (ch)
{
    case 'a': case 'e': case 'i': case 'o': case 'u':
          // do something
          break;
    default:
         // not a vowel
         break;
}

Thx for the reply.....it's only one sentence...I had moved the '}'....the error message that i get is : "error C2065: 'numz' : undeclared identifier". Additionally, i get awarning which says: " warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data".

>error C2065: 'numz' : undeclared identifier
Your code looks fine. Did you make any changes from what was posted originally?

>warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
You can ignore this, but it's a good practice to match the type of whatever values you're getting. In this case, the return type of string::length is string::size_type, which in your case equates to std::size_t. This would be better:

std::string::size_type numz = sentence.length();

Below is my revised version of the code...No alternation was made, dunno why it's not compiling:

#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;


int main()
{
	ifstream inFile;
	inFile.open ("vowels.txt");

	string sentence;//sentence from inFile
	

	char ch;
	int i;
	int length = 0;
	
	
	while (getline(inFile,sentence))
	{
	cout<<sentence<<endl;
	

	int numz = sentence.length();//number of vowels

	for (int i = 0; i < numz; ++i)
	{
		switch (ch)
		case 'a': case 'e': case 'i': case 'o': case 'u':

		{
			numz++;
		}
	}
	}
	

	cout<<"The number of vowels: "<<numz<<endl;

	inFile.close();
	return 0;
}

Thx for the assistance..

what error(s) do you get?

Move declaration of numz outside that while loop, probably just under the declaration of the other integers.

I had run the following...however, it shows that the number of vowels is 0.

#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>

using namespace std;


int main()
{
	ifstream inFile;
	inFile.open ("vowels.txt");

	string sentence;//sentence from inFile
	

	char ch;
	int i;
	int length = 0;
	int numz = sentence.length();//number of vowels
		
	while (getline(inFile,sentence))
	{
	cout<<sentence<<endl;
	

	for (int i = 0; i < numz; ++i)
	{
		switch (ch)
		case 'a': case 'e': case 'i': case 'o': case 'u':
		
		{
			numz++;
		}
	}
	}
	

	cout<<"The number of vowels: "<<numz<<endl;

	inFile.close();
	return 0;
}

Eliminate the "magic" in which you don't tell the program what to do but you want it to work right anyway. Things such as

  • Calculating the length of a sentence before you've read the sentence.
  • Evaluating the value of ch before you've given it a value.

Most folks would write the switch statement like this.

switch ( sentence[i] )
         {
         case 'a': case 'e': case 'i': case 'o': case 'u':
            numz++;
            break;
         }

Note where the braces are placed. And you may want to get into the habit of putting a break in there.

Make sure to test if the stream is open

inFile.is_open()

if it's not open your vowels.txt file is in the wrong directory.

that being said if you want I can PM you a working version, a little simpler, but i will wait till you struggle through a bit more before posting it :P

either way you've made good progress.

EDIT:

please remember that HI HELLO would look different than Hi Hello, your current statement would not count the vowels in the first uppercase version.

Thanks much for all the feedback...I modified the program and it calcualtes the number correctly.:P

Here is a version I wrote based on your code and heavily commented :)

#include <iostream>
#include <fstream>
using namespace std;
int main( void )
{
 ifstream inFile;   // create a file reader
 inFile.open("vowels.txt"); // open the vowels.txt file
 char ch = '\0';    // holds each letter for testing
 int numz = 0;    // number of vowels counted
 
 // get the string from the file specified
 if (inFile.is_open())
 {
  while (inFile.read(&ch, 1))
  {
   cout << ch; // output the character
   switch (tolower(ch)) // test the character
   {
   case 'a': case 'e': case 'i': case 'o': case 'u': // what about sometimes Y too?
    numz++; // incriment the counter
    break; // stop the switch
   }
  }
  cout << endl; // close off the sentence
  inFile.close(); // we are done reading the file close the stream
 } else {
  printf("Failed Opening Stream\n"); // oops we couldnt open the stream
 }
 // show the user the number of vowels
 printf("The number of vowels: %d\n", numz);
 cin.get(); // wait for user input before closing
 return 0;
}

Thanks a lot, worked just fine..apprecaite it, u may wanna take a look at my other posts...

This question has already been answered. Start a new discussion instead.