C++ Xecryption Code Help

C++ Xecryption Code Help

Post by Smeezinator on Thu Sep 11, 2008 4:51 pm
([msg=11417]see C++ Xecryption Code Help[/msg])

I am learning C++ and was trying to make an Xecryption program but it's not working and I can't figure out why. Here's the code:

Code: Select all
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

//Variables
string tstr, pstr;
char tcha[1000], pcha[20], answer;
int key, aspch, num1, num2, num3, i, intarray[1000];
bool running=true;

//Randomizing function
signed int rmz(void)
   {srand((unsigned)time(0));
    int random;
    int lowest= -10, highest= 10;
    int range= (highest-lowest)+1;
    random = lowest + int(range*rand() /(RAND_MAX + 1.0));
    return random;}
   
//Encrypting function
void charfunc (char tcha, int key)
   {for (i=0; tcha[i] != '\0'; i++)
        {intarray[i]=(int)tcha[i];
         num1=((intarray[i]+key)/3)+rmz();
         num2=((intarray[i]+key)/3)+rmz();
         num3=((num1+num2)-intarray[i])+key;
         cout << '.' << num1 << '.' << num2 << '.' << num3;}}
        
        
int main()
{do
{cout << "Type or paste some text to encrypt (under 1000 characters): " << endl;
   cin >> tcha;
   cout << endl << "Now enter the password (under 20 characters): ";
   cin >> pcha;
   for (i=0; pcha[i] != '\0'; i++)
      {aspch=(int)pcha[i];
       key+=aspch;}
   charfunc(tcha, key);
   cout << endl << endl << "Encrypt more text? [y/n] ";
   cin >> answer;
   if (answer='y')
      break;
   else
     return 0;
      running=false;}
   while (running=true);}


and here are the errors I get when compiling:

Code: Select all
xecryption.cpp: In function ‘void charfunc(char, int)’:
xecryption.cpp:24: error: invalid types ‘char[int]’ for array subscript
xecryption.cpp:25: error: invalid types ‘char[int]’ for array subscript
xecryption.cpp: In function ‘int main()’:
xecryption.cpp:41: error: invalid conversion from ‘char*’ to ‘char’
xecryption.cpp:41: error:   initializing argument 1 of ‘void charfunc(char, int)’


I have fixed other errors before, but I don't really get what these ones mean... :?: :?: :?:
Thanks for helping!
Smeezinator
New User
New User
 
Posts: 20
Joined: Sat Jun 28, 2008 5:22 pm
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by BhaaL on Sat Sep 13, 2008 5:07 am
([msg=11553]see Re: C++ Xecryption Code Help[/msg])

I suggest you work on your coding style a bit. That source code is hard to read, apart from being filled with lots of mistakes.
For example, comparison in C-style languages is done with ==, not =. = will still yield a true result, as the assignment worked.
You use parameters that have the same name as globals, whilst the globals serve no valid purpose to be global at all. Other than that, they dont even have the same types (see your first error message).
Passing the wrong stuff into your charfunc fails to cast the types to the requested ones, see your second error message.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by leonidas_heaven on Sat Sep 13, 2008 6:28 am
([msg=11561]see Re: C++ Xecryption Code Help[/msg])

Moreover,you have done some invalid conversion like you have declared :
char tcha[1000], pcha[20], answer;
int key, aspch, num1, num2, num3, i, intarray[1000];

Here aspch is int & pcha[20] is a char array..
you are doing
aspch=(int)pcha[i];

thats invalid and because of this you are getting error.
xecryption.cpp:41: error: invalid conversion from ‘char*’ to ‘char’

You cant simply change char into integer by just putting (int) before.
Instead of this you can take aspch in a char array..because in c++ char can store integers and aplhabets..

Bhaal is right try to write the program in a decent manner so that everyone can easily understand whats going on..
Challenges are not meant to be taken easily,rather they are meant to be taken Seriously
leonidas_heaven
Experienced User
Experienced User
 
Posts: 66
Joined: Sun Jun 01, 2008 5:20 am
Location: Uttarakhand,India
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by BhaaL on Sat Sep 13, 2008 7:28 am
([msg=11565]see Re: C++ Xecryption Code Help[/msg])

Wrong, pcha[i] takes the i'th character of pcha and the (int) takes the char code. This line is perfectly fine.
In case you didnt notice, line 41 is charfunc(tcha, key);
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by leonidas_heaven on Sat Sep 13, 2008 8:09 am
([msg=11568]see Re: C++ Xecryption Code Help[/msg])

Oops ,My mistake..
But the line i am talking is the place where conversion is taking place from char 2 int (line.41)
The way i am mentioning is also right i think..
Challenges are not meant to be taken easily,rather they are meant to be taken Seriously
leonidas_heaven
Experienced User
Experienced User
 
Posts: 66
Joined: Sun Jun 01, 2008 5:20 am
Location: Uttarakhand,India
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by BhaaL on Sat Sep 13, 2008 9:30 am
([msg=11580]see Re: C++ Xecryption Code Help[/msg])

Char to int is perfectly fine, its the same anyways internally.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by leonidas_heaven on Sat Sep 13, 2008 9:37 am
([msg=11582]see Re: C++ Xecryption Code Help[/msg])

When i make programs in Turbo c++ i have also handled the error like this and they are normally shown when i try to assign a char value to a integer one.And the same error is shown in the (line 41).
Challenges are not meant to be taken easily,rather they are meant to be taken Seriously
leonidas_heaven
Experienced User
Experienced User
 
Posts: 66
Joined: Sun Jun 01, 2008 5:20 am
Location: Uttarakhand,India
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by BhaaL on Sat Sep 13, 2008 11:10 am
([msg=11597]see Re: C++ Xecryption Code Help[/msg])

He doesnt assign a char to an integer variable, did you even read the source?
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by Smeezinator on Sat Sep 13, 2008 11:35 am
([msg=11605]see Re: C++ Xecryption Code Help[/msg])

Thanks for the help guys!
So what I understand so far is:
1) = should be == when checking (which I knew that was just a dumb mistake)
2) I have variables that are the wrong type
3) I have global variables that are unnecessary? (could you explain that a bit more please?)
4) if converting char to int isn't ok (which I thought it was) what should I do instead?
5) And my code is hard to read... To fix that should I add more comments, or change the order of stuff, or what?

Thanks again for all your replies!

EDIT: Also, is the charfunc function unnecessary? Like, would it be simpler just to put it in the main function?
Smeezinator
New User
New User
 
Posts: 20
Joined: Sat Jun 28, 2008 5:22 pm
Blog: View Blog (0)


Re: C++ Xecryption Code Help

Post by BhaaL on Sat Sep 13, 2008 12:18 pm
([msg=11610]see Re: C++ Xecryption Code Help[/msg])

Smeezinator wrote:2) I have variables that are the wrong type

Parameters in particular. charfunc expects char, where you pass in char*
Smeezinator wrote:3) I have global variables that are unnecessary? (could you explain that a bit more please?)

To be exact: all of them.
You could easily declare them in main, and pass them as parameters. Only use globals when you have to.
Smeezinator wrote:4) if converting char to int isn't ok (which I thought it was) what should I do instead?

It isnt a problem, converting 'A' to int gives you 65, which is the ascii code of it. Its prefectly fine, so I don't see a problem in that.
Smeezinator wrote:5) And my code is hard to read... To fix that should I add more comments, or change the order of stuff, or what?

Mainly watch your indentation and bracket placement.
Code: Select all
int main()
{int i; do{
  i++;
} while(i < 10);
}

What does that do, whats inside the loop, whats outside?
Code: Select all
int main()
{
  int i;
  do
  {
    i++;
  } while(i < 10);
}

Much more clear to read. Opening bracket on same line or next is personal preference, for some reason i use both depending on language (C#, next line. Pretty much all others, same line).

Smeezinator wrote:EDIT: Also, is the charfunc function unnecessary? Like, would it be simpler just to put it in the main function?

The part inside the loop is fine, but the loop itself shouldnt be there. You pass in a char, so work on that char (what you do is working on the global array, which shouldnt be the case).
Hence I say most (if not all) global variables are unnecessary. Move them to main, and see if it still works.
Fix errors that occur after that.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests