0

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;
}
5
  • 1
    Don't you need `"\"\"" ? Commented Apr 7, 2016 at 3:47
  • Because C++ is a bit weird, string stockSymbol[] in a function declaration makes stockSymbol a pointer to a string, not an array of strings, even though it looks like an array of strings. Commented Apr 7, 2016 at 3:50
  • I assume you meant '"\"\""' and put that there, but now it says the same error above except as an int cannot be assigned to std::string. Commented Apr 7, 2016 at 3:51
  • @immibis Do you know how I could go about fixing that? Commented Apr 7, 2016 at 3:53
  • 1
    @ReyH Unfortunately, you can't pass an array to a function, full stop. The usual workaround is to pass a pointer to the start of the array, and the number of elements in the array. Note that that isn't the only problem with your code. Commented Apr 7, 2016 at 3:56

2 Answers 2

1

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().

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

2 Comments

Okay, thank you, this seemed to fix it and help me understand how I should have been coding the entries. And sorry, this was only a snid bit of the code, it's this big giant thing and I was trying to cut it down to what I thought was needed.
@ReyH it is good to cut away unrelated stuff but it is also important that the parts left behind make sense.
0

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;

Comments

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.