Having trouble about 6 errors in my array code [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I can't seem to correctly run the code. Would be of a great help in finding the 6 errors. I am stuck and somehow no idea where to find the error for my simple array code.
Here are the errors I get.
// This program uses two arrays to record the names of 6 types of pizza
// and the sales numbers for each of these types
// The program then finds the best and the worst selling pizzas
#include <iostream>
#include <string>
using namespace std;
int main()
{
constint ARR_SIZE = 6; // Declare the array size and set it to 6
// Declare the array of pizza names and record values in it
int name[ARR_SIZE] = [ "Pepperoni", "Prosciutto", "Vegetarian",
"Sausage",
"Supreme",
"Mozzarella" ];
int sales[ARR_SIZE]; // Declare the sales array
int worstseller_number, bestseller_number; // The subscripts of the best- and worstseller
string worstseller_name, bestseller_name; // The sale numbers of the best- and worstseller
for (int i = 1; i < ARR_SIZE; i++) // A loop to enter all sales numbers
{
cout << "Enter sales for " << name[i] << ": ";
cin >> sales[i];
}
// Make the first element in name[] the bestseller and the worstseller name
worstseller_name = bestseller_name = name[0];
// Make the first element in sales[] the bestseller and the worstseller sales amount
worstseller_number = bestseller_number = sales[0];
for (int i = 0; i <= ARR_SIZE; i++) // Loop through all elements in sales[]
{
if (sales[i] < worstseller_number) // If an element is less than the lowest...
{
worstseller_number = i; // make the lowest sales equal to its sales
worstseller_name = name[i]; // make the worstseller name equal to its name
}
if (sales[i] < bestseller_number) // If an element is higher than the highest...
{
bestseller_number = sales[i]; // make the highest sales equal to its sales
bestseller_name = name[i]; // make the bestseller name equal to its name
}
}
cout << "The bestselling pizza is " << bestseller_name << " with the sales of "
<< bestseller_number << endl; // Display the best selling pizza
cout << "The worst selling pizza is " << worstseller_name << " with the sales of "
<< worstseller_number << endl;
} // display the worst selling pizza

First of all it should be const int ARR_SIZE = 6; Next, your array elements are strings but you are defining the array as int. Another error is that when you declare an array, it should be enclosed within curly brackets instead of square brackets. There was a mistake in the logic when you were trying to accept the sales and calculate the min and max which I have fixed. The first for loop should start from i=0 and the second for loop should go till i<ARR_SIZE
Try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int ARR_SIZE = 6; // Declare the array size and set it to 6
// Declare the array of pizza names and record values in it
string name[ARR_SIZE] = {"Pepperoni", "Prosciutto", "Vegetarian",
"Sausage",
"Supreme",
"Mozzarella"};
int sales[ARR_SIZE]; // Declare the sales array
int worstseller_number, bestseller_number; // The subscripts of the best- and worstseller
string worstseller_name, bestseller_name; // The sale numbers of the best- and worstseller
for (int i = 0; i < ARR_SIZE; i++) // A loop to enter all sales numbers
{
cout << "Enter sales for " << name[i] << ": ";
cin >> sales[i];
}
// Make the first element in name[] the bestseller and the worstseller name
worstseller_name = bestseller_name = name[0];
// Make the first element in sales[] the bestseller and the worstseller sales amount
worstseller_number = bestseller_number = sales[0];
for (int i = 0; i <ARR_SIZE; i++) // Loop through all elements in sales[]
{
if (sales[i] < worstseller_number) // If an element is less than the lowest...
{
worstseller_number = sales[i]; // make the lowest sales equal to its sales
worstseller_name = name[i]; // make the worstseller name equal to its name
}
if (sales[i] > bestseller_number) // If an element is higher than the highest...
{
bestseller_number = sales[i]; // make the highest sales equal to its sales
bestseller_name = name[i]; // make the bestseller name equal to its name
}
}
cout << "The bestselling pizza is " << bestseller_name << " with the sales of "
<< bestseller_number << endl; // Display the best selling pizza
cout << "The worst selling pizza is " << worstseller_name << " with the sales of "
<< worstseller_number << endl;
} // display the worst selling pizza

Related

How would I calcuate the total of all the items entered to then calculate the total price?

Hi there apologise if my question is poorly worded, I'm struggling to find a solution to my problem.
The purpose of my program is to allow the user to enter predefined bar codes that associate with items and a price. The user enters as many barcodes as they want, and when they're done they can exit the loop by pressing "F" and then total price for all the items is displayed.
This is my code so far, I'm very new to programming..
#include <iostream>
#include <iomanip>
using namespace std;
int index_of(int arr[], int item, int n) {
int i = 0;
while (i < n) {
if(arr[i] == item) {
return i;
}
i++;
}
return -1;
}
const int SIZE = 10;
int main()
{
string item [SIZE] = {"Milk", "Bread", "Chocolate", "Towel", "Toothpaste", "Soap", "Pen", "Biscuits", "Lamp", "Battery"};
int barcode [SIZE] = {120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009, 120010};
float price [SIZE] = {10.50, 5.50, 8.00, 12.10, 6.75, 5.20, 2.00, 4.45, 20.50, 10.00};
cout << "*************************************************************" << endl;
cout << "WELCOME TO THE CHECKOUT SYSTEM" << endl;
cout << "Please scan a barcode or manually enter the barcode ID number" << endl;
cout << "*************************************************************\n" << endl;
int newBarcode;
while (true){
cout << "Please enter a barcode (Type 'F' to finish): ", cin >> newBarcode;
int index = index_of(barcode, newBarcode, (sizeof(barcode) / sizeof(barcode)[0]));
cout << "\n>> Name of item: " << item[index] << endl;
cout << ">> Price of item: \x9C" << setprecision (4)<< price[index] << endl;
cout << ">> " <<item[index] << " has been added to your basket. \n" << endl;
float total = 0 + price[index];
cout << ">> Your current basket total is: \x9C" << setprecision(4) << total << endl;
/*float total = 0;
float newtotal = 0;
price[index] = total;
total = newtotal;
cout << ">> " << "Basket total: " << newtotal << endl; */
}
return 0;
}
You will need to iterate over all items and add their value to a variable. You can do it the old way:
float sum = 0;
for(int i = 0; i < SIZE; i++) {
sum += price [i];
}
Or the C++11 way:
float sum = 0;
for(float p : price) {
sum += p;
}
However I must point out a few important issues with your code:
Your array has a fixed size but user can enter as many entries as he wants. To avoid this issue, use vector. It behaves like array but has dynamic size. Simply use push_back() to add a new element.
Don't use separate containers (arrays) for the same group of objects. It's a bad coding practice. You can define a structure for product which will contain name, barcode and price, then make one container for all of the products.
Edit
I'm sorry, I misunderstood your problem. There are many ways to solve this, the most elegant way is to create a map where key is the bar code and value is your product object or just a price.
map<int, float> priceMap;
priceMap.insert(pair<int, float>([your bar code here], [your price here]))
Afterwards just create a vector of bar codes, fill it with user data and iterate over it sum all prices:
float sum = 0;
for(int b : userBarcodes) {
sum += priceMap.at(b);
}
You are trying to read from cin into an int. As you decide to put a stopping condition on 'F' input you must read into a string. Then decide what to do with the value. You will need to check if the input is an int or not. You can do it as given here or here.
Or you may change the stopping condition to a less likely integer like -1. And make sure you always read an int into newBarcode.
There are various small errors which are hard to list out. I have changed them in the code below which is implementing point 2 (You have to add the stopping condition).
One of the error or wrong practice is to declare new variables inside a loop. In most cases you can declare the variables outside and change there values in the loop.
I replaced (sizeof(barcode) / sizeof(barcode)[0] with SIZE as the lists are predefined and unchanging. Anyways you should use (sizeof(barcode) / sizeof(barcode[0]) for length calculation.
#include <iostream>
#include <iomanip>
using namespace std;
int index_of(int arr[], int item, int n) {
int i = 0;
while (i < n) {
if(arr[i] == item) {
return i;
}
i++;
}
return -1;
}
const int SIZE = 10;
int main()
{
string item [SIZE] = {"Milk", "Bread", "Chocolate", "Towel", "Toothpaste", "Soap", "Pen", "Biscuits", "Lamp", "Battery"};
int barcode [SIZE] = {120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009, 120010};
float price [SIZE] = {10.50, 5.50, 8.00, 12.10, 6.75, 5.20, 2.00, 4.45, 20.50, 10.00};
cout << "*************************************************************" << endl;
cout << "WELCOME TO THE CHECKOUT SYSTEM" << endl;
cout << "Please scan a barcode or manually enter the barcode ID number" << endl;
cout << "*************************************************************\n" << endl;
int newBarcode;
float total = 0;
int index;
while (true){
cout << "Please enter a barcode (Type -1 to finish): \n";
cin >> newBarcode;
while(cin.fail()) {
cout << "Not an integer" << endl;
cin.clear();
cin.ignore(100,'\n');
cin >> newBarcode;
}
index = index_of(barcode, newBarcode, SIZE);
cout << index;
if (index == -1) {
cout << "Apologies here for unsupported barcode\n";
continue;
} else {
cout << ">> Name of item: " << item[index] << endl;
cout << ">> Price of item: " << price[index] << "\n";
cout << ">> " <<item[index] << " has been added to your basket. \n";
total = total + price[index];
cout << ">> Your current basket total is: " << total << "\n";
}
}
return 0;
}
Your question could be more helpful to others if you find out what is wrong with your implementation and ask implementation specific questions which will probably be already answered. Asking what is wrong with my code is not quite specific.

C++ Why is my input validation crashing my program?

I have written a simple program that displays total jars of salsa sold, as well as the least and greatest types of salsa sold. The program works fine unless I intentionally use bad user input such as letters and negative numbers. The call to function inputValidation seems to work until I am finished inputting all the jars sold for each salsa type. Then it displays most of the results and crashes.
// this program allows a business to keep track of sales for five different types of salsa
#include <iostream>
#include <string>
#include <climits>
using namespace std;
// function prototype
int inputValidation(int);
int main()
{
const int SIZE = 5;
string names[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int jars[SIZE];
int totalSold = 0;
// get the number of jars sold for each salsa
int tempJars;
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the number of " << names[i] << " salsa jars sold this past month.\n";
cin >> tempJars;
// call to input validation function
jars[i] = inputValidation(tempJars);
totalSold += jars[i];
}
// determine the lowest and highest salsa type sold
int lowest = jars[0],
highest = jars[0],
leastType,
greatestType;
for (int i = 0; i < SIZE; i++)
{
if (jars[i] < lowest)
{
lowest = jars[i];
leastType = i;
}
if (jars[i] > highest)
{
highest = jars[i];
greatestType = i;
}
}
// display results
for (int i = 0; i < SIZE; i++)
{
cout << "You sold " << jars[i] << " jars of " << names[i] << " salsa.\n";
}
cout << "You sold a total of " << totalSold << " jars of salsa.\n"
<< names[leastType] << " salsa sold the least amount of jars, which was " << lowest << " jars sold.\n"
<< names[greatestType] << " salsa sold the most amount of jars, which was " << highest << " jars sold.\n";
}
/*
definition of function inputValidation
inputValidation accepts an int value as its argument. It determines that the value is a number that is
greater than 0. If it is not, then the user is prompted to input an acceptable value. the value is
returned and stored in the corresponding element of the jars array.
*/
int inputValidation(int jars)
{
do
{
while (cin.fail())
{
cin.clear(); // clear the error flags
cin.ignore(INT_MAX, '\n'); // return cin to usable state
cout << "You may only enter non negative numbers. Please try again.\n";
cin >> jars;
}
if (jars < 0)
{
cout << "You may only enter non negative numbers. Please try again.\n";
cin >> jars;
}
} while (jars < 0);
return jars;
}
If jars[0] happens to be the smallest of the five, then leastType is never initialized and contains random garbage. Then an attempt to access names[leastType] exhibits undefined behavior.
Similarly, if jars[0] is the greatest, then greatestType is never initialized.

Sorting both a string array and a double array from highest to lowest(parallel arrays) and aligning the text

Alright I'll bite, this program does sort the number from highest to lowest but it'll only sort if the numbers are arranged in order from highest to lowest;not if it's sporadic. Here are two example of what I mean.
Example 1: 12,11,10,9,8,7,6,5,4,3,2,1.
Example 2: 32,25,24,31,10,11,15,16,8,19,18,5.
You see how in example 2 that some of the numbers are in order like 32 25 24 but some of the others are not. This is my main problem. My secondary problem is aligning the text vertically so that it looks neat. Should I use setw left, right for this? Please give feedback.
Note 1: The IDE that I'm using is codeblocks.
Note 2: Keep in mind that whatever number the user inputs for a particular month has to be vertically parallel to each.
Note 3: I'm pretty sure the problem is with my selection sort. So you really should be looking at the function void selectionsort since that is the function that is doing all the sorting. I just don't know where I went wrong with my sorting though. Everything else seems to be in order.
/* This program lets the user enter the total rainfall
for each month into an array of doubles. The program also
calculates and displays the total rainfall for a year, the average
monthly rainfall, and the months with the highest and lowest amounts.
The program also displays the list of months, sorted in order of
rainfall from highest to lowest.*/
/* This program does not accept negative numbers for monthly rainfall
figures.*/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void SelectionSort(string[], double[], int);// Function Protoype.
int main()
{
const int SIZE = 12; /* A constant integer that represent the total
amount of months in a year. */
double totalRainfallPerMonth[SIZE]; /* Loop this array to force the user
to enter variables for each
element in this array. */
double totalRainfallPerYear = 0; /* The total amount of rainfall
(in inches) per year. */
// An array of every month.
string monthArray[SIZE]={"January", "February", "March", "April", "May",
"June", "July","August", "September",
"October", "November", "December"};
double average; // A variable that holds the average monthly rainfall.
int i; // Will be used as a counter for any loop.
cout << fixed << showpoint << setprecision(2); // Set decimal notation.
for(i=0; i<=11; i++)
{
// Prompt the user to enter values.
cout << "Please enter the total rainfall(in inches) for ";
cout << monthArray[i] << ": ";
cin >> totalRainfallPerMonth[i];
while(totalRainfallPerMonth[i] < 0) /* If the user enters a negative
value */
{
cerr << "No negative values allowed. "; // Display error message.
cout << "Please try again. ";
cin >> totalRainfallPerMonth[i];
}
}
for(i=0; i<=11; i++)
{
// Calculate the total rainfall for a year.
totalRainfallPerYear += totalRainfallPerMonth[i];
}
// Display the total rainfall for a year.
cout << "\nThe total rainfall this year is " << totalRainfallPerYear;
cout << " inches of rain. " << endl;
// Calculate the average monthly rainfall.
average = totalRainfallPerYear / SIZE;
// Display the average
cout << "\nThe average monthly rainfall per month is ";
cout << average;
cout << " inches of rain. " << endl << endl << endl;
cout << "\n" << "Month " << "\t";
cout << " Rainfall(in inches)" << endl;
cout << "-----------------------------------";
SelectionSort(monthArray, totalRainfallPerMonth, SIZE); /* Call in the
function. */
return 0;
}
void SelectionSort(string month[], double rain[], int SIZE)
{
int i;
int j;
int min;
for (i = 0; i < SIZE - 1; i++)
{
min = i; // The intial subscript or the first element.
for (j = i + 1; j < SIZE; j++)
{
if (rain[j] > rain[min]) /* if this element is greater,
then it is the new minimum */
{
min = j;
// swap both variables at the same times
double tempDouble = rain[i];
rain[i] = rain[j];
rain[j] = tempDouble;
string tempString = month[i];
month[i] = month[j];
month[j] = tempString;
}
}
}
for(i=0; i<=11; i++)
{
/* Display the amount of rainfall per month from highest to
lowest */
cout << "\n" << month[i] << "\t" << rain[i] << endl;
}
}
There is in fact an error in your selection sort implementation: you are swapping elements too early and too often.
Only after you have performed a full sweep over the whole remaining array and therefore have determined the global minimum (I keep that term in order to be consistent with the variable name and the comments in the code, although in fact you are looking for the maximum) you should perform a single swap of the first element of the remaining array with the minimum element.
The corrected code (only the main loop of the sorting function shown) should look like this:
for (i = 0; i < SIZE - 1; i++)
{
min = i; // The intial subscript or the first element.
for (j = i + 1; j < SIZE; j++)
{
if (rain[j] > rain[min]) /* if this element is greater,
then it is the new minimum */
{
min = j;
}
}
// swap both variables at the same times
double tempDouble = rain[i];
rain[i] = rain[min];
rain[min] = tempDouble;
string tempString = month[i];
month[i] = month[min];
month[min] = tempString;
}

Josephus prob using arrays?

http://en.wikipedia.org/wiki/Josephus_problem
The user can choose how many people are in the circle.
The user can choose the value of each person.
The user can choose the counting number for the persons death.
ex. User picks 5, every 5th person shall die.
I was thinking something like -
User choose amount of people ex- 50
PeopleArray becomes PeopleArray[50]
User chooses value of elements in PeopleArray[50]
They would have to type 50 values for the 50 elements
Death
User picks 3 so every third person dies how would I erase that number from the array.
Problem ^-Not sure on how to do the above with arrays
int main(){
int people = 5;
int peopleArray[5];
int peopleValue = 1;
int death;
cout << "Enter the amount of people: ";
cin >> people;
peopleArray[people];
for(int x = 1;x<=people;x++){
cout << "Enter the value of person #" << x << ":";
cin>> peopleValue;
peopleArray[peopleValue]; //Suppose to put the value into the array
}
}
If I understand you correctly what you are trying to do would be something like...
vector<int> totalPeople;
int totalIn;
int deathIn;
int person = 1;
int nthPerson = 0;
cout << "Enter total number of people." << endl;
cin >> totalIn;
cout << endl << "Pick the nth person." << endl;
cin >> deathIn;
for ( int i = 0; i < totalIn; i++ )
{
totalPeople.pushback(person++);
}
nthPerson = deathIn - 1;
while ( nthPerson < totalPeople.size() )
{
totalPeople.erase(totalPeople.begin() + nthPerson);
nthPerson = nthPerson + (deathIn -1);
}
or say that totalPeople[nthPerson] = 0;
This will remove the nth person from the total list of people.

Corporate Sales; accessing private static variable with linker error

EDIT: This has been solved. Please take a look at my second coding to see what was edited to fix the linker error problem. Please close this thread if it is possible.
Thanks for your answers/response/feedback <3
I am trying to access my private static variable to return the total corporate sales (Year_Sales) with function getvalue(). I am trying to trigger the function with
cout << DivisionSale[0].getvalue();
but I get the following error...
[linker error] undefined reference; private static variable
Here is my coding for the project
#include <iostream>
using namespace std;
// class declaration
class DivSales // The 6 divisions will use this
{
private:
// Holds the total corporate sales for all divisions
static int Year_Sales;
public:
static int Qtrsale[4]; // Elements for sale figures, 4 quarterly sales
static void AddSales (int,int,int,int); // calculates annual ammount
int Sales(int); // output
static int getvalue() { return Year_Sales; }
};
// This member function takes arguments and is copied into array holding sales data
// Year_Sales contains total of the 4 sales
void DivSales::AddSales(int sale1, int sale2, int sale3, int sale4)
{
// arrays 0 through 3 store the quarterly sales
Qtrsale[0] = sale1;
Qtrsale[1] = sale2;
Qtrsale[2] = sale3;
Qtrsale[3] = sale4;
// private variable access
// combines the 4 arguments
Year_Sales = Year_Sales + sale1 + sale2 + sale3 + sale4;
}
// returns values of quarterly sales for each division
// Used for table formatting in main
int DivSales::Sales(int n)
{
// Qtrsale[n] holds value from input to return for output
// returns sale(n)
int value = Qtrsale[n];
return value;
}
// this function is triggered if user enters a negative quarterly sale value
void error()
{
system("cls");
cout << "You have entered a negative value. Restart program and try again";
cout << "Press any key to restart program\n";
system("pause");
exit(0);
}
// Overload constructer
// Definition of the static member of DivSales class
// for access to the private static variable
int DivSales::Year_Sales = 0;
// Start main function
int main()
{
// insurance for protecting the array storage(s)
const int DS = 6;
DivSales DivisionSale[DS]; // 6 division with each of their own array
int quarter, division; // quarter = 4; division = 6
// This will pass quarter sales to AddSales
for (division = 0; division < 6; division++)
{
// 4 quarters for array storage
int Qrt1, Qrt2, Qrt3, Qrt4;
int Q; // used to check for negative input
// Prompts the user for quarter sales of each division
// divisions 1 - 6
cout << "Enter Sales of Division: " << division + 1 << endl;
// Each quarter is assigned to Q which is used for error checking.
// Error checking occurs when negative value is detected
cout << "Enter Q1 Sales: ";
cin >> Qrt1;
Q = Qrt1;
if (Q < 0)
error();
cout << "Enter Q2 Sales: ";
cin >> Qrt2;
Q = Qrt2;
if (Q < 0)
error();
cout << "Enter Q3 Sales: ";
cin >> Qrt3;
Q = Qrt3;
if (Q < 0)
error();
cout << "Enter Q4 Sales: ";
cin >> Qrt4;
Q = Qrt4;
if (Q < 0)
error();
// passes Qrt1 through Qrt4 to AddSales
DivisionSale[division].AddSales(Qrt1,Qrt2,Qrt3,Qrt4);
}
cout << "----------------------------------------\n";
cout << "\t" << "Q1" << "\t" << "Q2" << "\t" << "Q3" << "\t" << "Q4" << endl;
// table for quarterly sales of each division
for (division = 0; division < 6; division++)
{
cout << "Div " << division + 1;
for (quarter = 0; quarter < 4; quarter++)
{
// calling function Sales 6 times
cout << "\t" << DivisionSale[division].Sales(quarter);
cout << endl;
}
}
cout << "----------------------------------------\n";
cout << "\n Total All Division Sales for the Year: ";
cout << DivisionSale[0].getvalue();
system("pause");
return 0;
}
And here is what I need to do:
A corporation has six divisions, each responsible for sales to different geographic locations. Design a DivSales class that keeps sales data for a division, with the following members:
• An array with four elements for holding four quarters of sales figures for the division
• A private static variable for holding the total corporate sales for all divisions for the entire year.
• A member function that takes four arguments, each assumed to be the sales for a quarter. The value of the arguments should be copied into the array that holds the sales data. The total of the four arguments should be added to the static variable that holds the total yearly corporate sales.
• A function that takes an integer argument within the range of 0 to 3. The argument is to be used as a subscript into the division quarterly sales array. The function should return the value of the array element with that subscript.
Write a program that creates an array of six DivSales objects. The program should ask the user to enter the sales for four quarters for each division. After the data is entered, the program should display a table showing the division sales for each quarter. The program should then display the total corporate sales for the year.
Input Validation: Only accept positive values for quarterly sales figures.
The Linker error is the only error that is standing in my way. I am well aware that there could be some code optimization, but I really need to fix the linker error.
EDIT: Revised what's static and not static and used int DivSales::Year_Sales
New coding:
#include <iostream>
using namespace std;
// class declartion
class DivSales // The 6 divisions will use this
{
private:
// Holds the total corporate sales for all divisions
static int Year_Sales;
int Qtrsale[4]; // Elements for sale figures, 4 quarterly sales
public:
void AddSales (int,int,int,int); // calculates annual ammount
int Sales(int); // output
int getvalue() { return Year_Sales; }
};
// This member function takes arguments and is copied into array holding sales data
// Year_Sales contains total of the 4 sales
void DivSales::AddSales(int sale1, int sale2, int sale3, int sale4)
{
// arrays 0 through 3 store the quarterly sales
Qtrsale[0] = sale1;
Qtrsale[1] = sale2;
Qtrsale[2] = sale3;
Qtrsale[3] = sale4;
// private variable access
// combines the 4 arguments
Year_Sales = Year_Sales + sale1 + sale2 + sale3 + sale4;
}
// returns values of quarterly sales for each division
// Used for table formatting in main
int DivSales::Sales(int n)
{
// Qtrsale[n] holds value from input to return for output
// returns sale(n)
int value = Qtrsale[n];
return value;
}
// this function is triggered if user enters a negative quarterly sale value
void error()
{
system("cls");
cout << "You have entered a negative value. Restart program and try again";
cout << "Press any key to restart program\n";
system("pause");
exit(0);
}
// Overload constructer
// Definition of the static member of DivSales class
// for access to the private static variable
int DivSales::Year_Sales;
// Start main function
int main()
{
// insurance for protecting the array storage(s)
const int DS = 6;
DivSales DivisionSale[DS]; // 6 division with each of their own array
int quarter, division; // quarter = 4; division = 6
// This will pass quarter sales to AddSales
for (division = 0; division < 6; division++)
{
// 4 quarters for array storage
int Qrt1, Qrt2, Qrt3, Qrt4;
int Q; // used to check for negative input
// Prompts the user for quarter sales of each division
// divisions 1 - 6
cout << "Enter Sales of Division: " << division + 1 << endl;
// Each quarter is assigned to Q which is used for error checking.
// Error checking occurs when negative value is detected
cout << "Enter Q1 Sales: ";
cin >> Qrt1;
Q = Qrt1;
if (Q < 0)
error();
cout << "Enter Q2 Sales: ";
cin >> Qrt2;
Q = Qrt2;
if (Q < 0)
error();
cout << "Enter Q3 Sales: ";
cin >> Qrt3;
Q = Qrt3;
if (Q < 0)
error();
cout << "Enter Q4 Sales: ";
cin >> Qrt4;
Q = Qrt4;
if (Q < 0)
error();
// passes Qrt1 through Qrt4 to AddSales
DivisionSale[division].AddSales(Qrt1,Qrt2,Qrt3,Qrt4);
}
cout << "\n----------------------------------------\n";
cout << "\t" << "Q1" << "\t" << "Q2" << "\t" << "Q3" << "\t" << "Q4" << endl;
// table for quarterly sales of each division
for (division = 0; division < 6; division++)
{
cout << "Div " << division + 1;
for (quarter = 0; quarter < 4; quarter++)
{
// calling function Sales 6 times
cout << "\t$" << DivisionSale[division].Sales(quarter);
}
cout << endl;
}
cout << "----------------------------------------\n";
cout << "\nTotal corporate sales for the year: ";
cout << "$" << DivisionSale[0].getvalue() << endl;
system("pause");
return 0;
}
You simply need to have a definition for the variable somewhere:
int DivSales::Qtrsale[4];
You have a declaration in the class, but there also needs to be a definition (in a single place) somewhere in the project.
This has to be a C++ FAQ, but I'm too lazy to look it up right now.