#include <iostream>
#include <windows.h>
using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
usingnamespace std;
int main( )
{
// create a string to encrypt
char string[ ] = "this is a secret!";
cout << "Original string is: " << string << endl;
encrypt( string );
// call to the function encrypt( )
cout << "Encrypted string is: " << string << endl;
decrypt( string );
ofstream encode;
encode.open ("encoded.txt");
encode << string;
encode.close();
// call to the function decrypt( )
cout << "Decrypted string is: " << string << endl;
ofstream decode;
decode.open ("decoded.txt");
decode << string;
decode.close();
Sleep(10000);
return 0;
}// main
//encrypt data
void encrypt (char e[] )
{
for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt
//decrypt data
void decrypt( char * ePtr )
{
for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
}
It gives me an error: "18 C:\Users\Mathijs\Documents\C++\test7.cpp aggregate `std::ofstream encode' has incomplete type and cannot be defined " & "24 C:\Users\Mathijs\Documents\C++\test7.cpp aggregate `std::ofstream decode' has incomplete type and cannot be defined "
Wow, I'm gone remove my profile because of shame...
Thanks Man!
There is no shame in not getting it right from the start. I'm also new to C++ and there is so much to think about. Can't get it all to be perfect first time around, right? =)
when defining a standard array you always have to say how big it will be, so the compiler knows how much memory to reserve for it.
You would do this as such:
char string[100];
Which tells the compiler reserve memory for 100 items of type character (100 bytes).
Make the array large enough for it tohold the filename (easiest option) or read the size of the input file using ios::end and .tellg() ( http://cplusplus.com/reference/iostream/istream/tellg/ ) to determine the size of the input.
Little bit more patience Mathijs :)
There are more people than yourself waiting for answers.
Coming to think of it - you dont't need to use that string[] variable, just do:
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
getline (read_input,line); //input is already read into variable "line"
cout << "Original string is: " << line << endl;
encrypt( line );
// call to the function encrypt( )
cout << "Encrypted string is: " << line << endl;
ofstream encode;
encode.open ("encoded.txt");
encode << line;
encode.close();
decrypt( line );
// call to the function decrypt( )
cout << "Decrypted string is: " << line << endl;
ofstream decode;
decode.open ("decoded.txt");
decode << line;
decode.close();
Also, "string" is not a valid name for a variable because it has the same name as the datatype.
Choose variable names carefully, to represent their contents, in this case "inputstring" for example.