i m having issue can some one help?

General Description: You will write a program that reads a text file, counts the number of words in the file, and the number of occurrences of each character. It will print to a file the number of words, and the number of occurrences of each character, as well as the percentage of that particular character as compared to all the characters. The list should be in descending order of occurrence.

Details:
1. The body of the main function will only have variable declarations and function calls. (and the ‘return 0’ statement)
2. There can be NO global variables, all information must be passed in and out of the functions. (There can be global constants, if you want)
3. The characters to be counted are:
a. Letters: uppercase and lowercase will be counted as the same character.
b. Digits 0 – 9.
c. Punctuation: ! ? , . : ; ‘ “
d. Symbols: @#$%&*()+=/-<>
e. any other character does not need to be counted
4. You must use an array to store the number of each character.
5. You must have these functions in your code: (you may have others but not necessary)
a. getFileName – asks the user for the name of the input file. You must pass the file stream as a reference parameter. If the input file does not exist, you should print to the screen an appropriate message, and exit the program.
i. To exit a program early, you can use the function exit(0), but you must include the file cstdlib in your code.

b. countWords – counts the number of words in the file. Words are anything separated by whitespace. The file identifier must be passed in to the function. Should also print a message with the number of words in the file.

c. countChars – counts every occurrence of each character (see above) in the file. The information must be stored in an array, which must be passed in as a parameter. The file identifier must also be passed in as a reference parameter. Should also calculate the percentage for each character.

d. sortChars – sorts the array, in descending order, by the percentage.
You must use either a selection sort, or an insertion sort.

e. printCount – Prints each character, the number of each character, and the percentage of each character. The percentage should be in the form “25%”. The information should come from the array, which must be passed in as a reference parameter, and the info must be sorted

6. The program should work for any text file. I will run it on a text file for which I know the results. You may assume the text file is not an empty file
7. The output file should be named lastnameProj3Results.txt

#include <iostream>
#include <fstream>

using namespace std;

#define MAX_CHARS 256

string getFileName(ifstream & infile)
{
   string filename = " " ;
   cout << "Enter filename :" << endl;
   cin >> filename;
   //getline(cin, filename);
   infile.open(filename.c_str(), ifstream::in);
   if(infile.fail())
{
       cerr << "\tError : Filename : " << filename << " not found." << endl;
       filename.clear();
       return filename;
   }
   infile.close();
   return filename;
}
int countWords (string filename, ifstream & infile)
{
   infile.open(filename.c_str(), ifstream::in);
   infile.seekg (0);
   int wordCount = 0;
  
   string word;
   while(infile >> word)
{
       ++wordCount;
   }
   cout << "Word Count for file : " << wordCount << endl;
   infile.close();
   return wordCount;
}

void countChars (string filename, ifstream & infile, int charCount[], int & totalCount)
{
   infile.open(filename.c_str(), ifstream::in);
   infile.seekg (0);
   while(infile.good())
{
       char ch = infile.get();
       ++charCount[(unsigned char) ch];
   }
   totalCount = 0;
   for(int i = 0; i < MAX_CHARS; ++i)
{
       totalCount += charCount[i];
   }
   cout << "Character Count for file : " << totalCount << endl;
   infile.close();
}
void sortChars(int charCount[], int charPos[])
{
   for(int i = 0; i < MAX_CHARS; ++i)
{
       charPos[i] = i;
   }
   for(int i = 0; i < MAX_CHARS; ++i)
{
       //int minValue = charCount[i];
       int minPos = i;
       for(int j = i; j < MAX_CHARS; ++j)
{
           if(charCount[j] < charCount[minPos])
               minPos = j;
       }
       int temp = charCount[i];
       charCount[i] = charCount[minPos];
       charCount[minPos] = temp;
      
       temp = charPos[i];
       charPos[i] = charPos[minPos];
       charPos[minPos] = temp;
   }
}
void printCount(int charCount[], int charPos[], int & totalCount)
{
   for(int i = 0; i < MAX_CHARS; ++i)
{
       cout << "Character : " << (char)charPos[i] << "\tCount : " << charCount[i] << "\tPercentage : " << ((double)charCount[i]) / totalCount * 100 << "%" << endl;
   }
}
int main()
{
   ifstream infile;
   int charCount[MAX_CHARS] = {0};
   int charPos[MAX_CHARS] = {0};
   int totalCount = 0;
  
   string filename = getFileName(infile);
   if(!filename.empty())
{
       countWords(filename, infile);
       countChars(filename, infile, charCount, totalCount);
       sortChars(charCount, charPos);
       printCount(charCount, charPos, totalCount);
   }
  
  
   return 0;
}



Last edited on
Instead of throwing your entire project on our faces. How about you ask a question? Ask a specific question, help us help you. What do you need help with? What isnt working? Are you getting errors?

Also please edit your post and use code tags - http://www.cplusplus.com/articles/jEywvCM9/
I will do this for you this time, but next time put your code in code tags.
http://www.cplusplus.com/forum/articles/16853/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>

using namespace std;

#define MAX_CHARS 256

string getFileName(ifstream & infile)
{
   string filename = " " ;
   cout << "Enter filename :" << endl;
   cin >> filename;
   //getline(cin, filename);
   infile.open(filename.c_str(), ifstream::in);
   if(infile.fail())
{
       cerr << "\tError : Filename : " << filename << " not found." << endl;
       filename.clear();
       return filename;
   }
   infile.close();
   return filename;
}
int countWords (string filename, ifstream & infile)
{
   infile.open(filename.c_str(), ifstream::in);
   infile.seekg (0);
   int wordCount = 0;
  
   string word;
   while(infile >> word)
{
       ++wordCount;
   }
   cout << "Word Count for file : " << wordCount << endl;
   infile.close();
   return wordCount;
}

void countChars (string filename, ifstream & infile, int charCount[], int & totalCount)
{
   infile.open(filename.c_str(), ifstream::in);
   infile.seekg (0);
   while(infile.good())
{
       char ch = infile.get();
       ++charCount[(unsigned char) ch];
   }
   totalCount = 0;
   for(int i = 0; i < MAX_CHARS; ++i)
{
       totalCount += charCount[i];
   }
   cout << "Character Count for file : " << totalCount << endl;
   infile.close();
}
void sortChars(int charCount[], int charPos[])
{
   for(int i = 0; i < MAX_CHARS; ++i)
{
       charPos[i] = i;
   }
   for(int i = 0; i < MAX_CHARS; ++i)
{
       //int minValue = charCount[i];
       int minPos = i;
       for(int j = i; j < MAX_CHARS; ++j)
{
           if(charCount[j] < charCount[minPos])
               minPos = j;
       }
       int temp = charCount[i];
       charCount[i] = charCount[minPos];
       charCount[minPos] = temp;
      
       temp = charPos[i];
       charPos[i] = charPos[minPos];
       charPos[minPos] = temp;
   }
}
void printCount(int charCount[], int charPos[], int & totalCount)
{
   for(int i = 0; i < MAX_CHARS; ++i)
{
       cout << "Character : " << (char)charPos[i] << "\tCount : " << charCount[i] << "\tPercentage : " << ((double)charCount[i]) / totalCount * 100 << "%" << endl;
   }
}
int main()
{
   ifstream infile;
   int charCount[MAX_CHARS] = {0};
   int charPos[MAX_CHARS] = {0};
   int totalCount = 0;
  
   string filename = getFileName(infile);
   if(!filename.empty())
{
       countWords(filename, infile);
       countChars(filename, infile, charCount, totalCount);
       sortChars(charCount, charPos);
       printCount(charCount, charPos, totalCount);
   }
  
  
   return 0;
}
Topic archived. No new replies allowed.