2 Replies - 502 Views - Last Post: 08 July 2013 - 01:19 PM Rate Topic: -----

#1 biotech44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-July 13

Clarification with text encoding project re: 'code' & '

Posted 08 July 2013 - 12:40 PM

Okay, hi I've been working on my first project for C++. Anyway, it's a text encoding one. Also, this is my first time using this website.

The directions are to complete the code and sum functions. I've done some work on them, but would ask if someone could check my work as I believe I left out some of the steps.. Thanks.

Project info:
"Input: A key character followed by the characters of a string to be encoded. The first character of the string is the key character.
Output: The key character, the characters of the encoded message, and a four-decimal-digit checksum that is the sum modulo 10,000 of the output encoded characters. (The key character is not included in the checksum calculation). If the input string is empty, output is an empty string.
Algorithm: (Used separately on each character)
1. Get a character.
2. If the character code is zero or greater than 126, terminate encoding, generate the checksum, and append it to the output.
3. If the character code is in the range 1 through 32, output a caret (^) followed by a second character. Initially, the code for the second character is the code of the input character plus the code of the key character. If the resulting code value is greater than 126, then 32 is added modulo 126 to the initial value of the code for the second character.
4. If the code is anything else, generate the output character(s) as follows:
1. Subtract 33 from the character.
2. Add the code of the key character, modulo 94.
3. Reflect that value in the range 0 through 93.
4. Add 33.
5. Use the result as the output character code.
6. If the resulting code is the code for the caret character (^), also output the key character after the generated character."





So here's the skeleton code and then I started working on the code and sum functions. Please keep in mind that this is my second week with c++ so I'm still new..

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//  GLOBAL CONSTANT DECLARATIONS
	const int CHARACTER_OFFSET  = 33;	// Character offset
	const int CHARACTER_RANGE   = 93;	// Character range
	const int CHARACTER_MODULUS = 94;	// Character modulus
	const int CHECKSUM_MODULUS  = 10000;// Checksum  modulus
	const char SPECIAL_LIMIT    = ' ';	// Special character limit
	const char UPPER_LIMIT      = '~';	// Upper   character limit
	const char CARET            = '^';	// Caret character

	string	code (char key, char in);	// Encode a character			
	string	sum	(string	in);			// Generate a string checksum		

int main()
{
	bool	done;						// Done indicator
	char	key;						// Key character
	int		pos;						// Input line position
	string	iline;						// Input  line
	string	oline;						// Output line
	cout << "Input: String to be encoded " << endl;
	cout << "Output: Encoded string " << endl << endl;
	cout << "Empty string exits the program." <<endl;
	cout << endl;
	done = false;
	while (!done)
	{
		cout << "Input: ";
		getline(cin, iline);
		if (iline == "")				//If line is empty, set done to true
		{
			done = true;
		}
		else
		{
			key = iline[0];				// Extract the key character
			oline = "";					// Initilalize the output line

			for (pos = 1; pos < (iline[pos] != 0) && (iline[pos] <= UPPER_LIMIT) && (pos < iline.length()); pos++)
			{
				oline += code(key, iline[pos]); // Append the coded string for one character
			}

			oline += sum(oline);				// Append the output string checksum
			oline = key + oline;				// Prepend the output with the key character
			cout << "Output:" << oline << endl;	// Display the output line
			cout << endl;						// Display a blank line
		}
	}
	cout << endl;
	cout << "Done." << endl;
	return 0;
}
// CODE FUNCTION
// This function encodes one character.
// The return value is the string of encoded characters.
string	code							// Encode a character
		(char	key,					// Key character
		 char	in)						// Input character
{
	string	out = "";						// Output string

	// ***TO DO generate the encoded value of one character.***
    //If the character code is zero or greater than 126
        //terminate encoding
    //else
        // If the character code is in the range 1 through 32,
            //output a caret (^) followed by a second character.
            // Initially, the code for the second character is the code of the input
            // character plus the code of the key character.
            char t = in + key ^
            // If the resulting code value is greater than 126,
            // then 32 is added modulo 126 to the initial value
            // of the code for the second character.
            if (t > 126)
                t%=127
                t+=32
    
    //If the code is anything else, generate the output character(s) as follows:
else 
{
    //1.	Subtract 33 from the character. 
char (t = in - 33)
    //2.	Add the code of the key character, modulo 94. 
( t+=key; t%=94)
    //3.	Reflect that value in the range 0 through 93. 
(t = 93 - t)
    //4.	Add 33.
    //5.	Use the result as the output character code.
    //6.	If the resulting code is the code for the caret character (^), also output the key character after the generated character.
    //generate the checksum
    //append it to the output.
	
	return out;							// Return the encoded characters
}

// SUM FUNCTION
// This function generates a checksum for a text string.
// The return value is a string of four decimal digits that is the checksum.
string	sum								// Generate a string checksum
		(string	in)						// Input string
{
	string	out = "";						// Output string

	// ***TO DO generate a checksum for a text string.***
    for (int i=0; i<in.size(); i++)
    {
        sum += in[i];
    }
    sum &= 10000
    //convert sum to string
	return out;							// Return the checksum string
}




Is This A Good Question/Topic? 0
  • +

Replies To: Clarification with text encoding project re: 'code' & '

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4138
  • View blog
  • Posts: 12,877
  • Joined: 25-December 09

Re: Clarification with text encoding project re: 'code' & '

Posted 08 July 2013 - 12:57 PM

I would suggest you start by fixing your compile errors. It looks like you are waiting much too long before you compile your code. You should be compiling much more often, as early as after a one line addition, and fix any warnings or errors. By compiling often it'll be much easier to find and fix the errors because you know exactly where to start looking.

Jim

This post has been edited by jimblumberg: 08 July 2013 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Clarification with text encoding project re: 'code' & '

Posted 08 July 2013 - 01:19 PM

I agree with Jim.

Write little compile often should be your motto

You are missing a lot of ';' in your code...

I understand its your second week and things are tough but you should have that bit figured out by now.

I would suggest re-thinking it take one step at a time by that I mean one of the instructions in your list and get it perfect then compile error free and move onto the next
instruction.

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1