By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

Why does program only count 3 chars

P: n/a
#include <iostream> //for cin and cout
#include <iomanip> // for setw()
#include <string> // for strlen() strcmp() strrev()
#include <fstream> //ifstream and ofstream: file (input & output)
#include <stdlib.h> //for system calls
using namespace std;

//functions will go here!!
unsigned int count_vowels(char *pointer);
int main(void)
{
//int count; //for function count spaces
int vowels; //for function count vowels
//int digit; //for counting digits
char buffer[500];
cout << "Enter a file name > ";
cin.get(buffer, 500); //wanting to get all even a space!

//open file!

ifstream infile(buffer);

if (!infile)
{
cout << "\n\nFile not found or corrupt; bailing
out!\n\n";
exit(0);
}
else if (infile)
{//start for else if (infile)

while(!infile.eof())
{
infile.getline(buffer, 500);
//cout << buffer << endl;
vowels = count_vowels(buffer);

}

cout << "\n\nThis is what the function passed back: "
<< vowels << "\n\n";
}//end for else if (infile)

return 0;

unsigned int count_vowels(char *p)
{
//really return int c=0; ++-ed sized
int c=0;
for (; *p != 0; p++)
{
if (*p >= 'a' && *p <= 'z' || *p >= 'A' && *p <= 'Z')
{
c++;
}
}
return c;
}

the file i am entering is 1.txt that houses these chars:

I am a filE.

bob

Nov 2 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
grocery_stocker wrote:
#include <iostream> //for cin and cout
#include <iomanip> // for setw()
#include <string> // for strlen() strcmp() strrev()
This should be cstring, not string. string.h = cstring, which has
std::strlen, std::strcmp, and std::strrev. string has std::string.
#include <fstream> //ifstream and ofstream: file (input & output)
#include <stdlib.h> //for system calls
This should be cstdlib.
using namespace std;
Bad practice to do this, but your call.

//functions will go here!!
unsigned int count_vowels(char *pointer);
int main(void)
{
//int count; //for function count spaces
int vowels; //for function count vowels
//int digit; //for counting digits
char buffer[500];
Don't do this in C++. Use a std::string.
std::string buffer;
cout << "Enter a file name > ";
cin.get(buffer, 500); //wanting to get all even a space!
You can replace this with
std::getline(cin, buffer);

//open file!

ifstream infile(buffer);

if (!infile)
{
cout << "\n\nFile not found or corrupt; bailing
out!\n\n";
exit(0);
}
else if (infile)
Why an 'else if' when you already exited in the original if? Not necessary.
{//start for else if (infile)

while(!infile.eof())
Potential problem; See the FAQ

http://www.parashift.com/c++-faq-lit....html#faq-15.5
{
infile.getline(buffer, 500);
Again, we replace with
std::getline(infile, buffer);
//cout << buffer << endl;
vowels = count_vowels(buffer);

}

cout << "\n\nThis is what the function passed back: "
<< vowels << "\n\n";
}//end for else if (infile)

return 0;

unsigned int count_vowels(char *p)
Should be a const char* p or better yet a const std::string&. You'll
have to rewrite a few things here if you use const std::string& though.
{
//really return int c=0; ++-ed sized
int c=0;
for (; *p != 0; p++)
This is a stylistic thing that bothers me. I would do

while (*p != 0) {
...
++p;
}

But there's nothing seriously wrong with it I suppose.
{
if (*p >= 'a' && *p <= 'z' || *p >= 'A' && *p <= 'Z')
Why does a function called count_vowels really count any letter? A minor
point.

I'm not sure of the order of operations here, and I don't plan to go
look it up, but you might want to parenthesize that better. i.e.

if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z'))
{
c++;
}
}
return c;
}

the file i am entering is 1.txt that houses these chars:

I am a filE.


--John Ratliff
Nov 2 '05 #2

P: n/a
Ian
grocery_stocker wrote:
#include <iostream> //for cin and cout
#include <iomanip> // for setw()
#include <string> // for strlen() strcmp() strrev()
#include <fstream> //ifstream and ofstream: file (input & output)
#include <stdlib.h> //for system calls
using namespace std;

//functions will go here!!
unsigned int count_vowels(char *pointer);
int main(void)
{
//int count; //for function count spaces
int vowels; //for function count vowels
//int digit; //for counting digits Initialise these!
char buffer[500];
cout << "Enter a file name > ";
cin.get(buffer, 500); //wanting to get all even a space!

//open file!

ifstream infile(buffer);

if (!infile)
{
cout << "\n\nFile not found or corrupt; bailing
out!\n\n";
exit(0);
}
else if (infile)
{//start for else if (infile) Why not just else? The comment is a waste of space.
while(!infile.eof())
{
infile.getline(buffer, 500);
//cout << buffer << endl;
vowels = count_vowels(buffer); Should be +=, otherwise you just get the result from the last line....
}

cout << "\n\nThis is what the function passed back: "
<< vowels << "\n\n";
}//end for else if (infile)

return 0;

unsigned int count_vowels(char *p)
{
//really return int c=0; ++-ed sized
int c=0;
for (; *p != 0; p++)
{
if (*p >= 'a' && *p <= 'z' || *p >= 'A' && *p <= 'Z')
{
c++; I'm sure there's some logic missing here... }
}
return c;
}

Ian
Nov 2 '05 #3

P: n/a
John Ratliff wrote:
Why does a function called count_vowels really count any letter? A minor
point.


A major point I would say.

john
Nov 2 '05 #4

P: n/a

while(!infile.eof())
{
infile.getline(buffer, 500);
//cout << buffer << endl;
vowels = count_vowels(buffer);

}

cout << "\n\nThis is what the function passed back: "
<< vowels << "\n\n";


The loop logic is wrong. Also the way you test for end of file is wrong.

int vowels = 0;
while (infile.getline(buffer, 500))
{
vowels += count_vowels(buffer);
}

john
Nov 2 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.