I'm new to c++ and need help with a program

I'm trying to read in a file, find the total words and each character in the words. ANY help would be appreciated
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
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

/**********************************************/
//Name:        checkFile
//Description: Checks to see if file is valid
//Parameters:  countWords, name
//Return:      none
/**********************************************/
void checkFile(string &name, ifstream &countWords);

/**********************************************/
//Name:        addWords
//Description: Opens file and counts the words
//Parameters:  words, size, twords, countWords
//Return:      none
/**********************************************/
void addWords(int words[], int &size, int &twords, ifstream &countWords);

/**********************************************/
//Name:        getTotal
//Description: Tallys total
//Parameters:  words, size
//Return:      sum
/**********************************************/
int getTotal(int words[], int size);

/**********************************************/
//Name:        displayOutput
//Description: displays output
//Parameters:  sum, words, size, name
//             countWords
//Return:      none
/**********************************************/
void displayOutput(int words[], int &twords, int &sum, string &name, ifstream &countWords);


int main()
 {
   int words[10];
   int size, sum, twords;
   string name;
   ifstream countWords;

   checkFile(name, countWords);
   while(!countWords.eof())
     {
   addWords(words, size, twords, countWords);
   sum = getTotal(words, size);
     }
   displayOutput(words, twords, sum, name, countWords);

   return 0;
 }


void checkFile(string &name, ifstream &countWords)
 {
   cout << "Enter File Name: ";
   cin  >> name;
   countWords.open(name.c_str());
    if(countWords.fail())
     {
      cout << "File Failed to Open"<<endl;
      exit(0);
     }
 }

void addWords(int words[], int &size, int &twords, ifstream &countWords)
 {
  string letters;
  twords = 0;
  while(countWords >> letters)
    if(letters.size() >= 10)
     {
      words[9]++;
      twords++;
     }

    else
     {
      words[letters.size()-1]++;
      twords++;
     }
 }


int getTotal(int words[], int size)
 {
  int sum = 0;
  for(int r = 0; r < 10; r++)
   {
    sum += words[r];
   }
    return sum;
 }

void displayOutput(int words[], int &twords, int &sum, string &name, ifstream &countWords)
 {
  int size = 10;
  cout << "File: "<< name << "\t \t" << "Words: " << twords <<endl;
  cout << "\t \t" << "Analysis of Words" <<endl;
  cout << "Size " << "\t     " << "1   " << "2   " << "3   " << "4   " << "5   " << "6  " << "7   " << "8   " << "9   " << "1\
0+   " <<endl;
  cout << "#Words";
  for (int j = 0; j < size; j++)
   {
    cout << words[j] << "\t ";
   }
  cout <<endl;
  cout <<"Sum : " << sum <<endl;
 }

thats the whole code. once again I think its something to do with the .szie() but I don't know how to fix it 
Last edited on
Two remarks;

I'm not sure how this program works at all as it has a call to checkFile() with an undeclared variable "countWords" which caused my compiler to object. Have you actually compiled and run this code as I can't get it to work?

I'm a bit surprised there's no easy to find instructions on this but...

To display code properly you need to include it inside the code labels. I can't actualy show you these here because the system doesn't display labels -- it only shows their effects. That said, if you substitute square brackets ("[" and "]") for angled brackets ("<" and ">") in the following (and if you imagine that "-" is a space;

<code>
----int main ()
----{
------cout << "hello world";
----}
</code>

you get;

1
2
3
4
     int main ()
     {
        cout << "hello world";
     }


Which makes it far easier to read - and far easier to help you with your problem. Your code, by the way, is all left formated because the forum strips out leading (and multiple) spaces -- so the next line actually has about twenty spaces before "hi" but the forum doesn't display them;

hi

Sorry I couldn't do more, but as I mentioned, I couldn't even get your code to compile and run.

Last edited on
yes it does compile but for a 8 word sentence, it spilts out:

Size (the size of the word) 1 2 3 4 5 6 7 8 9 10+
result: 0 0 0 0 -123874993 6728839 -5839992 0 0 0
Hmm...something very strange is happening.

Again, it SHOULDN'T run - you should get an undeclared variable error in the line;

 
    checkFile(name, countWords);


because countWords has not been declared.

What's more, you have the following functions declared at the start of your code;

1
2
3
    int getTotal(int words[], int size);
    void displayOutput(int words[], int &twords, int &sum, string &name,
        ifstream &countWords);


without a corresponding definition later on.

Did you miss something when you cut and paste your code into your post?

I suspect that what your compiler sees is different from what's in your post!
Topic archived. No new replies allowed.