Please help this problem When I put guess number the compute
Please help this problem!
When I put guess number, the computer should generatepico or fermi or bagel!
But it does not work!
Also the number should be random number between (101-999) but I don\'t know how to do it
You can change the code as you want!!
thank you
//Creating Pico Fermi Bagel
//The program has to set the original number in the beginning and ask the user for the number.
//The program has to figure it out how much is same as the original number.(if-else if statement)
//If the number is the negative number or not a 3 different digit number,
//the program has to ask again until the number is correct to play the game.(if statement, do-while loop)
//This program must need a way for the user to \'quit\' the game and for the user to play again without restarting the program.(do-while loop)
//Write a text file with the original number in it, and then all the user guesses and the computer responses.(inputfile)
//Ask the user for the name of the file to save in the beginning of the program.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//function to count digit in number
int count_digit(int number)
{
int digits = 0;
while (number)
{
digits++;
number /= 10;
}
return digits;
}
//checking number contains any repeated digits as mentioned in problem
bool check_number(int number)
{
if (count_digit(number) == 3)
{
if (((number % 10) == ((number / 10) % 10)) || ((number % 10) == ((number / 100) % 10)) || (((number / 10) % 10) == ((number / 100) % 10)))
return false;
else
return true;
}
else
return false;
}
//count the digit occurence in leaders number
bool count_occur(int digit, int guess)
{
int place;
while (guess)
{
place = guess % 10;
if (place == digit)
return true;
guess /= 10;
}
return false;
}
//returning how many digits of players is matched with leaders number
int match_digit(int player, int guess)
{
int digit_unit, digit_tenth, digit_hundreth;
digit_unit = player % 10;
player /= 10;
digit_tenth = player % 10;
player /= 10;
digit_hundreth = player % 10;
if (count_occur(digit_unit, guess) && count_occur(digit_tenth, guess) && count_occur(digit_hundreth, guess))
return 3;
else if ((count_occur(digit_unit, guess) && count_occur(digit_tenth, guess)) || (count_occur(digit_tenth, guess) && count_occur(digit_hundreth, guess)) ||
(count_occur(digit_unit, guess) && count_occur(digit_hundreth, guess)))
return 2;
else if (count_occur(digit_unit, guess) || count_occur(digit_tenth, guess) || count_occur(digit_hundreth, guess))
return 1;
else
return 0;
}
//writing data t file of users choice
void write_file(int find_digit, int guess, int player_1, std::ofstream& outfile)
{
if (find_digit == 1) //If only single digit of players number is present in leaders number
{
//one digit is present and position is not matching
if ((player_1 % 10 != guess % 10) && ((player_1 / 10) % 10 != (guess / 10) % 10) && ((player_1 / 10) % 10 != (guess / 100) % 10))
outfile << player_1 << \":\" << \"Pico\" << endl;
else
outfile << player_1 << \":\" << \"Fermi\" << endl;
}
else if (find_digit == 2) //If two digits of players number is present in leaders number
{
//Two digits are present and two positions are matched with leaders number
if (((player_1 % 10 == guess % 10) && ((player_1 / 10) % 10 == (guess / 10) % 10)) ||
((player_1 % 10 == guess % 10) && ((player_1 / 100) % 10 == (guess / 100) % 10)) ||
(((player_1 / 10) % 10 == (guess / 10) % 10) && ((player_1 / 100) % 10 == (guess / 100) % 10)))
outfile << player_1 << \":\" << \"Fermi Fermi\" << endl;
//Two digits are present and single position is matched with leaders number
else if (((player_1 % 10 == guess % 10) && ((player_1 / 10) % 10 != (guess / 10) % 10)) ||
((player_1 % 10 != guess % 10) && ((player_1 / 10) % 10 == (guess / 10) % 10)) ||
((player_1 % 10 == guess % 10) && ((player_1 / 100) % 10 != (guess / 100) % 10)) ||
((player_1 % 10 != guess % 10) && ((player_1 / 100) % 10 == (guess / 100) % 10)) ||
(((player_1 / 10) % 10 == (guess / 10) % 10) && ((player_1 / 100) % 10 != (guess / 100) % 10)) ||
(((player_1 / 10) % 10 != (guess / 10) % 10) && ((player_1 / 100) % 10 == (guess / 100) % 10)))
outfile << player_1 << \":\" << \"Pico Fermi\" << endl;
//Two digits are present and none positions are matched with leaders number
else
outfile << player_1 << \":\" << \"Pico Pico\" << endl;
}
else if (find_digit == 3) //If all 3 digits of players number is present in leaders number
{
//If all digits are present and only units place is matching with leaders number
if ((player_1 % 10 == guess % 10) && ((player_1 / 10) % 10 != (guess / 10) % 10) && ((player_1 / 100) % 10 != (guess / 100) % 10))
outfile << player_1 << \":\" << \"Pico Pico Fermi\" << endl;
//If all digits are present and only 10th place is matching with leaders number
else if ((player_1 % 10 != guess % 10) && ((player_1 / 10) % 10 == (guess / 10) % 10) && ((player_1 / 100) % 10 != (guess / 100) % 10))
outfile << player_1 << \":\" << \"Pico Fermi Pico\" << endl;
//If all digits are present and only 100th place is matching with leaders number
else if ((player_1 % 10 != guess % 10) && ((player_1 / 10) % 10 != (guess / 10) % 10) && ((player_1 / 100) % 10 == (guess / 100) % 10))
outfile << player_1 << \":\" << \"Fermi Pico Pico\" << endl;
//If all digits are present and all places are matching with leaders number
else if ((player_1 % 10 == guess % 10) && ((player_1 / 10) % 10 == (guess / 10) % 10) && ((player_1 / 100) % 10 == (guess / 100) % 10))
outfile << player_1 << \":\" << \"Fermi Fermi Fermi\" << endl;
//If all digits are present and none of the place is matching with leaders number
else
outfile << player_1 << \":\" << \"Pico Pico Pico\" << endl;
}
else //If none digits of players number is present in leaders number
{
outfile << player_1 << \":\" << \"Bagel\" << endl;
}
}
int main()
{
std::ifstream myfile(\"Guess.txt\"); //input file
std::ofstream outfile; //Output file
std::string out_file_name; //User entered output file name
char ch = \'y\';
int player_1, guess_count = 1;
int guess;
myfile >> guess; //copying leader choice in guess
cout << \"Enter output file name : \";
cin >> out_file_name;
outfile.open(out_file_name.c_str()); //creating output file of user choice
outfile << guess << endl;
do
{
while (1) //it will execute until user enters proper value mentioned in Question
{
cout << \"Player guess \" << guess_count << \": \";
cin >> player_1;
if (check_number(player_1)) //user entered proper value so break the loop
break;
else
cout << \"Number is not ok\" << endl;
}
int find_digit = match_digit(player_1, guess); //counting how many digits of players guess number in leaders choice
write_file(find_digit, guess, player_1, outfile); //writing data to file depending on condition given in problem
cout << \"Do you want to continue y/n : \"; //indefinite loop executes on user choice.If want to stop press \'n\'
cin >> ch;
guess_count++;
} while (ch == \'y\');
return 0;
Solution
You can generate random numbers like this :
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
using namespace std;
int rand_num = 0;
int main()
{
srand(time(0)); // seed the random number generator
for(int i = 0 ; i<899; i++)
{
rand_num = rand() % (900-1) + 100;
cout<<rand_num<<\" \";
}
cout<<\"\ \";
}
OR
int rand_number()
{
int rand_num;
rand_num = rand() % (900-1) + 100;
}
you can Call rand_number funtion instead of where Player is guessing in main funtion ,So no need to enter random number.
I checked on my own compiler creating Pico Fermi Bagel is proprely in file and program also working good





