Data Encryption Program

Hello, I have a data encryption program I am trying to work on, and I am needing it to allow users to input 0's if they want to.

This is the last part of the program that I can't get to work...

Any suggestions are welcome!

I am only working with integers, and then the program is mainly functions in sequence.

-----------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

//Function Prototypes
//////////////////////////////////////////////////////
//Request Input Function
///////////////////////////////////////////////////
void requestInput()
{
cout <<"Welcome to the beginning of Data Encryption" << endl;//request user inputs four integers
cout<< "\nEnter a four digit integer: ";
return;
}
/////////////////////////////////////////////////
//Form integer function
////////////////////////////////////////////////
int formInt(int d1, int d2, int d3, int d4)
{
int encInt;
encInt = d1*1000 + d2*100 + d3*10 + d4;
if (d1 == 0)
encInt = d1 + d2*100 + d3*10 + d4;
return encInt;
}
//////////////////////////////////////////////////
//Display Integer Function
///////////////////////////////////////////////
void displayInteger(int d1, int NewInt)
{
if (d1 == 0)
{
cout<<"\nYour new integer is: 0";
cout<< NewInt << "\n";
}
else
cout<<"\nYour new integer is: "<< NewInt << "\n";
}

//////////////////////////////////////////////////
//Extract digit function
///////////////////////////////////////////////
void extractDigit(int x1, int x2, int x3, int x4)
{
cout<<"\nThe four digits I extracted, are: " << x1 << ", " << x2 << ", " << x3 << " & " << x4 <<"\n"; //displays extracted digits from the integer
}

int main()
{

int int4d, v, d1, d2, d3, d4;

requestInput();//request input

cin>> int4d;

cout<< "\nYou entered: " << int4d << "\nPress 1 if this is correct, 2 if this is incorrect: " ; cin>> v;

while (int4d > 9999 || int4d < 999 || v == 2 )
{
cout<<"\nPlease enter a four digit integer: "; cin>> int4d ;//ask user to input 4 digits

cout<<"\nYou entered: " << int4d << "\nEnter 1 if this is correct, 2 if incorrect: "; cin>> v;
//Verify user input
//*********************************************************************************************************************************************
while (v != 2 && v!= 1)
{
cout<<"\nPlease enter either 1 for correct, or 2 for incorrect: "; cin>> v;
}

while (v == 2)
{
cout<<"\nPlease enter a four digit integer: "; cin>> int4d ;

cout<<"\nYou entered: " << int4d << "\nEnter 1 if this is correct, 2 if incorrect: "; cin>> v;
}
//**********************************************************************************************************************************************
}
//Extract the four digits from the inputed 4 digit integer
d4 = int4d/1000;
d3 = (int4d%1000)/100;
d2 = (int4d%100)/10;
d1 = int4d%10;
//******

extractDigit(d1, d2, d3, d4);//call extract digit function



}


I can't get it to use zero if zero is the first input of the digits from the user.

I have deleted a lot of the code so that it will fit into this post, so I apologize if it doesn't run properly if you simply copy and paste these lines of code.
Proper formatting really helps. Since you didn't post all of your code, I cut out everything that didn't do anything.

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
#include <iostream>

using std::cout;
using std::cin;

int main ( )
{
	int int4d, v = 2;


	cout << "Welcome to the beginning of Data Encryption\n";


	do {
		cout<<"\nPlease enter a four digit integer: ";
			
		cin>> int4d;


		if ( int4d < 9999 && int4d > 999 )
		{
			cout << "\nYou entered: " << int4d << "\nEnter 1 if this is correct, 2 if incorrect: ";
			
			cin >> v;


			while ( v != 1 && v != 2 )
			{
				cout << "\nPlease enter either 1 for correct, or 2 for incorrect: ";
			
				cin >> v;
			}
		}

		else cout << "\nThat is not a four digit integer.\n";
	} while ( v == 2 );


	cout << "\nThe four digits I extracted, are: " << int4d / 1000 << ", "
		 << ( int4d % 1000 ) / 100 << ", " << ( int4d % 100 ) / 10
		 << " & " << int4d % 10 << '\n';
}



Note, this can still break. What happens if I enter the number, 'G'?
Last edited on
Hey, first, how do I post the code like you did so you can see the coloring, line numbers...etc? did you screen shot it?

Also, this code doesn't work. It wont even get down to your line of code because it isn't recognizing the 0 input. The code I gave you is important because I want my program to be all functions.
Is it possible for me to combine these numbers into one integer with 0 as the leading number? I would like to be able to use the user's input later on in the encryption/decryption process.
how do I post the code like you did so you can see the coloring, line numbers

Highlight your code and press the <> formatting button or manually bracket your code with
[code]...your code here...[/code]
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can go back and edit your post to apply code tags.

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

//Function Prototypes
//////////////////////////////////////////////////////
//Request Input Function
///////////////////////////////////////////////////
void requestInput() 
{
	cout <<"Welcome to the beginning of HW3: Data Encryption" << endl;//request user inputs four integers
	cout<< "\nEnter a four digit integer: ";
	return;
}


//////////////////////////////////////////////////////
//Request Input2 Function
///////////////////////////////////////////////////
void requestInput2() 
{
	cout <<"\nPlease verify your input by entering it again" << endl;//request user inputs four integers
	cout<< "\nPlease enter your four digit integer: ";
	return;
}




int main()
{
	
	int int4d, v, d1, d2, d3, d4;

	requestInput();//request input
	
	cin>> int4d; 

	cout<< "\nYou entered: " << int4d << "\nPress 1 if this is correct, 2 if this is incorrect: " ; cin>> v;

	cout<<"\nYou entered: "<< int4d/1000 << ", " << (int4d%1000)/100 << ", " << (int4d%100) /10 <<" & " << int4d%10 << "\nPress 1 if this is correct, 2 if this is incorrect: " ; cin>> v;

	while (int4d > 9999 || int4d < 999 || v == 2 )
	{
		cout<<"\nPlease enter a four digit integer: "; cin>> int4d ;//ask user to input 4 digits
		
		cout<<"\nYou entered: " << int4d << "\nEnter 1 if this is correct, 2 if incorrect: "; cin>> v;
		//Verify user input
	//*********************************************************************************************************************************************
		while (v != 2 && v!= 1)
		{
			cout<<"\nPlease enter either 1 for correct, or 2 for incorrect: "; cin>> v;
		}

		while (v == 2)
		{
			cout<<"\nPlease enter a four digit integer: "; cin>> int4d ;
		
			cout<<"\nYou entered: " << int4d << "\nEnter 1 if this is correct, 2 if incorrect: "; cin>> v;
		}
	//**********************************************************************************************************************************************
	}
	//Extract the four digits from the inputed 4 digit integer
	d4 = int4d/1000;
	d3 = (int4d%1000)/100;
	d2 = (int4d%100)/10;
	d1 = int4d%10;
	//******
	extractDigit(d1, d2, d3, d4);//call extract digit function

	//***************************************************************************************************************************
	//***************************************************************************************************************************

return 0;



The initial cin statement isn't reading "0" if this is the first digit in the input statement
Last edited on
Okay, so I've gotten it to work, but now it is giving me an error after I finish running the program "Stack around variable intlength is corrupt"

How do I fix this?!!

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

#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>

using namespace std;

//Function Prototypes
//////////////////////////////////////////////////////
//Request Input Function
///////////////////////////////////////////////////
void requestInput() 
{
	cout <<"Welcome to the beginning of HW3: Data Encryption" << endl;//request user inputs four integers
	cout<< "\nEnter a four digit integer: ";
	return;
}
//////////////////////////////////////////////////////
//Request Input2 Function
///////////////////////////////////////////////////
void requestInput2() 
{
	cout <<"\nPlease verify your input by entering it again" << endl;//request user inputs four integers
	cout<< "\nPlease enter your four digit integer: ";
	return;
}

int main()
{
	
	int int4d, v, d1, d2, d3, d4;

	requestInput();//request input
	
	cin>> int4d; 

	char int;

	requestInput2();
	
	cin >> int;
	
	cout << "\nlength: " <<int;

	cout << "\n" << intl;

	cout<< "\nYou entered: " << intlength << "\nPress 1 if this is correct, 2 if this is incorrect: " ; cin>> v;

	//cout<<"\nYou entered: "<< int4d/1000 << ", " << (int4d%1000)/100 << ", " << (int4d%100) /10 <<" & " << int4d%10 << "\nPress 1 if this is correct, 2 if this is incorrect: " ; cin>> v;

	while (strlen(int < 4 || strlen(int > 4 || v== 2)
	{
		cout<<"\nPlease enter a four digit integer: "; cin>> int4d ;//ask user to input 4 digits

		cout <<"\nPlease verify your input by entering it again: "; cin >> intlength;//request user inputs four integers
				
		cout<<"\nYou entered: " << intlength << "\nEnter 1 if this is correct, 2 if incorrect: "; cin>> v;
		//Verify user input
	//*********************************************************************************************************************************************
		while (v != 2 && v!= 1)
		{
			cout<<"\nPlease enter either 1 for correct, or 2 for incorrect: "; cin>> v;
		}
/*
		while (v == 2)
		{
			cout<<"\nPlease enter a four digit integer: "; cin>> int4d ;
		
			cout<<"\nYou entered: " << int << "\nEnter 1 if this is correct, 2 if incorrect: "; cin>> v;

			cout <<"Please verify your input by entering it again: "; cin >> intlength;//request user inputs four integers
		}*/
	//**********************************************************************************************************************************************
	}

return 0;
}
Last edited on
I fixed it by changing the char array size from "4" to "5"

original:
char intlength [4];

new:
char intlength [5];

problem solved
Last edited on
what can i do in the while loop to keep it prompting user for int if they enter char?
1
2
cin.sync(); // essentially dumps anything in cin
cin.clear(); // clears any error flags on cin 
Thanks, but these commands actually don't work with emacs...so does anyone have a solution that will work with emacs?
Huh? Emacs is an editor. The commands Yay295 gave you are C++ calls.
when I run the commands in a different platform, for some reason the cin.sync() & cin.clear() calls aren't working. I ran them using vs, and it worked. I was wondering if there were calls I could use specific to a platform that uses the emacs editor to clear cin in the while loop because right now this only works in vs.
Hmm, it looks like cin.sync() only works on Windows for some reason. That would be why they work with VS but not Emacs. It's not a Windows specific function though...

cin.ignore() will ignore one character from cin. The function can take two arguments: the first specifies the number of chars to ignore, the second specifies the delimiting character (the character at which it will stop, whether it has ignored the set number of characters or not). Default is 1 and EOF.
Topic archived. No new replies allowed.