Hey guys,

So I need to write a program that will prompt the user to enter the name of a .txt file, then count the occurence of each letter. It should print out the results on the screen and ask the user to enter a .txt file name to store it. It should use a vector of length 26 to count the occurance of the letters, and treat upper and lower case as the same letter.

I really don't understand vectors so I have no idea what to do after this:

#include <iostream>
#include <string>
#include <cassert>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
	cout<<"Please enter the name of the .txt file you wish to open: ";
	string file;
	cin >> file;
	ifstream fin(file.data());
	assert(fin.is_open());
}

Recommended Answers

All 8 Replies

Ok thats a start. Now you need to create a vector of size 26.

This is how to do that :

vector<int> numberOfOccurance(26,0); //create a vector of size 26 with each element initialized to 0

After that you need to read in the data character by character.

One way to do that is this :

char character = 0;
ifstream iFile("test.txt");
while( iFile.get(character) ){
//do logic here
}

So the variable character inside the loop contains the 1 character inside
the file. This happens on each loop until the end of file is reached.
the "do logic part" is where you should do the frequency counter.
Hint : google tolower and toupper function.

do u have the full code for this program.

umm...no. I'm stuck. I have no idea how to do this.

I wrote this snippet some time ago, it's the bare minimum and should be fairly easy to understand. [link]

Thanks but I need to use vectors to do it. Can anyone help?

I haven't seen much effort from you in this thread. To change my code to work with vectors is very simple, you could literally change two lines of my code and it would work with vectors instead.

Try it yourself.

I would try but I don't understand vectors at all. I'm taking an "Introduction to Computing" class and this is the last assignment. I have done 14 and asked you guys for help on about 4 of them. This is the first and last programming class I have ever and ever will take. I didn't know it was programming when I registered or else I wouldn't have taken it. I don't even need it for my degree, it's an elective. I would really like to pass this course, so could somebody please help me!?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.