Reputation: 1157
I have the following lines of code:
vector<string> c;
string a;
for(int i=0;i<4;i++){
cin>>a;
c.push_back(a);
}
If I provide input as:
120$,132$,435$,534$
How can I extract the integer values separately and add them up to get the total value?
Upvotes: 1
Views: 7814
Reputation: 409196
You can use e.g. std::getline
with a custome "line" separator using the comma, strip the last character from the string (the '$'
) and use std::stoi
to convert to an integer:
std::vector<int> c;
for (int i = 0; i < 4; i++)
{
std::string a;
std::getline(std::cin, a, ',');
a = a.substr(a.length() - 1); // Remove trailing dollar sign
c.push_back(std::stoi(a));
}
Edit: Using std::accumulate
:
int sum = std::accumulate(c.begin(), c.end(), 0);
Edit 2: Using std::strtol
instead of std::stoi
:
The function std::stoi
is new in the latest C++ standard (C++11) and it not supported in all standard libraries yet. Then you can use the older C function strtol
:
c.push_back(int(std::strtol(a.c_str(), 0, 10)));
Upvotes: 4
Reputation: 13320
You can use regex and streams:
#include <regex>
#include <iostream>
#include <sstream>
const std::string Input("120$,132$,435$,534$");
int main(int argc, char **argv)
{
const std::regex r("[0-9]+");
int Result = 0;
for (std::sregex_iterator N(Input.begin(), Input.end(), r); N != std::sregex_iterator(); ++N)
{
std::stringstream SS(*N->begin());
int Current = 0;
SS >> Current;
Result += Current;
std::cout << Current << '\n';
}
std::cout << "Sum = " << Result;
return 0;
}
Output:
120
132
435
534
Sum = 1221
If you must ensure that the number is followed by a '$'
then change the regex to: "[0-9]+\\$"
the stringstream
part will ignore the trailing '$'
in the number conversion:
#include <regex>
#include <iostream>
#include <sstream>
const std::string Input("120$,132$,435$,534$,1,2,3");
int main(int argc, char **argv)
{
const std::regex r("[0-9]+\\$");
int Result = 0;
for (std::sregex_iterator N(Input.begin(), Input.end(), r); N != std::sregex_iterator(); ++N)
{
std::stringstream SS(*N->begin());
int Current = 0;
SS >> Current;
Result += Current;
std::cout << Current << '\n';
}
std::cout << "Sum = " << Result;
return 0;
}
Output:
120
132
435
534
Sum = 1221
Upvotes: 2
Reputation: 45410
A simple version:
int getIntValue(const std::string& data)
{
stringstream ss(data);
int i=0;
ss >> i;
return i;
}
int getSum(std::vector<std::string>& c)
{
int sum = 0;
for (auto m = c.begin(); m!= c.end(); ++m)
{
sum += getIntValue(*m);
}
return sum;
}
Done
Upvotes: 1
Reputation: 153929
If the input isn't too large (and particularly if it comes as a single
line), the simplest solution is to pack it all into a string, and parse
that, creating a std::istringstream
to convert each of the numeric
fields (or using boost::lexical_cast<>
, if by some odd chance it has
the appropriate semantics—it normally does when translating a
string to a built-in numeric type). For something this simple, it's
possible, however, to read directly from a stream, however:
std::istream&
ignoreDollar( std::istream& stream )
{
if ( stream.peek() == '$' ) {
stream.get();
}
return stream;
}
std::istream&
checkSeparator( std::istream& stream )
{
if ( stream.peek() == ',' ) {
stream.get();
} else {
stream.setstate( std::ios_base::failbit );
}
return stream;
}
std::vector<int> values;
int value;
while ( std::cin >> value ) {
values.push_back( value );
std::cin >> ignoreDollar >> checkSeparator;
}
int sum = std::accumulate( values.begin(), values.end(), 0 );
(In this particular case, it might be even simpler to just do everything
in the while
loop. The manipulators are a generally useful technique,
however, and can be used in a wider context.)
Upvotes: 1