Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Ive been literally spending days on this project x_x; May I have some help, please?

  1. May 5, 2008 #1
    My final exam is tuesday and I am having an extremely extremely hard time grasping arrays and strings. I just cant visualize how they work and I am stuck on how to get my printOwnedShares function to work properly in a project that will help me study for the final. This is what I have worked on (its taking me over 30 hours and Ive been trying really hard):

    Code (Text):

    #include <iostream>
    #include <cstdlib>
    #include <string>

    using namespace std;

    // PROTOTYPE FUNCTIONS
    void printMenu(string name[], double price[], int size);
    int getCompany(string name[], double price[], int size);
    double getShares(string name);
    double calculateCost(char stock, int shares);
    double calculateCommission(double shares, double cost);
    void printPurchase(double shares, double cost,
                        double commission, string name);
    bool anotherTransaction();
    void printSummary(int purchases, double total_cost,
                                   double total_commission);
    // Project 8
    double getPrice (string name);
    void getMenu (string name[], double price[], int size);
    void printOwnedShares (string name[], double shares[], int size);

    int main ( )
    {

        // SET PRECISION
         cout.setf(ios::fixed, ios::floatfield);
         cout.precision(2);
         cout.setf(ios::showpoint);

        /* **********************************
        Declare Symbolic Constants
        ********************************** */
         const int MAX = 8;

        /* **********************************
        Declare Variables
        ********************************** */
        int stock;
        bool transaction;
        double cost, commission;
        /* ********* PROJECT 8 */
        int index;
        double price[MAX];
        double shares[MAX];
        string name[MAX];

        int current_size = 1;
        int purchases = 0;
        double total_cost = 0.0;
        double total_commission = 0.0;

        /* **********************************
        Input
        ********************************** */
         cout << "Welcome to FT's Stockbroker Program." << endl;

         // PROJECT 8
         getMenu(name, price, MAX);

         // ANOTHER TRANSACTION
          transaction = anotherTransaction();
            while(transaction == true)
            {
              // NUMBER OF PURCHASES
              purchases = purchases + 1;

         // RETURN CHOICE OF COMPANY
          stock = getCompany(name, price, MAX);

         // RETURN NUMBER OF SHARES
          if (stock == 1)
            shares[index] = getShares(name[0]);
          else if (stock == 2)
            shares[index] = getShares(name[1]);
          else if (stock == 3)
            shares[index] = getShares(name[2]);
          else if (stock == 4)
            shares[index] = getShares(name[3]);
          else if (stock == 5)
            shares[index] = getShares(name[4]);
          else if (stock == 6)
            shares[index] = getShares(name[5]);
          else if (stock == 7)
            shares[index] = getShares(name[6]);
          else
            shares[index] = getShares(name[7]);

          // PRINT CURRENTLY OWNED SHARES
          printOwnedShares(name, shares, current_size);
              current_size = current_size + 1;

        /* **********************************
        Compute
        ********************************** */
         // CALCULATE COST OF PURCHASE
          cost = shares[index] * price[index];
          total_cost = total_cost + cost;

         // CALCULATE COMMISSION
          commission = calculateCommission(shares[index], cost);
          total_commission = total_commission + commission;

        /* **********************************
        Output
        ********************************** */
         // PRINT DETAILS OF PURCHASE
          printPurchase(shares[index], cost, commission, name[index]);

         // UPDATE FOR TRANSACTION WHILE LOOP
              transaction = anotherTransaction();
            }

         // TOTAL PURCHASES, COST, AND COMMISSION
          printSummary(purchases, total_cost, total_commission);

         cout << "Thank you for using (my name)'s Stockbroker." << endl;

    // Return that program executed successfully
        return EXIT_SUCCESS;
    }

    void printMenu(string name[], double price[], int size)
    {
        int index;

        cout << "Here is a menu of stocks you can purchase:" << endl;
        for (index = 0; index < size; index++)
        {
          cout << "Enter " << index + 1 << " for " << name[index] << endl;
        }

        return;
    }

    int getCompany(string name[], double price[], int size)
    {
        int stock;

        do
        {
          // PRINT MENU OF STOCKS
          printMenu(name, price, size);
          cin >> stock;
        } while (stock < 1 || stock > 8);

        return stock;

    }

    double getShares(string name)
    {
        double shares;
        int index;

        do
        {
          cout << "How many shares of " << name
               <<  " would you like to buy? " << endl;
          cin >> shares;
        }while (shares < 0);

        return shares;
    }

    double calculateCommission(double shares, double cost)
    {
        const double HUNDRED_OR_LESS_COMMISSION = 9.99;
        const double THOUSAND_OR_LESS_COMMISSION = 19.95;
        const double THOUSAND_OR_MORE_COMMISSION = .025;

        double commission;
        int index;

        if(shares < 100)
        {
          commission = HUNDRED_OR_LESS_COMMISSION;
        }
        else
        {
          if(shares >= 1000)
          {
            commission = THOUSAND_OR_MORE_COMMISSION * cost;
          }
          else
          {
            commission = THOUSAND_OR_LESS_COMMISSION;
          }
        }

        return commission;

    }

    void printPurchase(double shares, double cost,
                        double commission, string name)
    {
        int index;

        cout << "The cost of " << shares << " shares of "
             << name << " is $" << cost << "." << endl;
        cout << "The commission paid is $" << commission << endl;

        return;
    }

    bool anotherTransaction()
    {
        char transaction;

        do
        {
          cout << "Would you like to buy another stock (y/Y/n/N)?" << endl;
          cin >> transaction;
            transaction = tolower(transaction);
        }while (!(transaction == 'y' || transaction == 'n'));

        if(transaction == 'y')
        {
          transaction = true;
        }
        else
        {
          transaction = false;
        }

        return transaction;
    }

    void printSummary(int purchases, double total_cost,
                                   double total_commission)
    {
        cout << "You have made " << purchases << " purchases." << endl
             << "The total cost is $" << total_cost << "." << endl
             << "The total commission is $" << total_commission << "."
             << endl;

        return;
    }

    // PROJECT 8
    double getPrice (string name)
    {
        double price;
        int index;

        do
        {
        cout << "Enter the price of a " << name
             << " share." << endl;
        cin >> price;
        } while (price < 0);

        return price;
    }

    void getMenu(string name[], double price[], int size)
    {
        int index;

        for (index = 0; index < size; index++)
        {
          cout << "Enter the name of company " << index + 1 << endl;
          cin >> name[index];
          price[index] = getPrice (name[index]);
        }

        return;

    }

    void printOwnedShares(string name[], double shares[], int size)
    {
        int index;

        cout << "You currently own: " << endl;
          for (index = 0; index < size; index++)
          {
           cout << shares[index] << " shares of " << name[index] << endl;
          }

        return;
    }
     
    and this is a sample run:

    What I want is for the "you currenly own:" part to print the correct shares corresponding to the correct name of the company. I think I need an if/else statement instead of the for statement or I am not using it correctly in the first place. I want more than anything to be able to understand this :cry:
     
    Last edited by a moderator: May 22, 2012
  2. jcsd
  3. May 8, 2008 #2
    you need to pass to getMenu by reference because as it is right now that function doesn't do anything for the calling program. change that arguments to be (string& name[], double& price[], int& size)
     
  4. May 8, 2008 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You misunderstand C++ semantics. The original poster's function is correct -- yours doesn't even compile!

    In the original poster's prototype, the first argument was declared as string name[], so it is a pointer to a string. When getMenu is called, a copy of the first argument is stored in name -- which is exactly what we want, because the first argument is a pointer to the first memory location of the array of data we want to set.

    You attempted to declare name as a pointer to a strong reference, which doesn't make sense, because reference types aren't stored in memory; you cannot point to them!

    You could have made the first argument string *&name -- but there is no good reason: you do not need to change the value of the first argument to the function (which is the address in memory of the 0th element of the array). Similarly, there's no reason to pass size by reference, because the function doesn't change its value.
     
  5. May 8, 2008 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    For the opening poster...

    Code (Text):

     // RETURN NUMBER OF SHARES
          if (stock == 1)
            shares[index] = getShares(name[0]);
          else if (stock == 2)
            shares[index] = getShares(name[1]);
          else if (stock == 3)
            shares[index] = getShares(name[2]);
          else if (stock == 4)
            shares[index] = getShares(name[3]);
          else if (stock == 5)
            shares[index] = getShares(name[4]);
          else if (stock == 6)
            shares[index] = getShares(name[5]);
          else if (stock == 7)
            shares[index] = getShares(name[6]);
          else
            shares[index] = getShares(name[7]);
     
    Is there any particular reason why you made this a giant chain of if's? And I'm having trouble finding in your code what index is supposed to be.

    I also noticed this line:
    Code (Text):
    } while (stock < 1 || stock > 8);
    which doesn't make any sense to me.

    Incidentally, did you bother to tell your compiler to emit warnings? And did you pay attention to them?



    Anyways, the problem with your output is that it's not adding the purchased shares to the right stock, right? It seems the obvious thing to do is to ensure that you've got the correct stock, and ensure you're adding those shares to the correct total. Have you looked at those parts of your code? Have you reviewed those parts of your program to make sure you're giving the right commands? Have you inserted debugging statements to ensure that they're behaving the way they're supposed to behave?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Ive been literally spending days on this project x_x; May I have some help, please?
Loading...