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?
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
usingnamespace 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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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);
}