How to convert std::string to int in C++ (without data loss)?
Hello everyone! I'm a beginner in C++ and writing a small program for inventory management. Data comes from a text file where the product price is stored as a string (e.g., "1500"). I need to perform arithmetic operations on these numbers: add prices, calculate discounts, etc. But the compiler gives an error when I try to add two strings.
Here is my example code:
```cpp
#include
#include
int main() {
std::string price1 = "2500";
std::string price2 = "1800";
int total = price1 + price2; // Error!
std::cout << "Total: " << total << std::endl;
return 0;
}
```
I understand that strings cannot be added like numbers, but how do I correctly convert std::string to int? I need the program to work without failures, even if the string contains garbage (na