can someone help me with making function for my program

Hi guys,
As you guys can probably guess, i'm new to c++ and for the most part, to programming as well. This is my first real program in terms of usefullness.
The goal of this program is for the user to input an 8 character password and have it encrypted. I am trying to make a function change the characters of the string into the ansi equivalent in numbers (a=65 for example), then change the value of the number by 5 or 6, then return them as the alpha numeric equivalent in ansi. thus 'abc' would turn into 'fgh'.
My problem lies in making a function do this. The problem is that it's not recognizing the function as a function. It keeps saying that ansi forbids making a character a number when i type cast it. if you want to see what my function is then here it is:

(very sloppy and newbie looking... probably isn't even a function!)


#include

int main ()

encryp(char encrypted[9]) //start function
{
int a=(int)pswd[0]; //making elements of the password into numbers
int b=(int)pswd[1];
int c=(int)pswd[2];
int d=(int)pswd[3];
int e=(int)pswd[4];
int f=(int)pswd[5];
int g=(int)pswd[6];
int h=(int)pswd[7];
int pwd[9]
a++; //changing char of the password by one
b++;
c++;
d++;
e++;
f++;
g++;
h++;
pwd[0]=a; //putting this back into an alpha form
pwd[1]=b;
pwd[2]=c;
pwd[3]=d;
pwd[4]=e;
pwd[5]=f;
pwd[6]=g;
pwd[7]=h;
} //end function
// start program
{
char pswd[9];
char encrypswd[9]=pwd[9];
char decrypswd[9]=pswd[9];
cout << "Please enter a password of 8 characters... :";
cin >> pswd;
encryp (pswd[9]);
cout

Comments

  • : Hi guys,
    : As you guys can probably guess, i'm new to c++ and for the most part, to programming as well. This is my first real program in terms of usefullness.
    : The goal of this program is for the user to input an 8 character password and have it encrypted. I am trying to make a function change the characters of the string into the ansi equivalent in numbers (a=65 for example), then change the value of the number by 5 or 6, then return them as the alpha numeric equivalent in ansi. thus 'abc' would turn into 'fgh'.
    : My problem lies in making a function do this. The problem is that it's not recognizing the function as a function. It keeps saying that ansi forbids making a character a number when i type cast it. if you want to see what my function is then here it is:
    :
    : (very sloppy and newbie looking... probably isn't even a function!)
    :
    :
    : #include
    :
    : int main ()
    :
    : encryp(char encrypted[9]) //start function
    : {
    : int a=(int)pswd[0]; //making elements of the password into numbers
    : int b=(int)pswd[1];
    : int c=(int)pswd[2];
    : int d=(int)pswd[3];
    : int e=(int)pswd[4];
    : int f=(int)pswd[5];
    : int g=(int)pswd[6];
    : int h=(int)pswd[7];
    : int pwd[9]
    : a++; //changing char of the password by one
    : b++;
    : c++;
    : d++;
    : e++;
    : f++;
    : g++;
    : h++;
    : pwd[0]=a; //putting this back into an alpha form
    : pwd[1]=b;
    : pwd[2]=c;
    : pwd[3]=d;
    : pwd[4]=e;
    : pwd[5]=f;
    : pwd[6]=g;
    : pwd[7]=h;
    : } //end function
    : // start program
    : {
    : char pswd[9];
    : char encrypswd[9]=pwd[9];
    : char decrypswd[9]=pswd[9];
    : cout << "Please enter a password of 8 characters... :";
    : cin >> pswd;
    : encryp (pswd[9]);
    : cout << "This is your encrypted password: " << encrypswd[9];
    : cout << "This is your decrypted password: " << decrypswd[9];
    : return 0;
    : }
    :
    :
    :


    oops! forgot to add a question.
    what is the problem with the function?






  • : I am trying to make a function change the characters of the string into the ansi equivalent in numbers (a=65 for example), then change the value of the number by 5 or 6, then return them as the alpha numeric equivalent in ansi. thus 'abc' would turn into 'fgh'.

    You don't "change a character into a number", a character already [italic]is[/italic] number. For instance, 'A' + 1 is 'B'.

    : My problem lies in making a function do this. The problem is that it's not recognizing the function as a function.

    That's because you've declared 'encryp' inside of 'main'. C++ does not allow nested functions. Here's a reworked version of your code that implements your simple encryption scheme:
    [code=ffffff]
    [color=a020f0] #include [/color][color=bb0000][/color]

    [color=000080]char[/color]* encrypt([color=000080]char[/color]* buf, [color=000080]size_t[/color] len) {
    [color=000000][b]for[/b][/color]( [color=000080]size_t[/color] i=[color=bb0000]0[/color]; i<len; ++i )
    ++buf[i];
    [color=000000][b]return[/b][/color] buf;
    }

    [color=000080]char[/color]* decrypt([color=000080]char[/color]* buf, [color=000080]size_t[/color] len) {
    [color=000000][b]for[/b][/color]( [color=000080]size_t[/color] i=[color=bb0000]0[/color]; i<len; ++i )
    --buf[i];
    [color=000000][b]return[/b][/color] buf;
    }

    [color=000080]int[/color] main() {
    [color=000080]char[/color] line[[color=bb0000]80[/color]];
    std::cin.getline(line, [color=000000][b]sizeof[/b][/color](line));
    std::cout << [color=bb0000]"Encrypted: "[/color] << encrypt(line, strlen(line)) << [color=907050]'
    '[/color];
    std::cout << [color=bb0000]"Decrypted: "[/color] << decrypt(line, strlen(line)) << [color=907050]'
    '[/color];
    [color=000000][b]return[/b][/color] [color=bb0000]0[/color];
    }
    [/code]
    Now that you've seen how this works, here's a variation on the same theme that's very easy to implement, and should really scramble the hell out of the message. I don't know what you would call it, because I just made it up. ;)

    Let the user choose a integer 'key'.

    To encrypt:
    (1) Seed the random number generator (srand) with the 'key'.
    (2) Add a random number from 0 to 127 (rand()%128) to each character in the buffer.

    To decrypt:
    (1) Seed the random number generator with the 'key'.
    (2) Subtract a random number from 0 to 127 to each character in the buffer.

    Now, implement this in C++. It's due on my desk by Monday. ;)

    For extra credit, let the user enter an arbitrary password and convert into an integer to seed the random number generator with.

    Cheers,
    Eric

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories