if you absolutely must use a std::string
(for any other reason.. perhaps homework?) then you can use the std::stringstream
object to convert it from a std::string
to int
.
std::stringstream strstream(num);
int iNum;
num >> iNum; //now iNum will have your integer
Alternatively, you can use the atoi
function from C to help you with it
std::string st = "12345";
int i = atoi(st.c_str()); // and now, i will have the number 12345
So your program should look like:
vector<string> num;
string holder;
int num_int, product[10];
cout << "enter numbers";
for(int i = 0; i < 10; i++){
cin >> holder;
num.push_back(holder);
}
for(int i =0; i<10; i++){
product[i] = atoi(num[i].c_str()) * 5; //I need the int value of num*5
}