My program keeps having to shut down

Hello,
I dont get any errors while compiling, but after entering the first text i get "This program has encountered an error and must be closed", prompting me to send an error report per usual, and i have absolutely no clue about what might be wrong, the only thing i could think of would be the size_t operator,
Here comes the code:
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//One-Time Pad A-Z
#include <iostream>
#include <new>


int encrypt(char input,char key)
{
    
	int output=(int(input)-64)+(int(key)-64);
    if(output>26){output=output-26;}       
    return output+64;  
}

int decrypt(char input,char key)
{
	int output=(int(input)-64)-(int(key)-64);
    if(output<0){output=output+26;}       
    return output+64;  
}

int main()
{
using namespace std;
char* input;
char* key;
char* output;
char cVal;
bool loop=true;
     while(loop)
     {
           system("cls");
     cout
     <<"Encrypt    (1)" <<endl
     <<"Decrypt    (2)" <<endl
     <<"Quit       (0)" <<endl;
     cin>>cVal;
     switch(cVal)
                          {
                           case '1':
                           {
                           cout<<"Enter massage to be encrypted: "<<endl;
                           cin>>input;
                           cout<<"Enter encryption key: "<<endl;
                           cin>>key;
                           size_t size_of_input = sizeof input;
                           size_t size_of_key = sizeof key;
                           if(size_of_input>size_of_key)
                           {
                           cout<<"WARNING: KEYLENGHT SHOULD BE EQUAL OR GREATER THEN INPUT LENGH;";
                           cout<<"SOME PARTS OF YOUR INPUT WILL NOT BE ENCRYPTED";
                           }
                           
                           for(int r=0;r<size_of_key;r++)
                           {
                           output[r]=encrypt(input[r],key[r]);        
                           }
                           for(int r=0;r<size_of_input;r++)
                           {
                           cout<<output[r];     
                           }
                           system("pause");
                           }
                           break;
                           
                           case '2':{cout<<"decrypt";}break;
                           
                           case '0':
                           {
                           loop=false;
                           break;
                           }
                           
                           default:
                           {cVal='4';}
                           break;
                           
                          }
     }
}

EDIT:: Forgot to say, im using Dev-C++ 4.9.9.2, with Mingw32 3.4.2.
Any help with this problem would be greatly appriciated,
Sincerely,
John
Last edited on
It looks like it is because you haven't assigned size/value/whatever output...so since the pointer is undefined, you will get undefined behavior when you try to access it as an array.
noticed it now, so i added
int size_of_input, size_of_key;
but it still doesnt work, however, when i tried to change the
char* input;

to
char input[255];

everything worked as expected, but the size of was returned at 255, which means that a lot of non-sense text was typed out as well, which was the reason to me wanting to use dynamic memory allocation, but doesnt the sizeof work on dynamic memory?

EDIT:: Problem Solved; I made a compromise; in short:
1
2
3
4
5
6
7
8
char input[255];
char key[255];

//and after the program recives the input
                           char* dynInput=input;
                           char* dynKey=key;
                           size_t size_of_input = sizeof dynInput+1;
                           size_t size_of_key = sizeof dynKey+1;


Last edited on
Topic archived. No new replies allowed.