#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 4 1931
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
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
John Ratliff wrote: Why does a function called count_vowels really count any letter? A minor point.
A major point I would say.
john 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Vinokur |
last post by:
Is there any tool to count C-program lines except comments?
Thanks,
=====================================
Alex Vinokur
mailto:alexvn@connect.to
http://mathforum.org/library/view/10978.html...
|
by: paul |
last post by:
colud someone please post up a c program to do this
Write a program that counts the number of times the first three
letters of the alphabet (a, b, c, and A, B, C) occur in a file. Do not...
|
by: radnus2004 |
last post by:
main()
{
static int a = {1,2,3};
printf("&a-&a=%u\n",&a-&a);
}
If you compile the above program and run it prints 1.
Why?
I printed &a and &a separately and the difference
|
by: |
last post by:
I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I...
|
by: sravishnu |
last post by:
Hello,
I have written a program to concatanae two strings, and should be
returned to the main program. Iam enclosing the code,
please give me ur critics. Thanks,
main()
{
char s1,s2;...
|
by: santosh |
last post by:
Hello all,
I've put together a small program to count the number of characters and
'words' in a text file. The minimum length of a word, (in terms of no.
of characters), as well as word...
|
by: roN |
last post by:
Hey,
I got following:
<form name="Formular" method="post" onSubmit="return chkFormular()"
action="mail.php" enctype="text/plain">
....
....
....
<td align="right" valign="top"...
|
by: samimmu |
last post by:
this is my code i made this code because to reverse the words and get the number or frequent characters.this my code below.
#include <iostream>
#include <cstring>
using namespace std;
...
|
by: gflor16 |
last post by:
Problem: I have this code to run a word counter. But I have a problem when I hit the enter key, it doesn't give me any output of how many chars or words.
''' <summary>
''' Returns Word...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |