help me im a C++ noobie!!!

This is a discussion on help me im a C++ noobie!!! within the C++ Programming forums, part of the General Programming Boards category; hey hey hey with the following code which is pretty simple, it works fine except for the line: item[5].grossF(); this ...

  1. #1
    fatso
    Guest

    Unhappy 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
    void header()
    {
    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);

    header();

    // 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";
    }

  2. #2
    Frank_1974_@@
    Guest

    Smile

    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.

  3. #3
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    use code tags and read faqs! all newbies should
    code tags ar like this

    (code)
    code here
    (/code)

    replace ( and ) with [ and ]
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> 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'

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    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
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting text onto the next line...lol...noobie questions
    By JOlszewski in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 03:02 PM
  2. noobie maze program
    By stormfront in forum C Programming
    Replies: 9
    Last Post: 11-30-2005, 09:23 AM
  3. Noobie Programmer --> Am i in the right place ?
    By Seeker in forum Game Programming
    Replies: 6
    Last Post: 09-07-2005, 03:49 AM
  4. C++ program jumps for noobie
    By bandito9111 in forum C++ Programming
    Replies: 3
    Last Post: 03-01-2003, 12:48 AM
  5. another noobie question
    By noobie in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21