#include <iostream>
#include <fstream>
using namespace std;
// Main Program
int main( )
{
using namespace std;
ifstream isfile;
isfile.open("G:\\vowels.txt");
if (isfile.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
// Variable Declarations
int vowels=1;
int consonants=1;
char character;
int spaces=1;
char ;
isfile.get(character);
tolower(character);
switch (character)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
case 'b':
case 'c':
case 'd':
case 'f':
case 'g':
case 'h':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
consonants++;
break;
case ' ':
spaces++;
break;
vowels= vowels++;
consonants=consonants++;
spaces = spaces++;
}
cout << " The input file has the folowing counts:" << endl;
cout << " Vowels" << vowels<<endl;
cout << " Consonants" << consonants << endl;
cout << " Spaces" << spaces << endl;
cout << "End Program.";
isfile.close();
return(0);
}
1 Replies - 155 Views - Last Post: 19 November 2011 - 06:11 AM
#1
how do i fix this to count vowels, spaces or consonants
Posted 18 November 2011 - 09:26 PM
this code is supposed to count the vowels consonants and spaces in a paragraph but it does not do that. my question is how do i fix it?
Replies To: how do i fix this to count vowels, spaces or consonants
#2
Re: how do i fix this to count vowels, spaces or consonants
Posted 19 November 2011 - 06:11 AM
// Variable Declarations int vowels=1; int consonants=1; char character; int spaces=1;
Why initializing to 1? Wouldn't 0 make more sense?
char ;
Huh???
isfile.get(character);
Only gets the first character in the file. Look into a loop.
tolower(character);
That does nothing. Try merging that line and the line following:
switch(tolower(character))
vowels= vowels++; consonants=consonants++; spaces = spaces++;
What is the point of these, when you increment the variables in the switch cases?
Also, instead of have a case with all the consonants, which clutters up the code, why not remove that and just include consonants as the default case, i.e., when a character is neither a vowel nor a space?
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|