I have received cipher documents which are believed to be secret messages. So, my mission is to break the encoding and reveal what the secrets are that each file contains. However,it is believed that each file contains a message protected with a "null cipher" which surround the real characters in messages with one or more "null" characters. So, to reveal the message, I have to read in each character in the file, but only print out every other, or every third, or every fourth character.

I already built my project and it debugged successful. However, I can't decode the secret and I stuck right here. I am confusing and wondering that I have built it correctly or not. So please take a look over my project and give me your opinion. Thanks.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int main(int argc, char * argv[])
{
int c, count = atoi(argv[1]);
srand(time(NULL));
c = rand();
char b;
do
{
for (int i = 1; i < count; i++)
c = cin.get();
c = cin.get();
c = cin.get();
cout.put(c);
}while(!cin.eof());
}

Edited 5 Years Ago by dkXIII: n/a

Where is cin declared and initialized? I see no mention of a filename, or an attempt to open a file; how is your program going to access the file?

Also, I see no need for the randomizing of c.

Finally, why are you casting the characters you read into integers? If I understand the cipher, you'll want to keep them as characters, but just ignore some of them, right?

Welcome dkXIII,

First of all, when posting code, please put [ CODE ] tags around it. Simply select the code you've pasted into your message, and click the [ CODE ] icon/button at the top of the editor. And read the "Read Me: Read This Before Posting A Question" sticky-thread at the top of the forum.

Now then, you get count from the command-line, what is that value supposed to represent? A count of what? Maybe it should be named something more informative....

You assign c a random value (which you never use), and declare a char variable b (which you also never use). And it looks like you've messed up the nesting of your for() and do-while() loops (though that might have been a copy-and-paste error, if your code has already compiled).

Slow down and think about each step of your program. Make sure it does things in a logical progression. Start with big "things", and then break each "thing" into smaller "things", and repeat, until each "thing" corresponds to a relatively simple line of code. :)

Where is cin declared and initialized?

std::cin is an instance of istream, and is declared and initialized in the included <iostream> header.

Oh, of course. Thanks, Raptr. Still, since the OP refers to "documents" I imagine that he's going to want to open at least one file for processing, and not just get his input from the console input.

Thanks for replying. But I am still confused about using Null Cipher. I changed my code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int main(int argc, char * argv[])
{
	char c;			// declaring variable
	char count = atoi(argv[1]); // discards whitespace between characters
	srand(time(NULL)); 
	
	// start project
	do
	{
		for (c = 1; c < count; c++)
		{
			c = rand() % 26; 
		}
		c = cin.get();
		c = cin.get();
		c = cin.get(); // get the 3rd integer 
		cout.put(c);   // out put the 3rd interger
	}while(!cin.eof()); // print out the integer
}

And to access the file, I think I need to provide information on the Command Line of Microsoft Visual C++ Express: project -> properties -> debugging -> command argument. I am not sure because I keep getting error : "Unable to start program" while I can debug successfully.

As far as the command-line, the only argument your program looks for is the "count", and it doesn't check whether the argument is actually -on- the command-line. That might be where the "Unable to start program" error comes from -- if there's no command-line argument, argc will be 1, and argv will be an array with one element: ["yourProgramName.exe"], attempting to access argv[1] will probably result in an error.

As far as a file, you either need to "pipe" the contents of the file into your program (so you can use cin):

linux$ cat input_file.txt | myProgramName
windows> type input_file.txt | myProgramName.exe

or modify your program to hardcode the filename (or prompt the user to enter it) and then open a new file-object:

string filename("input_file.txt");
ifstream infile(filename.c_str());
...
c = infile.get();

As far as using the count variable, go back to your code and read out loud what it does. It should be evident that, while you haven't exhausted the input, you're generating a new random number (between 0 and 25, inclusive), count times in a row, and not using any of them, and then reading three characters and outputting the third. Since this is presumably not what you mean to be doing, say out loud (and write down on paper) what you -do- want to do, and then write code that matches what you've written. I'll get you started:

Get the number, count, of null-characters to skip each time (zero -> output all characters).
While the input isn't exhausted:
    read and print the first/next character
    read and skip count characters (this requires a nested loop)
This article has been dead for over six months. Start a new discussion instead.