Here is the code (yes, I know it's a lot):
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//Define structure
struct Type
{
string name;
long int numOfItem;
double cost;
double sale;
double taxRate;
double profit;
};
//Function prototypes
string getName(); //Get Type.name
long int getAmount(); //Get Type.numOfItem
double getCost(); //Get Type.cost
double getSale(); //Get Type.sale
double getTax(); //Get Type.taxRate
int main()
{
int numTypes; //How many products are being sold
double totalProfit = 0.0; //Will hold the total profit later
double budget = 0.0; //Will hold how much you have available to spend
double totalCost = 0.0; //Will hold the amount needed to buy the items
cout << "What is your budget?\n";
cin >> budget; //Get the budget
cout << "How many types of items are there?\n";
cin >> numTypes; //Get the number of items
Type *products = new Type[numTypes]; //Dynamically allocate an array
cout << fixed << showpoint << setprecision(2); //Set the precision
for(int count = 0; count < numTypes; count++)
{
products[count].name = getName(); //This doesn't want to work
products[count].numOfItem = getAmount();
products[count].cost = getCost();
products[count].sale = getSale();
products[count].taxRate = (getTax()/100);
products[count].profit = ((products[count].sale
+ (products[count].sale
* products[count].taxRate))
- (products[count].cost
+ (products[count].cost
* products[count].taxRate)));
totalProfit += (products[count].profit
* products[count].numOfItem);
totalCost += (products[count].cost
+ (products[count].cost
* products[count].taxRate
* products[count].numOfItem));
} //Get the information about the item(s)
cout << "Your total cost is $" << totalCost << endl; //Display the cost of the items
//See if you have the funding to get the items, if not, end the program
if(budget < totalCost)
{
cout << "You are $" << (totalCost - budget) << " over budget!\n";
cout << "Order cancelled. Insufficeint funding.";
return 0;
}
else if(budget == totalCost)
{
cout << "You have met your budget.";
}
else
{
cout << "You are $" << (budget - totalCost) << " under budget!\n";
}
//Get the possible profit per item
cout << "Here is the profit for each unit of each item.\n";
for(int index = 0; index < numTypes; index++)
{
cout << "Item name: " << products[index].name << endl;
cout << "Profit: " << products[index].profit << endl;
} //Display the possible profit per item
delete [] products; //Close the array
products = nullptr; //Prevent bugs in the code
cout << "The total profit for these numbers of these items is: ";
cout << totalProfit << endl; //Display the total possible profit
system("pause");
return 0; //End of program
}
//Functions other than main
string getName()
{
string itemName;
cout << "What is the name of the item: ";
getline(cin, itemName); //This doesn't want to work
return itemName;
}
long int getAmount()
{
long int quantity;
cout << "How many of this item are there?\n";
cin >> quantity;
return quantity;
}
double getCost()
{
double itemCost;
cout << "How much does this item cost (before tax)?\n";
cin >> itemCost;
return itemCost;
}
double getSale()
{
double retail;
cout << "How much are you selling it for (before tax)?\n";
cin >> retail;
return retail;
}
double getTax()
{
double tax;
cout << "What is the tax rate on this item (in percent)?\n";
cin >> tax;
return tax;
}
For the most part, it works, just not the name part.
When it comes time to input an item name, it fails.
What is the name of the item: How many of this item are there?
(blah blah blah)
etc.
When it comes time to output an item name, it fails.
Item name:
Profit: (blah blah blah)
etc.
(blah blah blah)
is user input numbers in examples above
No item name! What do I do?