1

im doing an assignment on c++ and im stuck on how i would add a new transaction to my work, with a user defined numShares, and pricePerShare.

i have a transaction struct which looks like this:

struct Transaction
{
string stockSymbol;     // String containing the stock symbol, e.g. "AAPL"
string buyerName;       // String containing the buyer's name e.g. "Mr Brown"
int buyerAccount;       // Integer containing an eight digit account code
int numShares;          // Integer containing the number of sold shares
int pricePerShare;      // Integer containing the buy price per share
};

this is the buildTransaction class:

static Transaction* buildTransactions(int numTransactions)
{
    int maxShareVolume = 100000;
    int maxSharePrice = 1000;

    Transaction *transactions = new Transaction[numTransactions];

        for(int idx = 0; idx < numTransactions; idx++)
        {
            transactions[idx].stockSymbol = pickRandomStockSymbol();

            std::string buyerName = pickRandomBuyer();
            transactions[idx].buyerName = buyerName;
            transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);

            transactions[idx].numShares = 1 + rand() % maxShareVolume;
            transactions[idx].pricePerShare = 1 + rand() % maxSharePrice;       
        }

        return transactions;
    }

how would i use that to add data to the transactions array using this:

void Analyser::addTransactions(Transaction* transactions, int numTransactions)

i would assume from this that all i would really need to have as user input would be the amount of shares, and the price per share, but that the other information fills itself in automatically, from choosing from the arrays.

2
  • @Andrew Glass do you want us just to show how to operate with these declarations? Commented Mar 19, 2013 at 5:03
  • i just wanna know how i could use the way the buildTransaction class creates a new transaction so that when the addTransactions function is ran instead of outputting 10 seperate transactions it would output 11. Commented Mar 19, 2013 at 8:09

2 Answers 2

2

instead of using arrays, you should use vectors.. the buildTransactions would be written this way:

std::vector<Transaction> buildTransactions(int numTransactions)
{
    int maxShareVolume = 100000;
    int maxSharePrice = 1000;
    std::vector<Transaction> transactions;

    for(int idx = 0; idx < numTransactions; idx++)
    {
        Transaction t;
        t.stockSymbol = pickRandomStockSymbol();
        std::string buyerName = pickRandomBuyer();
        t.buyerName = buyerName;
        t.buyerAccount = lookupBuyerAccount(buyerName);
        t.numShares = 1 + rand() % maxShareVolume;
        t.pricePerShare = 1 + rand() % maxSharePrice;

        transactions.push_back(t);
    }

    return transactions;
}

by editting the buildTransactions function, you can easily add more data by doing this to your addTransactions function:

void Analyser::addTransactions(std::vector<Transaction> &transactions, int numTransactions)
{
    for(int idx = 0; idx < numTransactions; idx++)
    {
        Transaction t;
        t.stockSymbol = pickRandomStockSymbol();
        std::string buyerName = pickRandomBuyer();
        t.buyerName = buyerName;
        t.buyerAccount = lookupBuyerAccount(buyerName);

        std::cout << "Enter number of shares for transaction: ";
        std::cin >> t.numShares;
        std::cout << "Enter price per share for transaction: ";
        std::cin >> t.pricePerShare;

        transactions.push_back(t);
    }
}

hope it helps :)

Sign up to request clarification or add additional context in comments.

1 Comment

yeah that seems to be alright with a few tweeks of the code, the only thing im getting is when i try to call the buildTransaction in the main Transaction *transactions = Setup::buildTransactions(numTransactions); Transaction *moreTransactions = Setup::buildTransactions(numTransactions); i get the error "IntelliSense: a nonstatic member reference must be relative to a specific object" this is related to the "Setup::" in the call.
0

you can get amount of shares, and the price per share as inputs in a loop in addTransactions() method like the following:

for(int idx = 0; idx < numTransactions; idx++)
    {
        transactions[idx].stockSymbol = pickRandomStockSymbol();

        std::string buyerName = pickRandomBuyer();
        transactions[idx].buyerName = buyerName;
        transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);

        ctd::cout<<"Enter number of shares for transaction "<<idx+1<<std::endl;
        std::cin>>transactions[idx].numShares;
        ctd::cout<<"Enter price per share for transaction "<<idx+1<<std::endl;
        std::cin>>transactions[idx].pricePerShare;    
    }

3 Comments

thanks for your comment. but that doesnt add it thought that edits all of the ones in the array already without adding a new item to the array. what i want is say ive the numTransactions set to default 10. i want it so that if i run that class i can add an additional element to the array. so it would be an 11th item, but the stocksymbol, buyername and buyeraccount are just generated randomly so that all i need to update is the number of shares and the price, this would then print out a list of 11 elements?
Then you may need to re-size your array and this complicates the process. In this case, instead of using "Transaction* transactions" you can use "std::vector<Transaction> vTransactions" and give this vector as a parameter to addTransactions(std::vector<Transaction> &vTransactions) method and you can just call push_back your newly initialized trasaction object to your Transactions vector
how would i go about doing that? sorry for my ignorance, but im very new to this c++ stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.