Help making this program compile

This is supposed to be the game NIM. As far as I know the actual gameplay is sound, it just won't compile. I have attached both my program and the errors I'm getting. Any help would be greatly appreciated!

Errors:

subtraction.cpp: In function ‘void print_rods(int*, int)’:
subtraction.cpp:157: error: ‘percentage’ was not declared in this scope
subtraction.cpp: In function ‘void smallest(int*, int)’:
subtraction.cpp:216: error: invalid type argument of ‘unary *’
subtraction.cpp:216: error: invalid type argument of ‘unary *’
subtraction.cpp: In function ‘void largest(int, int)’:
subtraction.cpp:235: error: ‘array’ cannot be used as a function
subtraction.cpp:235: error: ‘array’ cannot be used as a function
subtraction.cpp:241: error: invalid types ‘int[int]’ for array subscript
subtraction.cpp: In function ‘void average(int*, int)’:
subtraction.cpp:250: error: ‘sum’ cannot be used as a function
subtraction.cpp: In function ‘void move(int*, int, int, int, int)’:
subtraction.cpp:293: error: ‘remove’ cannot be used as a function
subtraction.cpp: In function ‘int remove_read(int*, int)’:
subtraction.cpp:348: error: ‘rod_chosesn’ was not declared in this scope
subtraction.cpp:349: error: ‘remove’ was not declared in this scope


Program:

#include <iostream>
#include <iomanip>

using namespace std;


// FUNCTION PROTOTYPES GO HERE:
int num_rods( const int NUM_RODS_MAX);
void rod_size( int array[], const int array_size, const int NUM_OBJ_MAX);
void how_many( int array[], const int NUM_OBJ_MAX, int i);
void print_rods( int array[], const int array_size);
void print_pound( int array[]);
int sum_objects( int array[], const int array_size);
void percent( const int sum, int array[], int i);
void stats( int array[], int array_size);
void smallest( int array[], int array_size);
void largest( int array[], int array_size);
void average( int array[], int array_size);
bool rod_empty( int array[], int array_size);
void move( int array[], int array_size, int player, int rod_chosen, int remove);
int pick_rod( int player);
void validate_rod_num( int player, int rod_chosen, int array_size);
void validate_rod_obj( int array[], int player, int rod_chosen);
void remove_from_rod( int array[], int rod_chosen);
int remove_read( int array[], int rod_chosen);
void modify( int array[], int rod_chosen, int remove);
void winner( int player);
void player_switch( int& player);

int main()
{
// Define variables and constants here
const int NUM_RODS_MAX = 15; //maximum number of rods allowed
const int NUM_OBJ_MAX = 10; //maximum number of objects per rod allowed
int array_size = 0; //size of the array
int array[array_size];
int player = 1; //which player is playing
bool empty = false; //are all the rods empty
int rod_chosen; //rod being played
int remove; //number of objects being removed

array_size = num_rods( NUM_RODS_MAX);

rod_size( array, array_size, NUM_OBJ_MAX);

print_rods( array, array_size);

stats( array, array_size);

while (rod_empty( array, array_size))
{

move( array, array_size, player, rod_chosen, remove);

remove_from_rod( array, rod_chosen);

if (rod_empty( array, array_size))
{
winner( player);
}

else{

print_rods( array, array_size);

stats( array, array_size);

player_switch( player);

}

}

return 0;
}

// FUNCTION DEFINITIONS GO HERE:
int num_rods( const int MAX_NUM_RODS)
{
int rods = 0; //number of rods in the game
cout<< "How many rods are in this game? ";
cin>> rods;
while (rods < 1 || rods > MAX_NUM_RODS)
{
cout<< "Number of rods must be positive and less than or equal to 15" << endl;
cout<< "Enter number of rods again: ";
cin>> rods;
}
return (rods);
}


void rod_size( int array[], const int array_size, const int NUM_OBJ_MAX)
{
int i; //used to count in the loop
for (i = 0; i < array_size; i++)
{
how_many( array, NUM_OBJ_MAX, i);
}
}

void how_many( int array[], const int NUM_OBJ_MAX, int i)
{
cout<< "How many objects on rod " << i << ": ";
cin>> array[i];
while (array[i] < 1 || array[i] > NUM_OBJ_MAX)
{
cout<< "Sorry, the number of objects must be positive and less than or equal to 10." << endl;
cout<< "Hpw many objects are on rod " << i << ": ";
cin>> array[i];
}
}

void print_rods( int array[], const int array_size)
{
cout<< endl;
int i; //used to count in the loop
int sum = sum_objects( array, array_size); //sum of all the objects

for (i = 0; i < array_size; i++)
{
cout<< setw(7) << "Rod " << i << ":";
cout<< setw(15) << print_pound;
percentage( sum, array, i);
}
cout<< endl;
}

int sum_objects( int array[], const int array_size)
{
int i; //used to count in loop
int sum = 0; //sum of all the objects

for (i = 0; i < array_size; i++)
{
sum = sum + array[i];
}
return (sum);
}

void print_pound( int array[])
{
int i; //used to count in loop
for (i = 0; i <= array[i]; i++)
{
cout<< "#";
}
}


void percentage( const int sum, int array[], int i)
{
int percent;

percent = array[i] / sum;

cout<< "(" << fixed << setprecision(3) << percent << "%)" << endl;
}

void stats( int array[], int array_size)
{
smallest( array, array_size);
largest( array, array_size);
average( array, array_size);
}


void smallest( int *array, int array_size)
{
int small = 0; //smallest rod
int i; //used to count in the loop

for (i = 0; i < array_size; i++)
{
if (*array[i] < *array[small])
{
small = i;
}
}

cout<< "Rod " << small << " has the smallest number of objects with " << array[small] << " object(s)." << endl;

}


void largest( int array, int array_size)
{
int large = 0; //largest rod
int i; //used to count in the loop

for (i = 0; i < array_size; i++)
{
if (array(i) > array(large))
{
large = i;
}
}

cout<< "Rod " << large << " has the largest number of objects with " << array[large] << " object(s)." << endl;

}


void average( int array[], int array_size)
{

int sum = sum( array, array_size); //total number of objects
int num_rods = 0; //number of rods that have one or more object
int i; //used to count in the loop
double avg; //average number of objects

for ( i = 0; i < array_size; i++)
{
if ( array[i] > 0)
{
num_rods++;
}
}

avg = sum / num_rods;

cout<< "The average number of objects per rod (i.e., rods with objects) is " << fixed << setprecision(2) << avg << " objects." << endl;

}


bool rod_empty( int array[], int array_size)
{
int i; //used to count in the loop

for (i = 0; i < array_size; i++)
{
if (array[i] > 0)
{
return (false);
}
}

return (true);
}

void move( int array[], int array_size, int player, int rod_chosen, int remove)
{
rod_chosen = pick_rod( player);
validate_rod_num( player, rod_chosen, array_size);
validate_rod_obj( array, player, rod_chosen);
remove( array, rod_chosen);
}


int pick_rod( int player)
{
int rod_chosen; //rod the player choses to play
cout<< "Player (" << player << ") : Which rod would you like to play? ";
cin>> rod_chosen;

return (rod_chosen);
}


void validate_rod_num( int player, int rod_chosen, int array_size)
{
while (rod_chosen < 0 || rod_chosen >= array_size)
{
cout<< "Invalid rod number. Please try again." << endl;
pick_rod( player);
}
}


void validate_num_obj( int array[], int player, int rod_chosen)
{
while (array[rod_chosen] < 1)
{
cout<< "Rod " << rod_chosen << " has zero objects. Please select a different rod." << endl;
pick_rod( player);
}
}

void remove_from_rod( int array[], int rod_chosen)
{
int remove; //number of objects to remove from the rod

remove = remove_read( array, rod_chosen);

while (remove > array[rod_chosen])
{
cout<< "Can only remove up to " << array[rod_chosen] << " object(s). Please try again." << endl;
remove_read( array, rod_chosen);
}
}


int remove_read( int array[], int rod_chosen )
{
cout<< "Enter number of objects to remove (" << array[rod_chosesn] << " or less) from rod " << rod_chosen << ": ";
cin>> remove;

return (remove);
}

void modify( int array[], int rod_chosen, int remove)
{
array[rod_chosen] = array[rod_chosen] - remove;
}


void winner( int player)
{
cout<< "Congratulations! Player " << player << " wins." << endl;
}

void player_switch( int& player)
{
if ( player == 1)
{
player = 2;
}
else if (player == 2)
{
player = 1;
}
}
The first problem is in main()
1
2
  int array_size = 0;  //size of the array
  int array[array_size];

[Error] ISO C++ forbids variable length array 'array' [-Wvla]

From looking at the code, it seems the correct value should be NUM_RODS_MAX, according to the logic of function num_rods(). Hence it should read
 
    int array[NUM_RODS_MAX];


Next problem, in function print_rods()
 
      cout<< setw(15) << print_pound;

[Warning] the address of 'void print_pound(int*)' will never be NULL [-Waddress]

It seem you just want to print a string of # symbols, so change the line to
 
      cout<< setw(15) << string(array[i], '#');
and remove the function print_pound() completely. It needs the <string> header. See (6) fill constructor
http://www.cplusplus.com/reference/string/string/string/

Now, at last, we come to the first of the errors listed in the OP.
subtraction.cpp: In function ‘void print_rods(int*, int)’:
subtraction.cpp:157: error: ‘percentage’ was not declared in this scope


At the top of the program code the function prototype for the percentage() function is missing.
 
void percentage( const int sum, int array[], int i);



Next, function smallest(), this line,
 
      if (*array[i] < *array[small])
should read
 
      if (array[i] < array[small])


Function largest() - there are several errors. Just copy the syntax from function smallest(), the two are very similar in layout.


In function average()
 
  int sum = sum( array, array_size); //total number of objects 
I assume that should be
 
  int sum = sum_objects( array, array_size); // total number of objects 


That will do for now. There are a few more errors to fix, but I hope this helps.
Last edited on
Topic archived. No new replies allowed.