User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,504 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,666 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2585 | Replies: 12 | Solved
Reply
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

C++ vowel counter from inFile

  #1  
Sep 27th, 2007
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:

  1. #include <cstring>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <fstream>
  6. #include <string>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14. ifstream inFile;
  15. inFile.open ("vowels.txt");
  16.  
  17. string sentence;//sentence from inFile
  18.  
  19.  
  20. char ch;
  21. int i;
  22. int length = 0;
  23.  
  24. while (getline(inFile,sentence))
  25. {
  26. cout<<sentence<<endl;
  27. }
  28.  
  29. int numz = sentence.length();//number of vowels
  30.  
  31. for (int i = 0; i < numz; ++i)
  32. {
  33. ch = toupper (sentence [i]);
  34. if (isalpha (ch) && (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'))
  35. {
  36. numz++;
  37. }
  38. }
  39.  
  40.  
  41. cout<<"The number of vowels: "<<numz<<endl;
  42.  
  43. inFile.close();
  44. return 0;
  45. }
Last edited by Ancient Dragon : Sep 27th, 2007 at 11:48 am. Reason: add line numbers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ vowel counter from inFile

  #2  
Sep 27th, 2007
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;
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: C++ vowel counter from inFile

  #3  
Sep 27th, 2007
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".
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: C++ vowel counter from inFile

  #4  
Sep 27th, 2007
>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();
I'm here to prove you wrong.
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Troubleshooting Re: C++ vowel counter from inFile

  #5  
Sep 27th, 2007
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..
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ vowel counter from inFile

  #6  
Sep 27th, 2007
what error(s) do you get?

Move declaration of numz outside that while loop, probably just under the declaration of the other integers.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Troubleshooting Re: C++ vowel counter from inFile

  #7  
Sep 27th, 2007
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;
} 
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ vowel counter from inFile

  #8  
Sep 27th, 2007
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.
  1. switch ( sentence[i] )
  2. {
  3. case 'a': case 'e': case 'i': case 'o': case 'u':
  4. numz++;
  5. break;
  6. }
Note where the braces are placed. And you may want to get into the habit of putting a break in there.
Reply With Quote  
Join Date: Apr 2004
Location: Tracy
Posts: 744
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Rep Power: 7
Solved Threads: 32
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: C++ vowel counter from inFile

  #9  
Sep 27th, 2007
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

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.
Last edited by Killer_Typo : Sep 27th, 2007 at 3:14 pm.
!!!!! WARNING YOUR COMPUTER MAY BE INFECTED WITH SPYWARE!!!! PAY AN OVER PRICED AMMOUNT TO HAVE SOMTHING FIXED WE PLACED THERE IN THE FIRST PLACE!!!!!!!!!

sound familiar, know how to block yourself and keep yourself clean.
_____________________
http://www.lavasoftusa.com/ -->adaware
http://www.safer-networking.org/en/index.html -->spybot S&D
http://www.javacoolsoftware.com/spywareblaster.html -->spywareblaster
http://www.javacoolsoftware.com/spywareguard.html -->spywareguard
_____________________
and dont forget to spread the reputation to those that deserve!
Reply With Quote  
Join Date: Jun 2007
Location: Home
Posts: 2,451
Reputation: zandiago is on a distinguished road 
Rep Power: 6
Solved Threads: 24
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: C++ vowel counter from inFile

  #10  
Sep 27th, 2007
Thanks much for all the feedback...I modified the program and it calcualtes the number correctly.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC