•
•
•
•
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
![]() |
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:
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:
cplusplus Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon : Sep 27th, 2007 at 11:48 am. Reason: add line numbers
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
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
>>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;
} >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:
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.
Below is my revised version of the code...No alternation was made, dunno why it's not compiling:
Thx for the assistance..
#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..
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
what error(s) do you get?
Move declaration of numz outside that while loop, probably just under the declaration of the other integers.
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
Note where the braces are placed. And you may want to get into the habit of putting a break in there.
- Calculating the length of a sentence before you've read the sentence.
- Evaluating the value of ch before you've given it a value.
c Syntax (Toggle Plain Text)
switch ( sentence[i] ) { case 'a': case 'e': case 'i': case 'o': case 'u': numz++; break; }
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.
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!
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!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- hello i have a problem (VB.NET)
- Program reading in lines from a file. (each line individually) Please Help (C++)
- Need Advice on for loops with vowels (Java)
- My logic must be ALL wrong , help on Average. (C)
- Binary File IO (C#)
- how do you set a variable to be a filename? (C)
- C++ Address Book (C++)
- Counter Strike issue (Windows Software)
- Page counter print accounting (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: An introduction and a few questions..
- Next Thread: Help with a function.



Linear Mode