Unable to integer value of hard code in vector

advertisements

I'm trying to self-study about vectors using C++.

As far as i know, vector is much more dynamic and efficient as compared to array.

For my practice, i tried to store values of different datatype into the vector and printing those value from the vector.

So i started off by trying to hard-code some value into different datatype of vector such as integer, string and double.

So after some research i attempted to hard code some integer and double value into the vector as shown below.

std::vector<int> stockID = {023}; 

std::vector<double> stockPrice;
stockPrice.push_back("50.50");

std::vector<int> stockChange =  {100}; 

std::vector<int> transactionDate = {151212};

However it returns me an error called conversion of int/double to non-scarlar type.

However when i attempt to hard code value into a vector string it doesn't seem to have any problem.

std::vector<string> stockItem;
stockItem.push_back("Samsung Phone");

std::vector<string> category;
category.push_back("Electronic");

std::vector<string> subCategory;
subCategory.push_back("Mobile Phone");

May i ask how does the string vector works differently from int and double vector to produce such error.


As pointed by whiZTiM in comment section of question.

Anything quoted in "" is a string-literal in C++. Your stockPrice only stores double, and there is no such implicit conversion from a string-literal to double

If you want to in vector stockPrice. It should be like

stockPrice.push_back(50.50);

Instead of

stockPrice.push_back("50.50");