Encryption Errors Problem

a simple encryption program that is not working where the program is o

Page 1 of 1

13 Replies - 1512 Views - Last Post: 10 August 2007 - 09:15 PM Rate Topic: ***** 1 Votes

#1 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Encryption Errors Problem

Posted 10 August 2007 - 12:58 PM

Hi, I'm Leia. I am attempting to create a very simple encryption program, and my C++ compiler keeps giving me two errors:

"warning: multi-character character constant"
and
"warning: overflow in implicit constant converion"

The full code of my program is below, and includes the directives and what-not. Can someone please help me? I know that there is something wrong with my first array, since all my errors are generated from there, but am totally confused as to why I am getting errors and need help fixing them!!


#include <iostream.h>
#include <stdlib.h>

void main()
{
char code[30];
code[1] = '1';
code[2] = '2';
code[3] = '3';
code[4] = '4';
code[5] = '5';
code[6] = '6';
code[7] = '7';
code[8] = '8';
code[9] = '9';
code[10] = '10';
code[11] = '11';
code[12] = '12';
code[13] = '13';
code[14] = '14';
code[15] = '15';
code[16] = '16';
code[17] = '17';
code[18] = '18';
code[19] = '19';
code[20] = '20';
code[21] = '21';
code[22] = '22';
code[23] = '23';
code[24] = '24';
code[25] = '25';
code[26] = '26';

char encrypt[101];
int i = 1;
char character;
int characters_stored = 0;

cout << "Enter a character: ";
cin >> character;

while (character != 'S' && i <= 100)
{
encrypt[i] = character;
characters_stored = i;
i++;
cout << "Enter a character: ";
cin >> character;
}

cout << "\nYour message is: ";

for (i = 1; i <= characters_stored; i++)
{
 cout << code[encrypt[i]];
}

cout << endl << endl;

	  system("PAUSE");
	  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Encryption Errors Problem

#2 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:02 PM

#include <iostream>
#include <stdlib.h>

using namespace std;

void main()
{
char code[30];
code[1] = '1';
code[2] = '2';
code[3] = '3';
code[4] = '4';
code[5] = '5';
code[6] = '6';
code[7] = '7';
code[8] = '8';
code[9] = '9';
code[10] = '10';
code[11] = '11';
code[12] = '12';
code[13] = '13';
code[14] = '14';
code[15] = '15';
code[16] = '16';
code[17] = '17';
code[18] = '18';
code[19] = '19';
code[20] = '20';
code[21] = '21';
code[22] = '22';
code[23] = '23';
code[24] = '24';
code[25] = '25';
code[26] = '26';

char encrypt[101];
int i = 1;
char character;
int characters_stored = 0;

cout << "Enter a character: ";
cin >> character;

while (character != 'S' && i <= 100)
{
encrypt[i] = character;
characters_stored = i;
i++;
cout << "Enter a character: ";
cin >> character;
}

cout << "\nYour message is: ";

for (i = 1; i <= characters_stored; i++)
{
cout << code[encrypt[i]];
}

cout << endl << endl;

	  system("PAUSE");
}



iostream doesn't need the .h and your main was void and returning a value.. compiles now

I'll toss you in another direction to help.. this will fill up your initial character array for you although im not 100% sure what you are trying to do with the encryption.. but this is cleaned up some and compiles and does what it seems like it should:

#include <iostream>
#include <stdlib.h>

using namespace std;

int const CODE_MAX_SIZE = 30;
int const ENCRYPT_MAX_SIZE = 101;

void main()
{
	char code[CODE_MAX_SIZE];
	char encrypt[ENCRYPT_MAX_SIZE];
	char character;
	int i = 1;
	int characters_stored = 0;

	for(int nIndex = 0; nIndex < CODE_MAX_SIZE; nIndex++)
	{
		code[nIndex] = nIndex;
	}

	cout << "Enter a character: ";
	cin >> character;

	while (character != 'S' && i <= 100)
	{
		encrypt[i] = character;
		characters_stored = i;
		i++;
		cout << "Enter a character: ";
		cin >> character;
	}

	cout << "\nYour message is: ";
	
	for (i = 1; i <= characters_stored; i++)
	{
	cout << code[encrypt[i]];
	}

	cout << endl << endl;
	system("PAUSE");
}//end main


This post has been edited by Topher84: 10 August 2007 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:10 PM

It compiles properly now (thanks a LOT), but it is still not outputting anything. I am getting a blank output after "Your message is: ". Is there anything I can do about that?

View PostTopher84, on 10 Aug, 2007 - 01:02 PM, said:

#include <iostream>
#include <stdlib.h>

using namespace std;

void main()
{
char code[30];
code[1] = '1';
code[2] = '2';
code[3] = '3';
code[4] = '4';
code[5] = '5';
code[6] = '6';
code[7] = '7';
code[8] = '8';
code[9] = '9';
code[10] = '10';
code[11] = '11';
code[12] = '12';
code[13] = '13';
code[14] = '14';
code[15] = '15';
code[16] = '16';
code[17] = '17';
code[18] = '18';
code[19] = '19';
code[20] = '20';
code[21] = '21';
code[22] = '22';
code[23] = '23';
code[24] = '24';
code[25] = '25';
code[26] = '26';

char encrypt[101];
int i = 1;
char character;
int characters_stored = 0;

cout << "Enter a character: ";
cin >> character;

while (character != 'S' && i <= 100)
{
encrypt[i] = character;
characters_stored = i;
i++;
cout << "Enter a character: ";
cin >> character;
}

cout << "\nYour message is: ";

for (i = 1; i <= characters_stored; i++)
{
cout << code[encrypt[i]];
}

cout << endl << endl;

	  system("PAUSE");
}



iostream doesn't need the .h and your main was void and returning a value.. compiles now

I'll toss you in another direction to help.. this will fill up your initial character array for you:

#include <iostream>
#include <stdlib.h>

using namespace std;

int const MAX_SIZE = 30;

void main()
{
char code[MAX_SIZE];

for(int nIndex = 0; nIndex < MAX_SIZE; nIndex++)
{
	code[nIndex] = nIndex;
}

char encrypt[101];
int i = 1;
char character;
int characters_stored = 0;

cout << "Enter a character: ";
cin >> character;

while (character != 'S' && i <= 100)
{
encrypt[i] = character;
characters_stored = i;
i++;
cout << "Enter a character: ";
cin >> character;
}

cout << "\nYour message is: ";

for (i = 1; i <= characters_stored; i++)
{
cout << code[encrypt[i]];
}

cout << endl << endl;

	  system("PAUSE");
}


Was This Post Helpful? 0
  • +
  • -

#4 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:12 PM

I edited my previous post w/ some cleaned up code of what i wrote for you... Try it again i get output when it says "your message is"
Was This Post Helpful? 0
  • +
  • -

#5 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:14 PM

Yes that part works fine. But my program is supposed to output the code that I created in my first array, the one named code. That's the array where the problem started. But in the end, if I were to type in " abc " in different lines, it should say " Your message is 123 ", as according to the alphabet. How do I get that to work?

View PostTopher84, on 10 Aug, 2007 - 01:12 PM, said:

I edited my previous post w/ some cleaned up code of what i wrote for you... Try it again i get output when it says "your message is"

Was This Post Helpful? 0
  • +
  • -

#6 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:21 PM

So... what you are trying to do is you get the user to enter some numbers or letters and when they are done spit out the number equivalent in the alphabet or vice versa?
Was This Post Helpful? 0
  • +
  • -

#7 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:23 PM

I am trying to get a user to input characters and the program to encrypt them into the numbers equivalent in the alphabet.

BTW, thanks so much for helping me, and I like your website (it's cool!!)
Was This Post Helpful? 0
  • +
  • -

#8 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:24 PM

View Posttaiarget, on 10 Aug, 2007 - 01:23 PM, said:

I am trying to get a user to input characters and the program to encrypt them into the numbers equivalent in the alphabet.

BTW, thanks so much for helping me, and I like your website (it's cool!!)


ah ok i think i know what you want gimme a sec ... what is the maximum # of characters that the user can enter to encypt? 100?

This post has been edited by Topher84: 10 August 2007 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#9 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:29 PM

Yes, it is 100. Thanks so much for helping me!! (I have been puzzling over this by myself for a while now... :D )
Was This Post Helpful? 0
  • +
  • -

#10 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 01:51 PM

Here you go... Done! I just wrote an entirely new program instead! heh.. used a function to do the encryption.. There is a function called "toupper" which would take care of checking for whether or not a letter is upper case or lower case but i don't know how familiar you are with the functions so I just did it with or statements to help out some understanding.

#include <iostream>
#include <string>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int const MAX_SIZE = 100; //MAX NUMBER OF THINGS TO ENCRYPT

void vEncryptCode(char cArrayToEncrypt[], int& nNumCharacters); //ENCRYPTION FUNCTION DECLARATION
void main()
{
	char cArrayToEncrypt[MAX_SIZE]; //ARRAY OF WHATEVER TO ENCRYPT MAX SIZE OF 100
	string strCode;				 //INDIVIDUAL CODE ENTERED
	int  nNumCharacters = 0;		//NUMBER OF CHARACTERS


		cout << "Enter Code: ";	  //PROMPT FOR CODE
		cin  >> strCode;			 //ENTER CODE

		//FILL ARRAY WITH THE CODE
		for(int nIndex = 0; nIndex < strCode.size(); nIndex++)
		{
			cArrayToEncrypt[nIndex] = strCode[nIndex];
			nNumCharacters++;
		}//end for

		vEncryptCode(cArrayToEncrypt,nNumCharacters); //ENCRYPT THE CODE
}//end main

void vEncryptCode(char cArrayToEncrypt[], int& nNumCharacters)
{
	string strEncryption;

	for(int nIndex = 0; nIndex < nNumCharacters; nIndex++)
	{
		if(cArrayToEncrypt[nIndex] == 'a' || cArrayToEncrypt[nIndex] == 'A')
		{
			strEncryption += "1";
		}
		else if(cArrayToEncrypt[nIndex] == 'b' || cArrayToEncrypt[nIndex] == 'B')
		{
			strEncryption += "2";
		}
		else if(cArrayToEncrypt[nIndex] == 'c' || cArrayToEncrypt[nIndex] == 'C')
		{
			strEncryption += "3";
		}
		else if(cArrayToEncrypt[nIndex] == 'd' || cArrayToEncrypt[nIndex] == 'D')
		{
			strEncryption += "4";
		}
		else if(cArrayToEncrypt[nIndex] == 'e' || cArrayToEncrypt[nIndex] == 'E')
		{
			strEncryption += "5";
		}
		else if(cArrayToEncrypt[nIndex] == 'f' || cArrayToEncrypt[nIndex] == 'F')
		{
			strEncryption += "6";
		}
		else if(cArrayToEncrypt[nIndex] == 'g' || cArrayToEncrypt[nIndex] == 'G')
		{
			strEncryption += "7";
		}
		else if(cArrayToEncrypt[nIndex] == 'h' || cArrayToEncrypt[nIndex] == 'H')
		{
			strEncryption += "8";
		}
		else if(cArrayToEncrypt[nIndex] == 'i' || cArrayToEncrypt[nIndex] == 'I')
		{
			strEncryption += "9";
		}
		else if(cArrayToEncrypt[nIndex] == 'j' || cArrayToEncrypt[nIndex] == 'J')
		{
			strEncryption += "10";
		}
		else if(cArrayToEncrypt[nIndex] == 'k' || cArrayToEncrypt[nIndex] == 'K')
		{
			strEncryption += "11";
		}
		else if(cArrayToEncrypt[nIndex] == 'l' || cArrayToEncrypt[nIndex] == 'L')
		{
			strEncryption += "12";
		}
		else if(cArrayToEncrypt[nIndex] == 'm' || cArrayToEncrypt[nIndex] == 'M')
		{
			strEncryption += "13";
		}
		else if(cArrayToEncrypt[nIndex] == 'n' || cArrayToEncrypt[nIndex] == 'N')
		{
			strEncryption += "14";
		}
		else if(cArrayToEncrypt[nIndex] == 'o' || cArrayToEncrypt[nIndex] == 'O')
		{
			strEncryption += "15";
		}
		else if(cArrayToEncrypt[nIndex] == 'p' || cArrayToEncrypt[nIndex] == 'P')
		{
			strEncryption += "16";
		}
		else if(cArrayToEncrypt[nIndex] == 'q' || cArrayToEncrypt[nIndex] == 'Q')
		{
			strEncryption += "17";
		}
		else if(cArrayToEncrypt[nIndex] == 'r' || cArrayToEncrypt[nIndex] == 'R')
		{
			strEncryption += "18";
		}
		else if(cArrayToEncrypt[nIndex] == 's' || cArrayToEncrypt[nIndex] == 'S')
		{
			strEncryption += "19";
		}
		else if(cArrayToEncrypt[nIndex] == 't' || cArrayToEncrypt[nIndex] == 'T')
		{
			strEncryption += "20";
		}
		else if(cArrayToEncrypt[nIndex] == 'u' || cArrayToEncrypt[nIndex] == 'U')
		{
			strEncryption += "21";
		}
		else if(cArrayToEncrypt[nIndex] == 'v' || cArrayToEncrypt[nIndex] == 'V')
		{
			strEncryption += "22";
		}
		else if(cArrayToEncrypt[nIndex] == 'w' || cArrayToEncrypt[nIndex] == 'W')
		{
			strEncryption += "23";
		}
		else if(cArrayToEncrypt[nIndex] == 'x' || cArrayToEncrypt[nIndex] == 'X')
		{
			strEncryption += "24";
		}
		else if(cArrayToEncrypt[nIndex] == 'y' || cArrayToEncrypt[nIndex] == 'Y')
		{
			strEncryption += "25";
		}
		else if(cArrayToEncrypt[nIndex] == 'z' || cArrayToEncrypt[nIndex] == 'Z')
		{
			strEncryption += "26";
		}
	}//end for

	system("cls");
	cout << "Your Message Is: " << strEncryption;
	cout << "\n\nPress Any Key To End Program";
	getch();
}//end vEncryptCode


This post has been edited by Topher84: 10 August 2007 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#11 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 02:03 PM

Thanks so much!! But my program doesn't like the getch() at the end of the code and when I get rid of it, the program only takes one input. Can I do anything to fix this?

The error states: "implicit declaration of function 'int getchar(...)'
Was This Post Helpful? 0
  • +
  • -

#12 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 02:07 PM

Make sure you used all of the #includes that i have.... #include <conio.h> is what uses getch().. this is one way to do it c++... c doesn't have a getch()

For the 1 input thing... I made the program so that it takes ALL of the input at one time (up to 100 "codes" .. if you put more than 100 in it will crash!)

Sample Input (DO NOT USE SPACES):

Enter Code: abcdefghijlmnopqrstuv

I did it this way because the way you were doing it with 's' would be creating lots of problems for you


Note: if you DO put more than 100 things into your Enter Code: it will still display them but just crash at the end

This post has been edited by Topher84: 10 August 2007 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 taiarget  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-August 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 02:26 PM

Oooooooooooooooooooh :blink: Now I get it. It took a while, but thanks SOOOOO much. :P I'ld love to return the favour someday, but I'm probably not nearly as smart as you :D .

Thanks again,
Leia
Was This Post Helpful? 0
  • +
  • -

#14 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Encryption Errors Problem

Posted 10 August 2007 - 09:15 PM

np i like to help out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1