Decoding program help

This is a discussion on Decoding program help within the C++ Programming forums, part of the General Programming Boards category; Dear fellow members, Im a first year physics student and learning my first programming language and this is C++. We ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Decoding program help

    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

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Code:
    if (karn = '0')
    Those are all assignments, not comparisons.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    I hate the way that they always seem to give your first compression assignments using a text-based and ambiguous representation.
    E.g, what is 1234 supposed to decompress to:
    113333, or
    11111111111111111111111 and then four of whatever comes next, or
    two hundred and thirty four ones?


    When you find yourself repeating lines of code with no difference other than a number is incremented by one, then you're doing it wrong. Use simple calculations or look-up tables instead of code duplication.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    I`m obviously still such a beginner because of the karn = '0' instead of ==. Thanks for the help because now I can get get on with my assignment, saves me a lot of trouble. This board is very helpfull for me and you guys are doing us beginners a great favour but I`m not sure if it is ok to ask these beginner questions here. Is there maybe a board for super duper beginners like me?

    When you find yourself repeating lines of code with no difference other than a number is incremented by one, then you're doing it wrong. Use simple calculations or look-up tables instead of code duplication.
    What do you mean exactly by this? I know you are talking about my way to convert a character to a integer but I though this was a pretty simple way to do it. Could you give me some clues about a better way to do it as you implie?

    I hate the way that they always seem to give your first compression assignments using a text-based and ambiguous representation.
    E.g, what is 1234 supposed to decompress to:
    113333, or
    11111111111111111111111 and then four of whatever comes next, or
    two hundred and thirty four ones?
    Maybe I was not clear about this but a number like 1234 preceded by a character becomes 1234 times that character and a / followed by 1234 becomes 234 times a 1. You will never get something like 12121212 -> 12 x 6 because of the nature of the encoding as this is a very basic compressing algorithm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 08:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21