Reputation: 85
Alright so I have an assignment that I have been completely stumped on. My code thus far is as follows
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<sstream>
using namespace std;
const int SIZE = 12;
struct Division
{
char divName[SIZE]; // Division name
double sales[4]; // Quarterly sales stored as an array
};
int main()
{
void Intro();
Division CreateCorporateFile();
Division DisplayCorporateSales();
Intro();
CreateCorporateFile();
DisplayCorporateSales();
system("PAUSE");
return 0;
}
void Intro()
{
cout<<"This program will prompt you to enter in quarterly sales for "
"four different\ndivisions of a company.\n\n";
}
Division CreateCorporateFile()
{
Division div;
int x = 0;
//for(int x = 0; x < 4; x++)
do {
int quarter = 1;
cout << "Enter the name of the division: ";
cin >> div.divName;
for(int i = 0; i < 4; i++)
{
cout << "Enter in the sales for quarter "<< quarter <<": ";
cin >> div.sales[i];
if(div.sales[i] > 0)
{
quarter++;
}
else
{
cout << "Sales are not allowed to be negative.\n";
}
}
x++;
} while(x < 4);
return div;
}
Division DisplayCorporateSales()
{
Division test;
Division CreateCorporateFile();
test = CreateCorporateFile();
for(int i = 0; i < 4; i++)
{
cout << "Here are the quarterly sales for " << test.divName
<< ": ";
for(int i = 0, quarter = 1; i < 4; ++i, ++quarter)
{
cout << "Quarter "<< quarter << " sales: $"<< test.sales[i]
<<"\n";
}
}
return test;
}
One of the problems that I am running in to is that it ends up prompting for 8 divisions (should only do it 4 times), and another is that it ends up only displaying the 8th divisions data. So anybody see where I am going wrong? Because I do not, and I have been working on it for hours so I am probably overlooking obvious details.
Upvotes: 0
Views: 97
Reputation: 42175
The reason you only display data for the final Division
is that CreateCorporateFile
only returns a single Division
instance. Each do-while loop overwrites the contents of the previous loop.
One solution to this would be to return an array instead
std::vector<Division> CreateCorporateFile()
{
std::vector<Division> divArray;
Division div;
int x = 0;
do {
...
divArray.push_back(div);
x++;
} while(x < 4);
return divArray;
}
void DisplayCorporateSales()
{
std::vector<Division> divisions = CreateCorporateFile();
for (size_t i=0; i<divisions.size(); i++)
{
Division div = divisions[i];
cout << "Here are the quarterly sales for " << div.divName
<< ": ";
for(int j = 0, quarter = 1; j < 4; ++j, ++quarter)
{
cout << "Quarter "<< quarter << " sales: $"<< div.sales[j]
<<"\n";
}
}
}
Upvotes: 1
Reputation: 5837
The first problem is caused by you declaring the functions you are going to use inside main rather than before it in global scope: void Intro(); Division CreateCorporateFile(); Division DisplayCorporateSales();
The second problem is caused by the fact that you have used the variable i
twice for each of your loops. You need to use a different variable name (or just use quarter
as a loop variable).
Upvotes: 0