Help with Limiting Input

I need help with Limiting the user input to only 4 digits and each digit can only be 0-7. It's an encryption program that takes replaces each digit with (sum of the digit + 3)modulus 8. Then swaps the first digit with the second and the third with the fourth. I have a problem with my program allowing numbers with less and more than 4 digits. I can't properly create an if statement.
This is my code.

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

void encryptSwap(int,int,int,int);

using namespace std;

int main(){
    
    int num;
    //Prompt user to input 4 digit number
    cout<<"Enter 4 digit number to be Encrypted"<<endl;
    cin>>num;

    //Variables for each digit
    int d1,d2,d3,d4;

    //Grab each individual digit
    d1=num%10000;
    d1=d1/1000;
    d2=num%1000;
    d2=d2/100;
    d3=num%100;
    d3=d3/10;
    d4=num%10;

    //Check value
    if((d1>=0&&d1<=7)&&(d2>=0&&d2<=7)&&(d3>=0&&d3<=7)&&(d4>=0&&d4<=7)){
      encryptSwap(d1,d2,d3,d4);
    }else{
       cout<<"Value is Invalid!"<<endl;
    }
return 0;
}
void encryptSwap(int digit1,int digit2,int digit3,int digit4){
    //Replace value
    digit1=(digit1+3)%8;
    digit2=(digit2+3)%8;
    digit3=(digit3+3)%8;
    digit4=(digit4+3)%8;

    //Swap Values
    //Value place holder variables
    int x, y;
    x=digit2;
    y=digit4;
    digit2=digit1;
    digit1=x;
    digit4=digit3;
    digit3=y;
    //Display encrypted number
    cout<<"Encrypted number:"<<endl;
    cout<<digit1<<digit2<<digit3<<digit4<<endl;
}


I can't figure out how to keep it only 4 digits with each digit only being 0-7.
Have you considered getting the input as a char? Then as part of the input validation convert these characters to your int?
Last edited on
Your problem is std::cin grabs all 4 digits then you try to grab the numbers individually that cin has placed in num
1
2
3
4
5
6
7
d1=num%10000;
    d1=d1/1000;
    d2=num%1000;
    d2=d2/100;
    d3=num%100;
    d3=d3/10;
    d4=num%10;


What you are actually doing is this:
1
2
3
4
5
6
7
    d1=num%10000;//d1 still equals num
    d1=d1/1000;
    d2=num%1000;//d2 equals num as well
    d2=d2/100;
    d3=num%100;//d3 is num
    d3=d3/10;
    d4=num%10;//surprise its num D: 

To fix this you could split them up into four separate variables

1
2
3
4
cin>>d1;
cin>>d2'
cin>>d3;
cin>>d4; 


Unless you overload another operator and make your own cin type function or use bitshift algorithms this function wont work.
Last edited on
So something like this.
1
2
3
4
5
6
7
8
9
10
    char num[4];
    int d1,d2,d3,d4;
    cout<<"Enter 4 digit number"<<endl;
    cin>>num
    
    d1=(int)num[0]-48;
    d2=(int)num[1]-48;
    d3=(int)num[2]-48;
    d4=(int)num[3]-48;


I can still accept 5 digits with this though.
ForRealzZzZ wrote:
What you are actually doing is this:
1
2
3
4
5
6
7
    d1=num%10000;//d1 still equals num
    d1=d1/1000;
    d2=num%1000;//d2 equals num as well
    d2=d2/100;
    d3=num%100;//d3 is num
    d3=d3/10;
    d4=num%10;//surprise its num D:  


I think he had it working the way it was...

1
2
3
4
5
6
7
8
    //let's say num = 9876
    d1=num%10000; //d1 = 9876
    d1=d1/1000; //d1 = 9
    d2=num%1000; //d2 = 876
    d2=d2/100; //d2 = 8
    d3=num%100; //d3 = 76
    d3=d3/10; //d3 = 7
    d4=num%10; //d4 = 6 


@Kurisutofaa

Try using cin.getline(num, 4);.
http://cplusplus.com/reference/istream/istream/getline/
char num[3]; ---> 0 1 2 3
if (d1 > 7 || ...) ---> get another input
Last edited on
Thanks for the help guys. I got it working properly.
Topic archived. No new replies allowed.