Debugging Encoding/Decoding Program with White Space Issues

Click For Summary
The discussion centers on issues with a program designed for encoding and decoding strings, particularly when handling whitespace. The code successfully encodes a string without spaces but fails when spaces are included, leading to incorrect outputs during decoding. Suggestions include implementing logic to handle whitespace properly and utilizing a debugger to step through the code for troubleshooting. Additionally, there are recommendations for improving the algorithm and ensuring that the variable types are correctly defined, particularly for the choice variable. Overall, the conversation emphasizes the importance of algorithm design and debugging techniques in resolving these encoding issues.
magnifik
Messages
350
Reaction score
0
i am trying to write a program that encodes and decodes. my code works fine for encoding 1 string of characters with no spaces, but when there is any type of white space, it fails. also, when i try to decode, i get a random mix of characters and symbols. any help would be appreciated.

for example (using the keyword lemon)
input text file: attackatdawn
output text file (encode): lxfopvefrnhr // success!

input text file: attack at dawn
output text file (encode): lxfopvXmhaoeib // FAIL...the spaces should merely show up as spaces

input text file: lxfopvefrnhr
output text file (decode): aÂ#„åF§iÊ+Œí // FAIL...idk what this is

Code:
void perform(istream& inf, ostream& outf, string key, char choice)
{
	string text;
	while(getline(inf, text))
		{
		for (int i = 0; i < text.length(); i++)
		{
			if (key.length() <= text.length())
				key += key[i];
		}
		// encode
		if (choice == 'e')
		{
			for (int j = 0; j < key.length(); j++)
			{
				key[j] = tolower(key[j]);
				alpha_to_num(key[j]);
			}
			for (int k = 0; k < text.length(); k++)
			{
				text[k]= tolower(text[k]);
				alpha_to_num(text[k]);
			}
			for (int l = 0; l < text.length(); l++)
			{
				eForm(c, text[l], key[l]);
				num_to_alpha(c);
				outf << (char)c;
			}
		}
		// decode
		else if (choice == 'd')
		{
			for (int j = 0; j < key.length(); j++)
			{	
				key[j] = tolower(key[j]);
				alpha_to_num(key[j]);
			}
			for (int k = 0; k < text.length(); k++)
			{
				text[k]= tolower(text[k]);
				alpha_to_num(text[k]);
			}
			for (int l = 0; l < text.length(); l++)
			{
				dForm(c, text[l], key[l]);
				num_to_alpha(c);
				outf << (char)c;
			}
		}
	}
}

int main(){
	char choice;
	string key;
	string input;
	string output;
	ifstream inf;
	ofstream outf;
	 
        cin >> choice >> key >> input >> output;
	inf.open(input.c_str());
	outf.open(output.c_str());

	perform(inf, outf, key, choice);
}
 
Physics news on Phys.org
If you want spaces in plain text to show up as spaces in the encrypted text, you will need some logic in the encryption part of your code that doesn't encrypt spaces (and maybe other kinds of whitespace such as tabs and/or newline characters.

You should have in mind what your code will produce when it encrypts or decrypts a string. By that, I mean you should be spending more time on your algorithm and less time entering your code. If the plain text is "attack at dawn" and the key is "lemon" you should know before you run your program what the output should be, and should compare your actual output with what you are designing your program to do. The debugger is your friend - get to know how to use it to single step through your code.
 
http://www.daniweb.com/forums/thread257218.html"
 
Last edited by a moderator:
Mark44 said:
If you want spaces in plain text to show up as spaces in the encrypted text, you will need some logic in the encryption part of your code that doesn't encrypt spaces (and maybe other kinds of whitespace such as tabs and/or newline characters.

You should have in mind what your code will produce when it encrypts or decrypts a string. By that, I mean you should be spending more time on your algorithm and less time entering your code. If the plain text is "attack at dawn" and the key is "lemon" you should know before you run your program what the output should be, and should compare your actual output with what you are designing your program to do. The debugger is your friend - get to know how to use it to single step through your code.

i just discovered the debugger. i have a breakpoint in my code starting at the decoding part, but how do i step through it?
 
It probably depends on what debugger you're using. There should be some documentation or help or something for the debugger, and the docs would say how to single-step through your code.
 
i have run my debugger and it seems to show that an entire loop is not being iterated through. below is my modified code (not yet sure if the algorithm is correct). everything inside the while(inf.get(text)) does not get executed. any thoughts on why this is so?

Code:
int findTextSize(istream& inf)
{
	char text;
	int textSize = 0;
	while(inf.get(text))
	{
		textSize++;
	}
	return textSize;
}

// process the text from the file and do the actual encoding
void perform(istream& inf, ostream& outf, string& key, string choice)
{
	int textSize = findTextSize(inf);
	char text;
	for (int y = 0; y < textSize; y++)
	{
		if(key.size() <= textSize)
			key += key[y];
	}
	int inputIndex = 0;
	int keyIndex = 0;
	int i = 0;
	string alphabet = "abcdefghijklmnopqrstuvwxyz";
	while(inf.get(text))
	{
		if (!isalpha(text))
			outf << text;
		else
		{
			if (isalpha(text))
				text = tolower(text);
			for (int j = 0; j < alphabet.length(); j++)
			{
				if (text == alphabet[j])
					inputIndex = j;
				if (key[j] == alphabet[j])
					keyIndex = j;
			}
			if (choice == "e")
				outf << alphabet[(inputIndex - keyIndex) % 26];
			else if (choice == "d")
				outf << alphabet[(26 - inputIndex + keyIndex) % 26];
		}
		i++;
		i = i % key.length();
	}
}
 
I'm not familiar with c++, but I found this http://www.cplusplus.com/reference/iostream/istream/get/

Code:
// istream get
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  char c, str[256];
  ifstream is;

  cout << "Enter the name of an existing text file: ";
  cin.get (str,256);

  is.open (str);        // open file

  while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    if (is.good())
      cout << c;
  }

  is.close();           // close file

  return 0;
}

So from my guess, I would think you're not suppose to have anything in inf.get().

If that doesn't fix your problem, can you write in English what you want to do?
 
Last edited by a moderator:
The get method of istream has six overloads, one of which takes a reference to a char. See http://www.cplusplus.com/reference/iostream/istream/get/ for descriptions of these methods.

In your call to get you have
Code:
while(inf.get(text))
I think this is what you should be doing:
Code:
while(text = inf.get())

Also, as a minor point, you have
Code:
while(inf.get(text))
{
   if (!isalpha(text))
      outf << text;
   else
   {
      if (isalpha(text))
      ...
   }
There is no need for the first line in your else clause, if (isalpha(text)). To get to that point in your code, isalpha(text) must already have returned true (or rather, 1), so you don't need to test for it again with an if statement.

As for your algorithm, it needs some work. You shouldn't be testing to determine the value of choice for each character you decrypt or encrypt. You should do that once, not for each iteration of your while loop.

After you have determined that choice == 'e' or choice == 'd', you should carry out the operation that is needed.

BTW, you have define choice as a string, which is probably not the right thing to do. Since it holds only a single character (I think), it should be declared as char, not string. That brings me to an error you have by declaring choice as a string. This line,
Code:
if (choice == "e")
does not do what you think it will do, with choice declared as a string. Instead of evaluating to true if choice contains the letter e, it evaluates to true if the address of choice is the same as the address of the string "e". That's definitely NOT what you want. You cannot compare the contents of two strings using == in C or C++.

Finally, you should get into the habit of inserting comments in your code. There is only one comment in the last code you provided. Notice the comments in the code that sourlemon provided. Not only do comments help other readers understand what your code is trying to do, but they help you remember why you are trying to do something. Start from an algorithm in pseudocode, and then lay out your C++ code, with comments for each step you had in your pseudocode.
 
Last edited by a moderator:

Similar threads

  • · Replies 23 ·
Replies
23
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K