I am trying to write a program that compresses text. For example, it would change aaaa
to a^4
.
For whatever reason, my compiler thinks that I am trying to overload a function when I am not. Here is my code (I apologize if it's long):
Code:
#include <iostream>
#include <string>
using namespace std;
string compress(string str);
int main()
{
string input;
string str = "gggxxx";
cout << str;
cout << compress(str);
cin >> input;
}
string compress(string str)
{
//Adds space to the end and front of a string
str = " " + str + " ";
string final = str;
int repeatingcharnum = 0;
char relavantchar = ' ';
string replacestr = "";
string placeholder = "";
bool flag = false;
for (int i = 1; i < str.length(); i++)
{
char lastchar = str[i - 1];
char currentchar = str[i];
if (lastchar == currentchar)
{
relavantchar = currentchar;
repeatingcharnum++;
flag = true;
} else if (lastchar != currentchar && flag)
{
flag = false;
//cout << repeatingcharnum << endl;
//cout << "relavant:" <<relavantchar << endl;
string replacestr = "";
for (int x = 0; x < repeatingcharnum; x++)
{
replacestr += relavantchar;
}
placeholder = relavantchar + "^" + repeatingcharnum;
final.replace(replacestr, placeholder);
replacestr = "";
placeholder = "";
repeatingcharnum = 0;
relavantchar = ' ';
}
}
return final;
}
Output:
1>------ Build started: Project: Compression, Configuration: Debug Win32 ------
1>Compression.cpp
1>C:\Users\Admin\source\repos\Compression\Compression\Compression.cpp(27,23): warning C4018: '<': signed/unsigned mismatch
1>C:\Users\Admin\source\repos\Compression\Compression\Compression.cpp(47,50): error C2661: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::replace': no overloaded function takes 2 arguments
1>Done building project "Compression.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I am programming in C++ using Visual Studio 2019 Community Edition.