I'm trying to initialize arrays by passing them to an initialization function as a pointer. My program compiles with no errors, but when I run it; it prints out Character Count and stops.
This is what I have so far:
#include<iostream>
#include<cctype>
#include<string>
#include<iomanip>
using namespace std;
void initialize(int *array, int n, int value);
int main()
{
char ch;
int punctuation, whitespace, digit[10], alpha[26];
punctuation = 0, whitespace = 0;
initialize(digit, sizeof(digit)/sizeof(int), 0);
initialize(alpha, sizeof(alpha)/sizeof(int), 0);
while(cin.get(ch))
{
if(ispunct(ch))
++punctuation;
else if(isspace(ch))
++whitespace;
else if(isdigit(ch))
++digit[ch - '0'];
else if(isalpha(ch))
{
ch = toupper(ch);
++alpha[ch - 'A'];
}
if(whitespace > 0)
{
cout << "Whitespace = " << whitespace << endl;
}
if(punctuation > 0)
{
cout << "Punctuation = " << punctuation << endl;
}
cout << setfill('-') << setw(17) << '-' << setfill(' ') << endl;
cout << " Character " << " Count " << endl;
cout << setfill('-') << setw(17) << '-' << setfill(' ') << endl;
return 0;
}
}
void initialize(int *array, int n, int value)
{
int i;
for (i = 0; i < n; ++i)
{
value += array[i];
}
}
I'm not sure that I am doing anything wrong here. Although, I am a bit confused as to how pointers work after they have been passed to another function. Can someone explain?
Thank you