Dear fellow members,
Im a first year physics student and learning my first programming language and this is C++.
We have an assignment where we are supposed to make a program that can compress text files in the following way:
aaaaabbbbbbbbb/////////
5555555555
(becomes)->
a5b9/9
/510
and the decoding part must do the opposite.
I already got the coding part down:
Code:
void coderen (ifstream & input, ofstream & output) {
int counter;
counter = 1;
char karo, karn;
karo = input.get(); //first character is fetched
while (! input.eof()){
karn = input.get(); //new character is fetched
if ((karo == karn) && !(karo == '\n')) {
counter ++;
}
else {
output.put(karo);
if (counter > 1) {
output << counter;
} //if
if ((karn >= '0') && (karn <='9')){
output << '\\';
}//if kar = a number, put a slash.
counter = 1;
} //else
karo = karn; //old character becomes new character
} //while not end of file
}//code
But now I`m trying to write the code for the decoding and I`m having some difficulties.
My idea was that I take the first character of the file i need to decode, now I take the following character and if this is a number I`ll put that information in a counter and look at the following character and if that is a number I`ll add that info to the counter untill I encounter a non-number, then I`ll use a loop to write the first non-number the number of times is given by the number after this character in the "to decode" file.
But the bad part is, it doesnt work the way I intended: a5 doesnt give me aaaaa but instead gives me a/5.
So I`m looking for advice on this piece of code, I appreciate every help I can get. Here is the decoding code
Code:
void decoderen (ifstream & input, ofstream & output) {
int counter, tempcounter;
counter = 0;
tempcounter = 0;
char karo, karn; //old character, new character
karo = input.get();
while (! input.eof()){
karn = input.get();
while ((karn >= '0') && (karn <= '9')){
if (karn = '0'){
tempcounter = 0;}
else if (karn = '1'){
tempcounter = 1;}
else if (karn = '2'){
tempcounter = 2;}
else if (karn = '3'){
tempcounter = 3;}
else if (karn = '4'){
tempcounter = 4;}
else if (karn = '5'){
tempcounter = 5;}
else if (karn = '6'){
tempcounter = 6;}
else if (karn = '7'){
tempcounter = 7;}
else if (karn = '8'){
tempcounter = 8;}
else {
tempcounter = 9;}
counter = counter*10 + tempcounter;
karn = input.get();
} //while
cout << tempcounter << endl;
cout << "counter after countingloop" << counter <<endl;
while (counter > 0){
output.put(karo);
counter = (counter-1);
} //while
cout << counter <<endl;
}// while
} //decode