hi, I am trying to make a calculator and I need the stringstream function to convert a a certain part of a string.
string n1 = "243+79";
int n2 = 0;
stringstream(n1[0-2]) >> n2; // this converts the 243 from n1 to an int Any solutions?
hi, I am trying to make a calculator and I need the stringstream function to convert a a certain part of a string.
string n1 = "243+79";
int n2 = 0;
stringstream(n1[0-2]) >> n2; // this converts the 243 from n1 to an int Any solutions?
A quick clarification and some practical options.
Using the subscript operator (the s[i] form) returns a single character — it does not take a range. An expression like n1[0-2] evaluates 0-2 first (yielding a negative value converted to an unsigned index) and so is undefined behavior. The usual, robust approach is: locate the operator in the string, parse the left part as an integer, then parse the right part.
A modern, allocation-free way (C++17+) is std::from_chars:
#include <charconv>
#include <iostream>
#include <string>
#include <system_error>
std::string s = "243+79";
const char* begin = s.c_str();
const char* end = begin + s.size();
long long lhs = 0;
auto r1 = std::from_chars(begin, end, lhs);
if (r1.ec != std::errc()) { /* handle parse error */ }
if (r1.ptr == end) { /* no operator */ }
char op = *r1.ptr++;
long long rhs = 0;
auto r2 = std::from_chars(r1.ptr, end, rhs);
if (r2.ec != std::errc()) { /* handle parse error */ }
std::cout << lhs << op << rhs << '\n'; If you prefer C-style parsing or need pointer-based tracking, strtol gives an endptr you can inspect:
const char* c = s.c_str();
char* endptr = nullptr;
long a = std::strtol(c, &endptr, 10);
char op = *endptr++;
long b = std::strtol(endptr, nullptr, 10); To remove parts of a string in-place use erase (no allocation for a new string):
auto pos = s.find_first_of("+-*/");
if (pos != std::string::npos) s.erase(pos, 1); // remove operator Notes and tradeoffs: std::from_chars is fast and predictable (no exceptions). std::stoi throws on bad input (C++11), stringstream (as shown by ) is simple and readable but slower. strtol (as suggested) works well in C-style code. As hinted, extract the substring around the operator dynamically (use find_first_of) instead of hardcoding indices. Always validate input (whitespace, unary minus, overflow) before using the parsed numbers.
Jump to Post— Nick Evan 4,005Have a look at the substr() function
Jump to Post— L7Sqr 227You can also think about
strtolas a way to parse known good input.
You can also think about strtol as a way to parse known good input.
Also, i need a function that can remove parts of a string.
std::string asd="234+34";
char plus;
int first,second;
std::stringstream (asd) >> first >> plus >> second; Also, i need a function that can remove parts of a string.
That's what the substr() function does.
string n1 = "243+79";
string first = n1.substr(0,3);
string second = n1.substr(4,2); In other words string.substr(starting_index, number_of_characters);
Ok thanks for the help.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.