Rey H
Rey H

Reputation: 3

Array string in function error

So, I need to salvage this large program for my assignment, but I cannot make sense of the error I'm getting for this string array in a function.
At the = of stockSymbol = """" I keep getting the error
'Error:a value type "const char *" cannot be assigned to an entity of type "std::string *"
I included where the string is defined and the function. Anyone have any ideas on what's happening and how to fix this?

int menu()

{
int actents = 0;

int opt = 0;

string stockSymbol[MAXENTS];

double stockShares[MAXENTS];

double stockPrice[MAXENTS];

int opt;

string opts;

void resetPortfolio(string stockSymbol[], double stockShares[], double stockPrice[], int & actents)

{
    // code logic to set all entries of the stock symbol array to ""
    stockSymbol = "\"\"";
    // code logic to set all entries of the other arrays to 0
    stockShares = 0;
    stockPrice = 0;
    // set the number of actual entries in the arrays (actents) to 0
    actents = 0;
    return;
}

Upvotes: 0

Views: 69

Answers (2)

Miles Budnek
Miles Budnek

Reputation: 30494

stockSymbol, stockShares, and stockPrice are all pointers to the first element of arrays. You cannot just assign to them to set the values of their elements. Instead you need to loop over the arrays and set the value of each element.

void resetPortfolio(string stockSymbol[], double stockShares[], double stockPrice[], int & actents)
{
    for (int i = 0; i < actents; ++i) {
        // code logic to set all entries of the stock symbol array to ""
        stockSymbol[i] = "";
        // code logic to set all entries of the other arrays to 0
        stockShares[i] = 0;
        stockPrice[i] = 0;
    }
    // set the number of actual entries in the arrays (actents) to 0
    actents = 0;
}

There are other problems with the code you posted. menu() is never closed, and you never actually call resetPortfolio().

Upvotes: 1

v7d8dpo4
v7d8dpo4

Reputation: 1399

Your function menu isn't closed.

string stockSymbol[MAXENTS];

double stockShares[MAXENTS];

double stockPrice[MAXENTS];

Are these supposed to be inside or outside menu?

"""" means the string "". If you write 2 strings together, it means the concatenation of the strings.

To set fill the arrays with the value,

for(var i=0; i<MAXENTS; i++)
    stockSymbol[i]="\"\"";
for(var i=0; i<MAXENTS; i++)
    stockShares[i]=0;
for(var i=0; i<MAXENTS; i++)
    stockPrice[i]=0;

Upvotes: 0

Related Questions