#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(ch ar *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.e of())
{
infile.getline( buffer, 500);
//cout << buffer << endl;
vowels = count_vowels(bu ffer);
}
cout << "\n\nThis is what the function passed back: "
<< vowels << "\n\n";
}//end for else if (infile)
return 0;
unsigned int count_vowels(ch ar *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 1957
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(ch ar *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(ci n, 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.e of())
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(in file, buffer);
//cout << buffer << endl; vowels = count_vowels(bu ffer);
}
cout << "\n\nThis is what the function passed back: " << vowels << "\n\n";
}//end for else if (infile)
return 0;
unsigned int count_vowels(ch ar *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(ch ar *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.e of()) { infile.getline( buffer, 500); //cout << buffer << endl; vowels = count_vowels(bu ffer);
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(ch ar *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.e of()) { infile.getline( buffer, 500); //cout << buffer << endl; vowels = count_vowels(bu ffer);
}
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(bu ffer);
}
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
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
|
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
distinguish between lowercase and uppercase letters.
|
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 was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
|
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;
printf("enter first string");
scanf("%s",s1);
printf("enter second string");
| |
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 delimiting characters can be specified
on the command line. The default delimiting characters built into the
program are space, newline, tab, carriage return, form feed, vertical
tab, comma and null. If a 'u' or 'U' is specified as the last command
line...
|
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" class="text"><font
|
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;
void reverse(char *str, int count = 0);
|
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 Count base on spaces
''' </summary>
''' <param name="textToParse"></param>
''' <returns>Integer = Word Count</returns>
''' <remarks></remarks>
Private Function CountWords(ByVal textToParse As String) As Integer
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |