# help me im a C++ noobie!!!

• 09-03-2002
fatso
help me im a C++ noobie!!!
hey hey hey

with the following code which is pretty simple, it works fine except for the line: item[5].grossF();

this in turn displays the gross of only item 5 which is basically just the items total...

i want it to add all the gross total together and then display the sum as the variable grossT?

any help will recieve copious amounts of sex in return!

thanks

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// Defining the stock structure
struct stock
{
// Variables
int quantity;
string name;
float price;
float total[6];
float grossT;

// Functions
void calc(int j);
void display(int i);
void grossF();
};

// Function which calculates total value of stock
void stock::calc(int j)
{
total[j] = (price * quantity);

grossT = grossT + total[j];
}

// Function which prints headings and border to the screen
{
cout << setw(15) << "Stock Item" << setw(15) << "Quantity" << setw(20) << "Unit Price" << setw(20) <<"Total Value\n";
cout << "----------------------------------------------------------------------\n";
}

// Function displays name, quantity, price and calculated total for each stock type
void stock::display(int i)
{
cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
cout << setw(15) << name << setw(15) << quantity << setw(20) << price << setw(20) << total[i] << "\n";
}

// Prints the bottom border
void footer()
{
cout << "----------------------------------------------------------------------\n";
}

// Calculates total value of entire stock
void stock::grossF()
{
// Displays footer with gross title and calculated value
cout << setw(53) << "Gross Total: " << setw(17) << grossT << "\n";
}

// main function which controls program flow initializes structure items
void main()
{
// Decleration of struct items
stock item[6];
item[0].name = "Television";
item[0].price = 368.00;
item[1].name = "Video Recorder";
item[1].price = 268.00;
item[2].name = "Camera";
item[2].price = 300.00;
item[3].name = "DVD";
item[3].price = 468.00;
item[4].name = "Hi Fi Stereo";
item[4].price = 2500.00;
item[5].name = "Video CD";
item[5].price = 345.00;
item[0].grossT = 0;
item[1].grossT = 0;
item[2].grossT = 0;
item[3].grossT = 0;
item[4].grossT = 0;
item[5].grossT = 0;

cout << "PLEASE ENTER STOCK QUANTITY, ENTER 0 TO EXIT PROGRAM...\n\n";

// For loop which collects quantity of each stock from user
for (int counter = 0; counter < 6; counter++)
{
cout << "Please enter the number of " << item[counter].name << "s: ";
cin >> item[counter].quantity;
}

cout << "\n";

// For loop which calculates total value of each stock
for (int j = 0; j < 6; j++ )
item[j].calc(j);

// Displays stock name, price and total value using a for loop
for (int i = 0; i < 6; i++ )
item[i].display(i);

// Footer function
footer();

// Displays gross total of all stock
item[5].grossF(); this is the line!!!

cout << "\n\n";
}
• 09-03-2002
Frank_1974_@@
I saw that you are printing the gross total of
the last Item in your array of structures.
Remember that you are using 5 different structs.
Try to run through all the structs and sum the gross totals
That is what you want.
• 09-07-2002
Klinerr1
use code tags and read faqs! all newbies should
code tags ar like this

(code)
code here
(/code)

replace ( and ) with [ and ]
• 09-07-2002
face_master
>> any help will recieve copious amounts of sex in return!

I don't know if I really want to take up that offer due to the fact that youre probably a bloke and you call yourself 'Fatso' :p
• 09-07-2002
Dr. Bebop
This works better and is easier to understand.
Code:

```#include <iostream> #include <iomanip> #include <string> using namespace std; float grossT; void print_header(); void print_footer(); void display_grossF(); // Defining the stock structure struct stock {         // Variables         int quantity;         string name;         float price;         float total[6];                 // Functions         void calc_stock(int j);         void display(int i); }; void stock::calc_stock(int j) {         total[j] = (price * quantity);         grossT += total[j]; } void stock::display(int i) {         cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);         cout << setw(15) << name << setw(15) << quantity << setw(20) << price << setw(20) << total[i] << "\n"; } void print_header() {         cout << setw(15) << "Stock Item" << setw(15) << "Quantity" << setw(20) << "Unit Price" << setw(20) <<"Total Value\n";         cout << "----------------------------------------------------------------------\n"; } void print_footer() {         cout << "----------------------------------------------------------------------\n"; } void display_grossF() {         cout << setw(53) << "Gross Total: " << setw(17) << grossT << "\n"; } int main() {         stock item[6];         item[0].name = "Television";    item[0].price = 368.00;         item[1].name = "Video Recorder"; item[1].price = 268.00;         item[2].name = "Camera";        item[2].price = 300.00;         item[3].name = "DVD";            item[3].price = 468.00;         item[4].name = "Hi Fi Stereo";  item[4].price = 2500.00;         item[5].name = "Video CD";      item[5].price = 345.00;                 // Receive quantities.         for (int counter = 0; counter < 6; counter++)         {                 cout << "Please enter the number of " << item[counter].name << "s: ";                 cin >> item[counter].quantity;         }         cout << "\n";                 for (int j = 0; j < 6; j++ )                 item[j].calc_stock(j);         // Display formatted data.         print_header();         for (int i = 0; i < 6; i++ )                 item[i].display(i);         print_footer();         display_grossF();         cout << "\n\n";         return 0; }```
Bebop