0

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

3
  • In the initialize function, print the value of n. Commented Apr 9, 2016 at 4:19
  • "Stops"? Hangs? Crashes? Just exits? Commented Apr 9, 2016 at 5:17
  • It prints Character Count then exits Commented Apr 10, 2016 at 2:21

2 Answers 2

2

You probably want
a)

void initialize(int *array, int n, int value)
{
  int i;    

  for (i = 0; i < n; ++i)
  {
     // no: value += array[i]; but:
     array[i] = value;
  }
}

see also std::fill

and b) move the return 0; off the while-loop-body

    cout << " Character " << " Count " << endl;
    cout << setfill('-') << setw(17) << '-' << setfill(' ') << endl;
  }
  return 0;
}

edit: regarding a)
You can use

std::fill(std::begin(digit), std::end(digit), 0);
std::fill(std::begin(alpha), std::end(alpha), 0);

instead of your initialize() function or (given the context) just

int punctuation, whitespace, digit[10]={0}, alpha[26]={0}; 
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow, now I feel really stupid, because I didn't even notice that return 0; was inside the while loop, and the way you corrected my function is the way I had it originally. Thanks, VolkerK.
0

If you are developing in C++, why not use vectors instead? (Working with arrays* is very C style).

vector<int> array(n, value);

n = number of integers. value = value to place in each array cell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.