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());
}

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.

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

Edited 7 Years Ago by cole davidson: n/a

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!?

This article has been dead for over six months. Start a new discussion instead.