473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

C++ - output the number of occurrences of members in an array.

I am very new to C++. In my assignment I have to read in a phrase from a file and send out to another file the number of words in the phrase and a list of letters in the phrase and how many times they occur. Example - If I input "I say hi" my output would look something like this:

3 words
1 a
1 h
2 i
1 s
1 y

not necessarily in that order. I am able to read in the phrase with no problems but not sure where to go from here. This is what I've got so far. Please help!


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{

ifstream fin;
ofstream fout;

fin.open("l:/A4Q1.txt");
fout.open("l:/A4Q1out.txt");

char a[100];
int i;

fin.getline(a,100);

i=0;
while(a[i] != '\0')
{
cout << a[i];
i++;
}
return 0;
}
Mar 1 '07 #1
1 2402
sicarie
4,677 Expert Mod 4TB
I am very new to C++. In my assignment I have to read in a phrase from a file and send out to another file the number of words in the phrase and a list of letters in the phrase and how many times they occur. Example - If I input "I say hi" my output would look something like this:

3 words
1 a
1 h
2 i
1 s
1 y

not necessarily in that order. I am able to read in the phrase with no problems but not sure where to go from here. This is what I've got so far. Please help!


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{

ifstream fin;
ofstream fout;

fin.open("l:/A4Q1.txt");
fout.open("l:/A4Q1out.txt");

char a[100];
int i;

fin.getline(a,100);

i=0;
while(a[i] != '\0')
{
cout << a[i];
i++;
}
return 0;
}
I'm not entirely sure of tghe syntax you have, but the way you have it, you increment I no matter ehat, there is no check or qualification for the occurrence of qa certain character.

Did you want the number of chars, or the occurrence of each?
Mar 1 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: francescomoi | last post by:
Hi. I'm using 'strpos', but wonder whether this function exists: find_string($haystack, $needle) which returns an array with all the numeric positions of the occurrences of needle in the...
8
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in...
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
3
by: rsteph | last post by:
I have a script that shows the time and date. It's been working on my site for quite a while now. Suddenly it stops showing up, after getting my drop down menu to work. If I put text between the...
2
by: Jay | last post by:
I have a few enums, a shortened example of one is: enum ParamTest {Err, Start, Block, Hold} Is there a programmatic way to find the number of "members" (probably the wrong C# term) of the Enum...
0
by: hennas | last post by:
Basically i want to design a membership Name and Telephone List form using the following command buttons. Edit Add New; Update; Delete; Cancel; Save; Clear, and Exit
1
by: LittlBUGer | last post by:
Hello. First of all I'm programming in VB.NET/ASP.NET doing a page for a website. Now, to my question.... I have a simple array of integer numbers (15 characters in length) which can hold up to...
5
by: AB3004 | last post by:
Hey there, Just wanted to say Hi. - I'm new on here & have drifted here as I'm having a minor problem I was hoping someone could assist ...? (for some reason I couldn't post this directly into the...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.