please help me finish.

i am so close to finishing this program that will count the occurrences of letter in a word but i cannot get the calculations function to work. i believe that if i use it as a parameter into the info function it would display the results but i have yet to accomplish that. can anyone please help me get this program to run?

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
  #include<iostream.h>
#include<iomanip.h>
int main()
{
    char Word;
    int loop = 0;
    char a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;
    char const Letters = 52;
    int const Counter = 52;
    char NumLetters[Letters] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    int LetterCount[Counter]= {0};
    char entireword[15];
    int spot = 0;
    char fullword[spot];
    char Info(int LetterCount[],char NumLetters[], char entireword[], char (*Calculations)());
    char userinput(char entireword[]);
    char Calculations(char entireword [], int LetterCount [], char NumLetters [],int loop);
    cout<<endl;

userinput(entireword);
Calculations(entireword,LetterCount,NumLetters,loop);
Info(LetterCount,NumLetters,entireword,Calculations);
system("pause");
return 0;
}
char Calculations(char entireword [], int LetterCount [], char NumLetters [], int loop)
{
  loop = 0;
  while (loop < strlen(entireword))   
    {  
       
      for (int Letters =0, Counter = 0, i = 0; Letters < 52; Letters++, Counter++,i++)
      {
      
          if(NumLetters[Letters] == entireword[i])
            LetterCount[Counter]++;
       }  
      
      loop++;
    }   
    return 0; 
}
char userinput(char entireword[]) 
{

    cout<<setw(26)<<"LETTER COUNTS"<<endl;
    cout<<"******************************************"<<endl;
    cout<<"Enter the word to be letter counted ";
    cin.getline(entireword, 15);
    cout << endl;
    strlen(entireword);
}
char Info(int LetterCount[],char NumLetters[],char entireword[],char (*Calculations)())
{
   int loop = 0;
   Calculations(entireword,LetterCount,NumLetters,loop);
   for (int Counter = 0,Letters = 0 ; Counter < 52; Counter++, Letters++)
     { 
       if (LetterCount[Counter]>0)
       { 
        cout<<"There are "<<LetterCount[Counter]<<" "<<NumLetters[Letters]<<"'s"<<endl;
       }
     }
   cout<<"There are "<<strlen(entireword)<<" Letters in the word '";
   for(int i = 0; i < strlen(entireword); i++)
   {
        cout << entireword[i];
   }
   cout<<"'";
   cout<<endl;
} 

Last edited on
First of all, it is recommended that you use the following:

1.) <iostream> instead of <iostream.h>

2.) <iomanip> instead of <iomanip.h> (if you even need to use it)

3.) std::strings instead of character arrays

Anyways, you some strange variables. char Word is just a single character, and there are only a couple one-letter words I can think of.
Later on you have char entireword[15]... What's the difference supposed to be?
You then have 52 variables named after each letter in the alphabet in upper and lower case. You really don't need those... Additionally, what's the difference between LetterCount and NumLetters??? If you like, I can show you some examples of cleaning up your code...
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
#include <iostream>
#include <string>
#include <utility>
#include <cctype>
using namespace std;

constexpr int v_size = 52;
typedef pair<char, int> pci;
pci letter_count[v_size] = {{' ', 0}};

void count_occurences(const string& word) {
	pci *ptr;
	for (auto ch : word)
	{
		if (isalpha(ch)) {
			ptr = &letter_count[char(ch | 32) == ch ? ch - 71 : ch - 65];
			if (!isalpha(ptr->first))
				ptr->first = ch;
			ptr->second++;
		}
	}
}

int main() {
	count_occurences("popsicle");
	for (int t = 0; t < v_size; ++t)
		if (letter_count[t].second)
			cout << "Found " << letter_count[t].second
			     << " " << letter_count[t].first << "(s)\n";
	return 0;
}
Last edited on
the program runs fine if i remove the function calculations from the parameter. my problem is getting the functioncalculations into the function info to calculate the letters in a word.
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
#include<iostream.h>
#include<iomanip.h>
int main()
{
    char Word;
    int loop = 0;
    char a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;
    char const Letters = 52;
    int const Counter = 52;
    char NumLetters[Letters] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    int LetterCount[Counter]= {0};
    char entireword[15];
    int spot = 0;
    char fullword[spot];
    char Info(int LetterCount[],char NumLetters[], char entireword[]);
    char userinput(char entireword[]);
    char Calculations(char entireword [], int LetterCount [], char NumLetters [],int loop);
    cout<<endl;

userinput(entireword);
Calculations(entireword,LetterCount,NumLetters,loop);
Info(LetterCount,NumLetters,entireword);
system("pause");
return 0;
}
char Calculations(char entireword [], int LetterCount [], char NumLetters [], int loop)
{
  loop = 0;
  while (loop < strlen(entireword))   
    {  
       
      for (int Letters =0, Counter = 0, i = 0; Letters < 52; Letters++, Counter++,i++)
      {
      
          if(NumLetters[Letters] == entireword[i])
            LetterCount[Counter]++;
       }  
      
      loop++;
    }   
    return 0; 
}
char userinput(char entireword[]) 
{

    cout<<setw(26)<<"LETTER COUNTS"<<endl;
    cout<<"******************************************"<<endl;
    cout<<"Enter the word to be letter counted ";
    cin.getline(entireword, 15);
    cout << endl;
    strlen(entireword);
}
char Info(int LetterCount[],char NumLetters[],char entireword[])
{
   int loop = 0;
   Calculations(entireword,LetterCount,NumLetters,loop);
   for (int Counter = 0,Letters = 0 ; Counter < 52; Counter++, Letters++)
     { 
       if (LetterCount[Counter]>0)
       { 
        cout<<"There are "<<LetterCount[Counter]<<" "<<NumLetters[Letters]<<"'s"<<endl;
       }
     }
   cout<<"There are "<<strlen(entireword)<<" Letters in the word '";
   for(int i = 0; i < strlen(entireword); i++)
   {
        cout << entireword[i];
   }
   cout<<"'";
   cout<<endl;
} 
Topic archived. No new replies allowed.