I am using VC++6.0

I am attempting to implement the TEA for a Oracle server. I am plannng to write the algorithm in C++(C), then add it to a shared library.

I am new with C++ and I am having some difficlulty, mainly in two basic areas:

1. I want to extract, for test purposes from the keyborad the key and message. Howeve, I cout the prompt for key, then cout the prompt for message. I cin.getline(k,15,'\n') for the key and the same for the message. If I go over the key specified limit I am not able to enter the message at all. When I cout the key and message the key is returned fine 16 chars etc, but the message isn't even prompted to enter. I see what's going wrong, but I really need to know how to resolve it.

2. The second issue folows on from this. I want to accept an inputted message and key. I use an array to take the inputs. I then want to use the 32bit (4byte) pieces of the message and key for binary operations. So I want to take the eg 16 character key, and use the first 4 chars, then 2nd 4, etc..etc.. How do I do this...??? Also with the message???

Your compiler is new enough, you should be using the string class rather than C-style strings. That would sidestep the problem you're having entirely:

#include <string>

std::string key;
std::string msg;

std::cout<<"Enter the key: ";
if (!std::getline(std::cin, key)) {
  // Handle input error
}

std::cout<<"Enter the message: ";
if (!std::getline(std::cin, msg)) {
  // Handle the error
}

The problem you're having is identical to what you would see in C with fgets. The first call reads n-1 characters and returns without extracting a newline character, the second call read everything that was left. The effect is that it looks like the second call for input is skipped. The solution is also identical to what you would do in C (assuming you don't want to use the string class).

>use the 32bit (4byte) pieces of the message and key for binary operations
What binary operations are you planning on using?

I understand what you have told me, and thnks.

However, I am still having the same problem. I want the program to take the first 16 chars of anything entered as key. After hitting return(newline) I then want the cin buffer to be empty, ready to take an upto 1024 chars for the message. I want these two inputs to be divided into 32-bit (4 byte) chunks, ready for binary operations, such as Xor, Adding. Then at the end I'll en up with two 32bit pieces, which is effectively the envrypted mesage. Another issue I am having is how do I return this two piece message back as a single concatenated string.

This article has been dead for over six months. Start a new discussion instead.