help with arrays and functions

This program keeps giving me the same error while compiling

line 21:
invalid conversion from int to int
initializing argument 1 of void setCounts(int)
line 24:
invalid conversion from int to int
initializing argument 1 of void readAndCount(int, int)
line 27:
invalid conversion from int to int
initializing argument 2 of void displayCounts(int, int)
line39:
declaration of char_count as array references
expected ) before , token
expected unqualified-id before int



//This program reads text from the user and returns
//a count for each letter used in addition to the
//number of words used in the text.

#include <iostream>
using namespace std;

void setCounts(int char_count);
void readAndCount(int char_count, int word_count);
void displayCounts(int word_count, int char_count);

int main()
{
//declarations
int word_count = 0;
int char_count[26];

// intialize character counts to 0
setCounts( char_count);

// read and count characters and words
readAndCount ( char_count, word_count);

// display counts
displayCounts ( word_count, char_count);

return 0;
}

void setCounts(int char_count[])
{
int i;
for(i = 0; i < 26; i++)
char_count[i] = 0;
}

void readAndCount (int& char_count[], int& word_count)
{
char ch;
int in_word = false;
char low_case;
while('\n' !=(ch = cin.get()))
{
if(' ' == ch || '\n' == ch || '\t' == ch)
in_word = false;
else if (in_word == false)
{
in_word = true;
word_count++;
}
low_case = tolower( ch);
char_count[ int(low_case) - int('a') ]++;
}
}

void displayCounts (int word_count, int char_count[])
{

int i;
cout<< word_count << " words" << endl;
for(i = 0; i < 25; i++)
if(char_count[i] != 0)
cout<< char_count[i] << " "<< char(i + 'a') << endl;
}




if anyone could help me out, it'd be greatly appreciated. I just started toying with C++ a couple months ago and i think my big problem are arrays and functions.
Last edited on
closed account (D80DSL3A)
The function prototypes don't match the definitions.
The prototypes (given above main() ) should be:
1
2
3
void setCounts(int char_count[]);
void readAndCount(int char_count[], int& word_count);
void displayCounts(int word_count, int char_count[]);

In the definition of readAndCount() you don't need to pass the array by reference. When you pass an array the changes made in the function will apply to it. Passing word_count by reference is needed though.
Thank you so much! It works perfect!
Topic archived. No new replies allowed.