Why does program only count 3 chars

  • Thread starter grocery_stocker
  • Start date
G

grocery_stocker

#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
 
J

John Ratliff

grocery_stocker said:
#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-lite/input-output.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 said:
{
c++;
}
}
return c;
}

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

I am a filE.

--John Ratliff
 
I

Ian

grocery_stocker said:
#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
 
J

John Harrison

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top