Reputation: 428
So, I'm programming a Run-Length-Decoder to practice my C++ Skills and when attempting to build and test my RLE Program I received this error:
44 | std::stringstream dig(next);
| ^~~~
| |
| char
In file included from src/rle.cpp:3:
/usr/include/c++/9/sstream:756:45: note: initializing argument 1 of 'std::__cxx11::basic_stringstream<_CharT, _Traits, _Alloc>::basic_stringstream(std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::ios_base::openmode = std::_Ios_Openmode]'
756 | basic_stringstream(ios_base::openmode __m)
| ~~~~~~~~~~~~~~~~~~~^~~
make[2]: *** [CMakeFiles/cpp-rle.dir/build.make:63: CMakeFiles/cpp-rle.dir/src/rle.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:112: CMakeFiles/cpp-rle.dir/all] Error 2
make: *** [Makefile:141: all] Error 2
Now I'm not super well verse in C++ but I understand that there is an issue with where I try to convert the sub-string of str into an integer std::stringstream dig(next);
but I'm not well-versed enough to understand why this is returning an error.
#include "rle.hpp"
#include <iostream>
#include <sstream>
std::string run_length_encode(const std::string& str)
{
std::string encoded_string = "";
char prev;
char next;
int count = 1;
prev = str[0];
for (int i = 0; i < str.length(); i++){
next = str[i];
if (prev == next){count++;}
else{
encoded_string += prev;
if(count > 1){
encoded_string += std::to_string(count);
}
prev=next;
count=1;
}
}
if (prev == next){
encoded_string += next;
if (count > 1){
encoded_string += std::to_string(count);
}
}
return {encoded_string};
}
std::string run_length_decode(const std::string& str)
{
// Implement!
std::string decoded_string = "";
char prev;
char next;
int count = 0;
prev = str[0];
for (int i = 0; i < str.length(); i++){
next = str[i];
if (isdigit(next)){
prev = str[i - 1];
std::stringstream dig(next);
dig >> count;
for (int j = 1; j < count; j++){
decoded_string += prev;
}
}
else{ decoded_string += next; }
}
return { decoded_string };
}
I posted the entirety of my program as well as the error so that all important information regarding my code is present. Below is also an example of my Inputs and expected Outputs so that you get an understanding of what I'm attempting to return.
Encoding:
WWWABC should be replaced with W3ABC.
WWWWBBWWWWW should be replaced with W4B2W5.
Decoding:
W3ABC should be replaced with WWWABC .
W4B2W5 should be replaced with WWWWBBWWWWW.
Upvotes: 0
Views: 485
Reputation: 60208
You don't need to use stringstreams
for this. This entire logic
if (isdigit(next)){
prev = str[i - 1];
std::stringstream dig(next);
dig >> count;
for (int j = 1; j < count; j++){
decoded_string += prev;
}
}
can be replaced by:
if (isdigit(next)){
decoded_string += std::string(next - '0', str[i - 1]);
This just uses the string constructor that constructs a string of length n of repeated characters.
Upvotes: 1
Reputation: 11400
next
is a char. You can't get a stringstream from a single char. You probably want:
count = next - '0';
if it is certain, at that point, that next
is between '0'
and '9'
.
Upvotes: 2