}
cout<<setw(10)<<"\nTotal Price:"<<totprice;
cout<<setw(10)<<"\nTotal Tax:"<<totax;
cout<<setw(10)<<"\nTotal Price With Tax:"<<totprice+totax;
cin.get();
}
Can Some One Plz Tell Me How This Array Name work?
I Initialize The Array By Allocation The Size To 10. After That Inside The For Loop I'm Taking 5 Item Names(One At A Time). But When Displaying The Names I Entered It Display Only The Current One. Previously Entered Item Name Will Not Display. How This Work Actually. Can Some One Plz Explain This ?????? :(
struct Item
{
std::string name;
float price;
};
// my arrays of items
Item itemArray[10]; // allocate 10 items.
int main()
{
for(int i = 0; i < 6; i++)
{
// data entry.
std::cin >> itemArray[ i ].name;
std::cin >> itemArray[ i ].price;
}
for(int i = 0; i < 6; i++)
{
//reproduce list
std::cout << "Name: " << itemArray[ i ].name;
std::cout << " Price: " << itemArray[ i ].price << std::endl;
}
// tally total.
float total = 0;
for(int i = 0; i < 6; i++)
{
total+=itemArray[ i ].price;
}
// compute tax
float Tax = total*TaxRate;
float withTax = Total + tax;
// report findings.
}
What you defined with char name[10] was a character array of 10 bites, and every time you read in fron cin, it would replace that one variable over and over. Thus the last one entered would be the last one you saw. If you wanted 10 names it would be char name[10] [10] a two dimensional array. That is why in my code example I did what I did with a struct and then made 10 instances of the struct with item itemArray[10] with all the relative information.