Hello b3y0nd,
Again
PLEASE USE CODE TAGS. Along with proper indenting it makes your code easier to read.
The header files are OK as best as I can tell. They are the same as what I used to test the original function.
"
using namespace std
" should be
avoided at all times. It
WILL get you in trouble some day. There are many posts here on this subjece and this is helpful
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
I understand your use of the typedef, but I would consider making it a "double" instead of the "int" since the numbers are likely in dollars and cents.
I still have a problem wiith the "getSales" function. The use os global variables in the function and then local variables of the same name will work, but local variables will over shadow the global variables causing a problem. If you need these global variables for later it may be better to define them in main and pass them to the function like you did with "numOfYears". If you need these global variables to hold a number change inside the for loop not inside the () of the for condition. Keep in mind that anything defined in the () after "for" is a local to the for loop and disappears after the for loop ends.
At the end of the "getSales" function you have this comment
// Fill in the code to read and store the next value
. Not sure what you have in mind here, but you really should not add anything else here. The function should do one thing.
With a slight change to the "typedef", after seeing what you did, and the array in main that I setup for testing I redid the code for "getSales" and added a function to print the array. Thi may not be what you want, but, at least, you can use it for ideas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <iomanip>
constexpr size_t MAXYEAR{ 10 };
constexpr size_t MAXQUARTER{ 4 };
constexpr size_t MAXROW{ 10 };
constexpr size_t MAXCOL{ 5 };
typedef double SalesType[MAXROW][MAXCOL];
void getSales(SalesType table, int& numOfYears);
void GetSales(SalesType table, int& numOfYears);
void PrintTable(const SalesType table, const int numOfYears);
int main()
{
SalesType table
{
{2000, 10256.25, 15872.49, 20273.91, 25456.85},
{2001, 11987.61, 16573.42, 21357.75, 26951.95}
};
int numOfYears{ 2 };
//GetSales(table, numOfYears);
PrintTable(table, numOfYears);
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
// <--- This function has problems.
void getSales(SalesType table, int& numOfYears)
{
std::cout << "Please input the number of years (1 -" << MAXYEAR << "): ";
std::cin >> numOfYears;
int quarter = 0;
int columncount = 1;
for (int count = 0, displaycount = 1; count < numOfYears; count++, displaycount++)
{
std::cout << "\nInput year " << displaycount << " (e.g. 2004): ";
std::cin >> table[count][0];
for (int quarter = 1/*, columncount = 1*/; quarter <= MAXQUARTER; quarter++/*, columncount++*/);
{
std::cout << "Input the sales figures for the " << columncount << " Quarter Year " << table[count][0] << ": ";
std::cin >> table[count][quarter];
columncount++;
}
std::cout << std::endl;
}
// Fill in the code to read and store the next value
}
// <--- New function does same as above.
void GetSales(SalesType table, int& numOfYears)
{
std::cout << "Please input the number of years (1 -" << MAXYEAR << "): ";
std::cin >> numOfYears;
for (int row = 0; row < numOfYears; row++)
{
std::cout << "\nInput year " << row + 1 << " (e.g. 2004): ";
std::cin >> table[row][0]; // <--- Used to store the year in column 0.
for (size_t col = 1; col <= MAXQUARTER; col++) // <--- Starts at 1 to store the data in the proper column. Starting at 0 would overwrite the year.
{
std::cout << "Input the sales figures for the " << col << " Quarter Year " << table[0][0] << ": ";
std::cin >> table[row][col];
}
}
}
// <--- Added for testing.
void PrintTable(const SalesType table, const int numOfYears)
{
int year{};
std::cout << std::fixed << std::showpoint << std::setprecision(2);
for (int row = 0; row < numOfYears; row++)
{
year = table[row][0];
std::cout << "\n Year " << year << std::endl;
std::cout << "1st Qtr 2nd Qtr 3rd Qtr 4th Qtr" << std::endl;
for (size_t col = 1; col <= MAXQUARTER; col++)
{
std::cout << std::setw(8) << table[row][col] << " ";
}
std::cout << std::endl;
}
}
|
Note: for classes, structs and functions I like to start these names with a capital letter. This shows a difference between a regular variable name and something different.
Notice also that I used the names "MAXQUARTER" and "MAXROW" for better clarity and readability.
I still need to test your complete code. I will let you know what I find.
Hope that helps,
Andy