ok so I am using this code below to figure out how to get the computer to guess a random 5 digit number and the player has to put in there own number and i got to let them know if they got any correct numbers and if they got any correct numbers in the right spot. i got the game loop implemented and i got it working all the way up until the telling them how many they got right this is where i am super confused can someone please guide me in the right direction
intconst max = 99999;
intconst min = 10000;
int answer = rand() % (max - min + 1) + min;
int attempt = 1;
int guess = 0;
while(guess != answer)
{
std::cout << "Guess attempt #" << attempt << std::endl;
do
{
std::cout << "Please enter a guess(" << min << '-' << max << "): ";
if(!cin) //error with input
{
std::cin.clear();
std::cin.ignore(1024, '\n'); //or use std::numeric_limit
} //edited to add this forgot last time
} while(!(std::cin >> guess) || guess < min || guess > max);
++attempt;
}
std::cout << "Congrats you guessed the number in " << attempt << " guesses!" << std::endl;
oh sorry I missed that part. You're going to have to iterate through both of them and keep track with two flag variables. Or you could use the .find function
Have your computer generate a random 5 digit number, as a string. Then the user must guess the number. Using the code:
1 = implies right Number, Right Place.
2 = implies right Number, Wrong Place.
0 = implies Wrong Number.
So the computer decides on 12345; the user guesses 11235; the computer should respond with 10221. Hint: Watch out for a double number like 11 when there is only one.
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
usingnamespace std;
struct fields{
int size = 5;;
int range = 9;
char lowest = '0';
string guess;
string answer;
int number;
int correct;
int position;
bool gameover = false;
};
void gameplay(fields & info);
int main()
{
fields game;
gameplay(game);
system("pause");
return 0;
}
void gameplay(fields & info){
srand(time(0));
info.answer = "";
for (int i = 0; i < info.size; i++)
{
char ch = info.lowest + rand() % info.range;
info.answer += ch;
}
info.number = 1;
info.correct = 0;
info.position = 0;
while (!info.gameover)
{
cout << "Guess #" << info.number << ": Enter 5 numbers that are '0' through '9': ";
cout << info.answer;
cout << "\n";
cin >> info.guess;
if (info.guess == info.answer)
{
cout << "Right! It took you " << info.number << " move";
if (info.number != 1) cout << "s";
cout << "." << endl;
info.gameover = true;
}
int correctNumbers = 0;
for (charconst &ch : info.guess) //iterate over string you can do anyway you want
{
if (info.answer.find(ch) != string::npos)
{
++correctNumbers;
}
}
intconst digits = 5;
int correctPositions = 0;
int correctPosition[digits];
int test = 0;
for (int i = 0; i < digits; ++i)
{
if (info.answer[i] == info.guess[i])
{
++correctPositions;
}
if (info.answer[i] == info.guess[i]){
correctPosition[i] = 2;
cout << correctPosition[i];
}
}
while (info.position < info.size)
{
if (info.size == info.position) info.correct++;
info.position++;
}
info.number++;
cout << "\nCorrect position: " << correctPositions << endl;
cout << "Correct letter: " << correctNumbers << endl;
}
cout << "GAME OVER\n\n";
}
I go tit where it puts a 2 if it is the right number in the right place and a 0 if the number is not part of it,,h i cant figure out how to include the 1 though can i please get some help