Split main.cpp into a header file and main.cpp

Hello, everyone. I need to make a header file with 3 functions, "generating new key", "encryption" and "decryption". Then, just include this in the main.cpp so that it works the same way, can you help me?

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
  #include<iostream>
#include<string.h>
 
using namespace std;
 
int main(){
    char msg[] = "ATTACKATDAWN";
    char key[] = "LEMON";
    int msgLen = strlen(msg), keyLen = strlen(key), i, j;
 
    char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
 
    //generating new key
    for(i = 0, j = 0; i < msgLen; ++i, ++j){
        if(j == keyLen)
            j = 0;
 
        newKey[i] = key[j];
    }
 
    newKey[i] = '\0';
 
    //encryption
    for(i = 0; i < msgLen; ++i)
        encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
 
    encryptedMsg[i] = '\0';
 
    //decryption
    for(i = 0; i < msgLen; ++i)
        decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
 
    decryptedMsg[i] = '\0';
 
    cout<<"Original Message: "<<msg;
    cout<<"\nKey: "<<key;
    cout<<"\nNew Generated Key: "<<newKey;
    cout<<"\nEncrypted Message: "<<encryptedMsg;
    cout<<"\nDecrypted Message: "<<decryptedMsg;
 
	return 0;
}
Hello propvgvnda,


I need to make a header file with 3 functions


You do not put functions in a header file. I am thinking along the lines of "Functions.cpp", for the functions, "proto.hpp", for the prototypes of the functions and "main.cpp" for the main program.

I am thinking that the 3 sections:

//generating new key

//encryption

//decryption


are to be your 3 functions.

I have not worked on this yet, but this is my initial thought:

Proto.hpp
1
2
3
4
5
6
#ifndef PROTO_HPP
#define PROTO_HPP

void GenerateNewKey();  // <--- The return value and the parameters may need to change.

#endif // !PROTO_HPP 


And Functions.cpp
1
2
3
4
5
6
7
8
#include <iostream>

#include "Proto.hpp"

void GenerateNewKey()
{
    // <--- Add code from "main" here.
}


My only question is why do you need to use "string.h" and C-strings when a "std::string" will work better?

I would discus where you define "i" and "j", but for the moment with your code it works.

Andy
I don't know how to implement this with only strings.
What C++ compiler are you using - as this is invalid standard C++

 
char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen]


The size of an array must be able to be determined by the compiler at compile time. strlen() is a run-time function.

If you're using C++17, then

1
2
3
int msgLen = strlen(msg), keyLen = strlen(key), i, j;
 
char newKey[std::size(msg)], encryptedMsg[std::size(msg)], decryptedMsg[std::size(msg)];


Hello propvgvnda,


That is to bad that you do not know about "std::string"s yet. It makes it much easier.

The first problem: char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];. This is known as a VLA, (variable length array), and is not allowed in C++ and not allowed by most newer compilers. A few very old compilers may allow this though.

What is in the []s needs to be a constant number like 12 or a variable defined as a constant like:
constexpr int MSGLEN{ 12 }; or you could just "const".

If you have a need for using char newKey[msgLen] you could use "magLen" to create a dynamic array using the "new" key word. But do not forget to include "delete" to free the memory before the program ends.

Before I can continue I need to know if you know how to create a dynamic array.

It just hit me. You say char newKey[msgLen] which would create an array of 12 elements, but you did not allow for the "\0" at the end. what you would need is: char newKey[msgLen + 1] to allow for the '\0' at the end.

Using "std::string"s your code would change to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string msg{ "ATTACKATDAWN" };
std::string key{ "LEMON" };
size_t i, j;
 
std::string newKey, encryptedMsg, decryptedMsg;

//generating new key
for (i = 0, j = 0; i < msg.size(); ++i, ++j)
{

    if (j == key.size())
        j = 0;

    newKey += key[j];
}

Here you do not have to worry about sizes or arrays, but you can still access the string the same as you would an array.

Something to think about and if you want some information:
http://www.cplusplus.com/reference/string/string/
https://en.cppreference.com/w/cpp/string
https://www.learncpp.com/cpp-tutorial/4-4b-an-introduction-to-stdstring/
or the home page https://www.learncpp.com Very useful.

I will try working with what you started with.

Andy
std::size(msg) includes the terminating \0
Topic archived. No new replies allowed.