//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.
#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'?
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.
#include <iostream>
#include <string>
#include <cmath>
usingnamespace 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
#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
usingnamespace 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;
charint;
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;
}
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.