I'm writing a simple program that reads a text file and counts the vowels in it and outputs how many are included.

With the code I have I keep getting the wrong number of vowels outputted. Can anyone see where I made a mistake, I can't figure it out.

my example file has : aeiou AEIOU
but when the program runs it says there's 11 vowels isntead of 10.

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>


using namespace std;

int main()
{
char ValueRead;
int counter=0;

//get name of input file

cout << "Enter the name of the input file: ";
string FileName;
getline(cin, FileName);


//check for success of the input file

ifstream fin;
fin.open(FileName.data());
assert (fin.is_open());


//while not end of file, read in chars and check if theyre a vowel,
//increment counter

while (!fin.eof())
{

fin >> ValueRead;

        if (ValueRead = 'A' || 'a' || 'E' || 'e' || 'I' || 'i' || 'O' ||
'o'|| 'U' || 'u')

        {
             counter++;
        }

}

cout << "There are: " << counter << "  vowels in the file!"<< endl;

fin.close();

return 0;
}

I'm writing a simple program that reads a text file and counts the vowels in it and outputs how many are included.

With the code I have I keep getting the wrong number of vowels outputted. Can anyone see where I made a mistake, I can't figure it out.

my example file has : aeiou AEIOU
but when the program runs it says there's 11 vowels isntead of 10.

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>


using namespace std;

int main()
{
char ValueRead;
int counter=0;

//get name of input file

cout << "Enter the name of the input file: ";
string FileName;
getline(cin, FileName);


//check for success of the input file

ifstream fin;
fin.open(FileName.data());
assert (fin.is_open());


//while not end of file, read in chars and check if theyre a vowel,
//increment counter

while (!fin.eof())
{

fin >> ValueRead;

        if (ValueRead = 'A' || 'a' || 'E' || 'e' || 'I' || 'i' || 'O' ||
'o'|| 'U' || 'u')

        {
             counter++;
        }

}

cout << "There are: " << counter << "  vowels in the file!"<< endl;

fin.close();

return 0;
}

Guy,the marked line tell a truth that every input symbol will result a true outcome.
Because,such as if(input == 1 || 2),it executes like this:
input == 1 : it depends
2 is true for ever
? || true : is true forever
so whatever you input,the judgement is always true

so the last character in the string,'\0' ,is judged as a vowel,so you get 11,not 10

Guy,the marked line tell a truth that every input symbol will result a true outcome.
Because,such as if(input == 1 || 2),it executes like this:
input == 1 : it depends
2 is true for ever
? || true : is true forever
so whatever you input,the judgement is always true

so the last character in the string,'\0' ,is judged as a vowel,so you get 11,not 10

I get what you're saying with the "or" logic, but is there a way I can omit the NULL character from not being counted as a viwel?

Maybe,the correct logic,the right result

eh,i made a mistake,not '\0', maybe '\n'
check your file,is there a '\n' in it?

hmm there is a \n in the text file...but i don't put it there.

im using cygwin and use pico test_file.txt

i put in aeiou AEIOU in the first line...but then i press down arrow it goes to new line...

it seems to put in a newline by itself everytime i access the file!!!!

you can print every character,the 11th should be '\n'.

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>


using namespace std;

int main()
{
char ValueRead;
int counter=0;
char ch= '\0';


//get name of input file

cout << "Enter the name of the input file: ";
string FileName;
getline(cin, FileName);


//check for success of the input file

ifstream fin;
fin.open(FileName.data());
assert (fin.is_open());


//while not end of file, read in chars and check if theyre a vowel,
//increment counter

while (!fin.eof())
{

fin >> ValueRead;

 if (ValueRead ==  'A' || 'a')

        counter++;

 else if (ValueRead == 'E' || 'e')

        counter++;

 else if (ValueRead == 'I' || 'i')

        counter++;

 else if (ValueRead == 'O' || 'o')

        counter++;
 else if (ValueRead == 'U' || 'u')

        counter++;

}

cout << "There are: " << counter << "  vowels in the file!"<< endl;

fin.close();

return 0;
}

i modified the code...but still not getting the right number of vowels...i creahed char ch='\0' to hold the newling char so it doesn't print it out..but still doesn't work :(

The if-statements there are fundamentally plain wrong.
If you write ..

if (ValueRead ==  'A' || 'a')

that evaluates to

if (ValueRead ==  'A' || true)

which is always true -> counter is incremented.

So, you want to write the if-statements like ..

if (ValueRead ==  'A' || ValueRead == 'a')
  counter ++;
else if ...

Furthermore, Dave Sinkula ^^^ pointed out that it is generally not a good idea to use .eof() in a loop control and gave you a good alternative. Maybe pay attention to his suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.