First program structure questions.

I've recently taken a passive/hobby interest in learning programming, and after a (barely) successful Hello World, I've decided to make a basic cipher encryption/decryption executable.
Right now I'm cannibalizing other people's public code found on forums to help me learn it and I'll write it from scratch later when I have a better understanding.

What I want the program to do is to be in a folder with a .txt file, referred to as "Text.txt", that can be written to by calling the "encrypt" function followed by the message which would immediately write it to the Text.txt file. Then I want to be able to call the "decrypt" function and have it do the opposite, and turn the coded message back into english.

Right now I'm using a basic caesar shift cipher which works well on stand alone, but I don't really know how to define it in a function to work with an fstream function, or that's what I should be doing at all.

I'm not asking you to write this for me, rather to show me how to YOU would personally structure a program like this, and also point out any noob mistakes, if you would be so kind. I've been reading the tutorials on the site, and they're helpful to me, but I learn much better by seeing other people's work.

I don't really have much background with C++, though I have used lua functions some in the past, do they work in the same basic way?

Thanks for any help you can give.

(Edit: Feel a bit stupid, one of the issues I was having was something elementary I missed.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>

using namespace std;
char caesar( char );
int main(int argc, char *argv[])
{    
    	std::string in;

	while ((std::cin >> in) && (in != "exit"))
	{

	}
	return 0;
} 

{

int encrypt ()
{
    string input;
    do {
        getline(cin, input);
        string output = "";
        for(int x = 0; x < input.length(); x++)
        {
            output += caesar(input[x]);
        }
        cout << output << endl;
    } while (!input.length() == 0);
}*/

char caesar( char c )
{
    if( isalpha(c) )
    {
        c = toupper(c);
        c = (((c-65)+13) % 26) + 65;
    }
    return c;
}
Last edited on
Here is a basic way to do an encryption ( easy to crack ) but I would break it up to multiple functions I am just being lazy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//c++98 v1
#include <iostream>

int main()
{
    std::string input;
    std::cout << "Please enter a sentence.\n> " << std::flush;
    std::getline( std::cin , input );
    
    for( int i = 0; i < input.size(); ++i ) ++input[ i ];
    std::cout << "The encrypted form is: " << input << std::endl;
    for( int i = 0; i < input.size(); ++i ) --input[ i ];
    std::cout << "The decrypted form is: " << input << std::endl;
    return( 0 );
}

//c++98 v2
#include <iostream>

int main()
{
    std::string input;
    std::cout << "Please enter a sentence.\n> " << std::flush;
    std::getline( std::cin , input );
    
    for( std::string::iterator it = input.begin(); it != input.end(); ++i ) ++input[ i ];
    std::cout << "The encrypted form is: " << input << std::endl;
    for( std::string::iterator it = input.begin(); it != input.end();  ++i ) --input[ i ];
    std::cout << "The decrypted form is: " << input << std::endl;
    return( 0 );
}

//c++11
#include <iostream>

int main()
{
    std::string input( "" );
    std::cout << "Please enter a sentence.\n> " << std::flush;
    std::getline( std::cin , input );
    
    for( auto &it : input ) ++it;
    std::cout << "The encrypted form is: " << input << std::endl;
    for( auto &it : input ) --it;
    std::cout << "The decrypted form is: " << input << std::endl;
    return( 0 );
}


Edit maybe check out this thread I made another basic encryption progrma way back in the day
http://www.cplusplus.com/forum/beginner/100936/#msg542610

also if you want to output input to a file try fstream/ifstream/ofstream

1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <iostream>

int main()
{
    std::ifstream in( "Text.txt" ); //input stream
    std::ofstream out ( "Text.txt" ); //output if you want to append to end of file instead of rewriting use this "Text.txt" , std::ios::app 
    std::string input;
    in >> input;
    std::cout << input << std::endl;
    out << "I am being outputted!" << std::endl;
}
Last edited on
Thanks giblit, combined with what I just learned browsing another thread this will be very helpful.
I'm not really worried about encryption strength, this is just a proof of concept.
Topic archived. No new replies allowed.