4-digit numbers guessing game - need help

Hi forums, i need someone's help to solve this problem. I am trying to use the random function to generate a four digit numbers and give clues when the user enter a correct digit. I do not know why the digit generated is wrong and the clue given is wrong too. I used the MOD and integer division but it does not solve the problem . Hope that someone can help me find out the problem of my code.

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std ;

int main (){
// declaring storage
int Option, Setting, MaxScore ;
int UserScore = 0 ;
int LowerValueRange = 1 ;
int UpperValueRange = 5 ;
int DefaultMaxScore = 25 ;
string NickName1 ;
int Num, SecretNum, SecretNum1, SecretNum2, SecretNum3 ;
bool again = true ;
bool again1 = true ;



int Range = UpperValueRange - LowerValueRange + 1 ;




int Num1 = Num/10 ;
int Num2 = Num/100 ;
int Num3 = Num/1000 ;



//input
while(again){

cout << "Welcome to the Guessing Game !" << endl << endl ;
cout << "Game Menu" << endl ;
cout << "*********" << endl ;
cout << "1. Start Game" << endl ;
cout << "2. Setting" << endl ;
cout << "3. High Score Board" << endl ;
cout << "4. Exit System" << endl << endl ;
cout << "Please select an option :" ;
cin >> Option ;
cout << endl ;

system("CLS");
switch(Option){
case 1 : srand(time(NULL)) ;
SecretNum = rand()% Range + LowerValueRange ;
SecretNum1 = rand()% Range + LowerValueRange ;
SecretNum2 = rand()% Range + LowerValueRange ;
SecretNum3 = rand()% Range + LowerValueRange ;
cout <<" Welcome, please enter your nick name:" ;
cin.ignore() ;
getline(cin, NickName1) ;
cout << endl << "Hi," << NickName1 << "." << endl << endl ;
cout << "Settings" << endl ;
cout << "********" << endl ;
cout << "The possible number for each digit is :" << LowerValueRange << " to " << UpperValueRange << "." << endl ;
cout << "The maximum score is :" << DefaultMaxScore << "." << endl << endl ;
cout << "Rules" << endl ;
cout << "*****" << endl ;
cout << "Enter -1 at any times if you wish to skip a round." << endl << endl ;
cout << "Clue" << endl ;
cout << "*****" << endl ;
cout << "$ : If a digit is guessed correctly and in the correct position." << endl ;
cout << "# : If a digit is guessed correctly but not in the correct position." << endl << endl ;



while ((Num%10)!= SecretNum || (Num1%10)!= SecretNum1 || (Num2%10 )!= SecretNum2 || (Num3%10 )!= SecretNum3){

cout << "Enter 4 digits number :" ;
cin >> Num ;

cout << "Clue :" ;

if ((Num%10)== SecretNum){
cout << "$" ;
}
else if ((Num1%10)== SecretNum){
cout << "#" ;
}
else if ((Num2%10)== SecretNum){
cout << "#" ;
}
else if ((Num3%10)== SecretNum){
cout << "#" ;
}
if ((Num1%10)== SecretNum1){
cout << "$" ;
}
else if ((Num%10)== SecretNum1){
cout << "#" ;
}
else if ((Num2%10)== SecretNum1){
cout << "#" ;
}
else if ((Num3%10)== SecretNum1){
cout << "#" ;
}
if ((Num2%10)== SecretNum2){
cout << "$" ;
}
else if ((Num%10)== SecretNum2) {
cout << "#" ;
}
else if ((Num1 % 10)== SecretNum2){
cout << "#" ;
}
else if ((Num3%10)== SecretNum2){
cout << "#" ;
}
if ((Num3%10)== SecretNum3){
cout << "$" ;
}
else if ((Num%10)== SecretNum3){
cout << "#" ;
}
else if ((Num1%10 )== SecretNum3){
cout << "#";
}
else if ((Num2%10 )== SecretNum3){
cout << "#" ;
}
if ((Num%10 )== SecretNum && (Num1%10)== SecretNum1 && (Num2%10 )== SecretNum2 && (Num3%10 )== SecretNum3){
cout << endl << endl ;
cout << "Congratulations, Your score is :" << UserScore ;
}

if (Num == -1){
break ;
}
cout << endl << endl ;
}

break ;

case 2 : while(again1){
cout << "Setting" << endl ;
cout << "*********" << endl ;
cout << "1. Value Range (for each digit) :" << LowerValueRange << " to " << UpperValueRange << "." << endl ;
cout << "2. Maximum Score :" << DefaultMaxScore << "." << endl << endl ;
cout << "If player wish to change the value range, please enter 1." << endl ;
cout << "If player wish to change the maximum score, please enter 2." << endl ;
cout << "If player do not wish to change anything, please enter 3." << endl << endl ;
cout << "Option :" ;
cin >> Setting ;
cout << endl ;

if (Setting ==1){
cout << "PLease enter the lower limit of the value range :" ;
cin >> LowerValueRange ;
cout << "Please enter the upper limit of the value range :" ;
cin >> UpperValueRange ;
LowerValueRange = LowerValueRange ;
UpperValueRange = UpperValueRange ;
cout << endl ;
cout << "The new value range :" << LowerValueRange << " to " << UpperValueRange << endl << endl ;


}

else if (Setting ==2){
cout << "Please enter maximum score :" ;
cin >> MaxScore ;
cout << "The new maximum score :" << MaxScore << endl << endl ;
DefaultMaxScore = MaxScore ;

}
else if (Setting==3) {
again1 = false ;

}
else {
cout << "Invalid Option. Please enter the option again :" << endl << endl ;

}
}
break ;

case 3 : cout << "High Score Board" << endl ;
cout << "----------------" << endl ;

break ;

case 4 : cout << "Game End" ;
again = false ;

break ;

default: cout << "Invalid option. Please enter the option again :" << endl << endl ;
}
}
return 0 ;
}



What do you type when you run it. What is the first thing you see on screen that you don't like?
#include <cstdlib>
#include <stdlib.h> //remove this. its the C version of the one you already had.


it looks like you want the digits but are dealing with it as ints.
if you just read the number as a string, you can look at the characters that represent digits and its a LOT easier to do and cleaner.


I am not sure but I don't think you grok mod.

num % 10 … if num is 1234, this is 4. That works.

num %100 … not so much. this is 34. if you subtracted the above from it and divided by 10, you would get 3....

unless I am totally not seeing what you are trying to do.



Last edited on
To All :
I have already fixed the problem by using the MOD and integer division to generate the four digits number that i want. Thanks for all of the help and i really appreciate it .
Topic archived. No new replies allowed.