AlliedAdmin
asked on
Getting errors when trying to compile in C++
Getting errors when compiling the code below. Errors are related to the graph function on line 121. I'm just learning C++ so I'm not sure where I went wrong. Thx in advance.
//Reads data and displays a bar graph showing productivity for each plant.
#include <iostream>
#include <cmath>
#include <iomanip>
const int NUMBER_OF_PLANTS = 4;
void input_data(int a[], int last_plant_number);
//Precondition: last_plant_number is the declared size of the arraya.
//Postcondition: For plant_number = 1 throughlast_plant_number:
//a[plant_number-1] equals the total production for plant numberplant_number.
void scale(int a[], int size);
//Precondition: a[0] through a[size-1] each has a nonnegativevalue.
//Postcondition: a[i] has been changed to the number of 1000s(rounded to
//an integer) that were originally in a[i], for all i such that 0<= i <= size -1.
void graph(const int asterisk_count[], int last_plant_number);
//Precondition: asterisk_count[0] throughasterisk_count[last_plant_number-1]
//have nonnegative values.
//Postcondition: A bar graph has been displayed saying thatplant
//number N has produced asterisk_count[N-1] 1000s of units, foreach N such that
//1 <= N <= last_plant_number
void get_total(int& sum);
//Reads nonnegative integers from the keyboard and
//places their total in sum.
int roundd(double number);
//Precondition: number >= 0.
//Returns number rounded to the nearest integer.
int main( )
{
using namespace std;
int production[NUMBER_OF_PLANTS];
cout << "This program displays a graphshowing\n"
<<"production for each plant in the company.\n";
input_data(production, NUMBER_OF_PLANTS);
scale(production, NUMBER_OF_PLANTS);
graph(production, NUMBER_OF_PLANTS);
system("pause");
return 0;
}
//Uses iostream:
void input_data(int a[], int last_plant_number)
//The rest of the definition of input_data is given in Display7.6.
{
using namespace std;
for (int plant_number = 1;
plant_number <= last_plant_number; plant_number++)
{
cout << endl
<< "Enter production data for plant number "
<< plant_number << endl;
get_total(a[plant_number- 1]);
//cout<<plant_number-1<<""<<a[plant_number-1]<<endl;
}
}
//Uses iostream:
void get_total(int& sum)
//The rest of the definition of get_total is given in Display7.6.
{
using namespace std;
cout << "Enter number of units produced byeach department.\n"
<< "Append anegative number to the end of the list.\n";
sum = 0;
int next;
cin >> next;
while (next >= 0)
{
sum = sum + next;
cin >> next;
}
cout << "Total = " << sum <<endl;
}
//The rest of the definition of scale is given in Display 7.7.
void scale(int a[], int size)
{
for (int index = 0; index < size;index++)
a[index] =roundd(a[index]/1000.0);
}
//Uses cmath:
//The rest of the definition of round is given in Display 7.7.
//Uses iostream:
int roundd(double number)
{
using namespace std;
return static_cast<int>(floor(number +0.5));
}
/*void graph(const int asterisk_count[], int last_plant_number)
{
using namespace std;
cout << "\nUnits produced in thousands ofunits:\n";
for (int plant_number = 1;
plant_number <= last_plant_number; plant_number++)
{
cout << "Plant #"<< plant_number << " ";
print_asterisks(asterisk_count[plant_number - 1]);
cout << endl;
}
}
*/
void graph(const int a[], int n)
{
using namespace std;
int i,j,max=a[0];
for(i=1;i<n;i++)
if(a[i]>max)
max=a[i];
char graph[max][n];
for(i=0;i<max;i++)
for(j=0;j<n;j++)
graph[i][j]=' ';
for(i=0;i<n;i++)
for(j=0;j<a[i];j++)
graph[j][i]='*';
cout << "\nUnits produced in thousands ofunits:\n";
for(i=max-1;i>=0;i--)
{cout<<setw(3)<<i<<" ";
for(j=0;j<n;j++)
cout<<graph[i][j]<<" ";
cout<<endl;
}
for(i=0;i<n;i++)
{if(i==0)
cout<<" ";
cout<<" plant ";
}
cout<<endl;
for(i=0;i<n;i++)
{if(i==0)
cout<<" ";
cout<<" "<<i+1<<" ";
}
cout<<endl;
}
//Uses iostream:
void print_asterisks(int n)
{
using namespace std;
for (int count = 1; count <= n;count++)
cout << "*";
What's the errors that you are getting?
ASKER
main.cpp(120): error C2057: expected constant expression
main.cpp(120): error C2466: cannot allocate an array of constant size 0
main.cpp(120): error C2057: expected constant expression
main.cpp(120): error C2466: cannot allocate an array of constant size 0
main.cpp(120): error C2087: 'graph' : missing subscript
main.cpp(120): error C2133: 'graph' : unknown size
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
main.cpp(120): error C2466: cannot allocate an array of constant size 0
main.cpp(120): error C2057: expected constant expression
main.cpp(120): error C2466: cannot allocate an array of constant size 0
main.cpp(120): error C2087: 'graph' : missing subscript
main.cpp(120): error C2133: 'graph' : unknown size
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much for your help. Your solution makes complete sense.
No problems, glad to help!