Problem with strings in Turbo C
Edit: I am sorry, I accidentally put this in C, rather than C++. Thank you for the person who moved this thread.
I was assigned a program to encrypt a string by inputting a word to be encrypted and inputting a code word. The encrypting function then calculates an average of the ascii values to subtract from the ascii value of each letter of the word. Then, we output the encrypted values. For example, inputting a word of potter and a code word of the ascii value of 1 would give onssdq (Alt 1). Now when I do this, it outputs the word but then crashes with a NTVDM illegal instruction issue. I would have tried changing this to a void, but we were given instructions to leave the functions as char*s.
Code:
//Pranay Orugunta
//9/12/11
//Mr Potter 8
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <iomanip.h>
int codenum(char* c1){ //codenum func
int sum=0;
int length=strlen(c1);
for (int i=0;i<length;i++){ //adds up to the sum
sum=sum+(int)c1[i];}
sum=sum/length;
return sum;} //returns sum
char* encrypt(){
cout<<"Please input the Phrase to be encrypted."<<endl;
char* word; //takes in word and code
cin>>word;
cout<<"Please input the code word."<<endl;
char* code;
cin>>code;
cout<<codenum(code)<<endl;
int len1=strlen(word);
int value; //init vars
char* ascii;
for (int j=0;j<len1;j++){
value=(int)word[j]-codenum(code); //finds ascii val and subtracts codenum
ascii[j]=(char)value;} //converts back to char from ascii
ascii[len1+1]='\0';
return ascii;}
char* decrypt(){
cout<<"Please input the encrypted phrase."<<endl;
char* w1;
cin>>w1; //takes in enc phrase and cnum
cout<<"Please input a code#."<<endl;
int cnum;
cin>>cnum;
char* realw;
int len2=strlen(w1);
int v1;
for (int k=0;k<len2;k++){
v1=(int)w1[k]+cnum; //ascii value + code num
realw[k]=(char)v1;} //converts back
realw[len2+1]='\0';
return realw;}
void main(){
clrscr();
cout<<"Please select 1) Word Encryption or 2) Word Decoding"<<endl;
int form; //asks for the form
cin>>form;
if (form==1){
cout<<encrypt()<<endl;} //goes to each function depending on form
if (form==2){
cout<<decrypt()<<endl;}
getch();}
Thank you