Limiting Character Input Size

Hi there.
Before going to the question, let me introduce my self.
Im just a first year computer science student and i know only the bare basics of c++. The code below is just a fun game i made up while trying to test the "IF" function. The problem i have with it is that i don't know how to limit the input size.
I want to limit the character size input to 1 in line 22, Is there any command or function to do it?

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
79
80
81
82
83
84
85
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
main()
{
	int cn1, cn2, cn3, cn4, un1, un2, un3, un4, tn, w, l, pd;
	w=0;
	l=0;
	do {
	srand(time(NULL));
	cn1=rand()%9;
	cn2=rand()%9;
	cn3=rand()%9;
	cn4=rand()%9;
	tn=5;
	do {
	cout<<"Code set!\n";
	cout<<"Input the 4 numbers for the code:\n";
	cout<<"They must be between 0-9!\n";
	cin>>un1>>un2>>un3>>un4;
	system("cls");
	cout<<"Your code:\n";
	cout<<"["<<un1<<"]"<<"["<<un2<<"]"<<"["<<un3<<"]"<<"["<<un4<<"]\n";
	if (un1>cn1) {
		cout<<"[1]First number is bigger than the combination number\n";
	}
	else if (un1<cn1) {
		cout<<"[1]First number is lower than the combination number\n";
	}
	else {
		cout<<"[1]First number is correct\n";
	}
	if (un2>cn2) {
		cout<<"[2]Second number is bigger than the combination number\n";
	}
	else if (un2<cn2) {
		cout<<"[2]Second number is lower than the combination number\n";
	}
	else {
		cout<<"[2]Second number is correct\n";
	}
	if (un3>cn3) {
		cout<<"[3]Third number is bigger than the combination number\n";
	}
	else if (un3<cn3) {
		cout<<"[3]Third number is lower than the combination number\n";
	}
	else {
		cout<<"[3]Third number is correct\n";
	}
	if (un4>cn4) {
		cout<<"[4]Fourth number is bigger than the combination number\n";
	}
	else if (un4<cn4) {
		cout<<"[4]Fourth number is lower than the combination number\n";
	}
	else {
		cout<<"[4]Fourth number is correct\n";
	}
	if (cn1==un1&&cn2==un2&&cn3==un3&&cn4==un4) {
		break;
	}
	tn--;
	cout<<"You have "<<tn<<" tries left\n";
    } while (tn!=0);
    if (cn1==un1&&cn2==un2&&cn3==un3&&cn4==un4) {
    	cout<<"You cracked the code!\n";
    	cout<<"Congratulations!\n";
    	w++;
	}
	else {
		cout<<"You didn't crack the code?\n";
		cout<<"You doomed us all!\n";
		l++;
	}
	cout<<"Total wins: "<<w<<"\n";
	cout<<"Total loses: "<<l<<"\n";
    cout<<"Want to try again?\n";
    cout<<"[1]Yes\n";
    cout<<"[2]No\n";
    cin>>pd;
    } while (pd==1);
}
Assuming that you are not yet familiar with arrays and loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cctype>

int main()
{
    char d1, d2, d3, d4 ;

    std::cout << "enter four decimal digits [0-9] for the code: ";
    std::cin >> d1 >> d2 >> d3 >> d4 ;

    using std::isdigit ; // http://en.cppreference.com/w/cpp/string/byte/isdigit
    if( isdigit(d1) && isdigit(d2) && isdigit(d3) && isdigit(d4) ) // if all four are digits
    {
        const int un1 = d1 - '0' ; // '7' - '0' == 7 etc.
        const int un2 = d2 - '0' ;
        const int un3 = d3 - '0' ;
        const int un4 = d4 - '0' ;

        std::cout << "your code: [" << un1 << "] [" << un2 << "] [" << un3 << "] [" << un4 << "]\n";
    }

    else std::cout << "you did not enter four digits\n" ;
}
Thanks a lot, i will be using this a reference
Topic archived. No new replies allowed.