Answer:
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool equal(int sys[],int user[])
{
for (int i=0;i<7;i++)
{
if(sys[i]!=user[i]){
return false;
}
}
return true;
}
int main()
{
int sys[7];
int user[7];
int min=0
;int max=9;
string userinput;
while(0==0){
for (int i=0;i<7;i++)
{
sys[i]=rand() % (max - min + 1) + min;
}
cout <<endl;
cout<<"enter numbers"<<endl;
for (int i=0;i<7;i++)
{
getline (cin,userinput);
if(userinput=="x"){
return 0;
}
else{
stringstream(userinput) >> user[i];
}
}
cout<<"User number"<<endl;
for (int i=0;i<7;i++)
{
cout << user[i];
}
cout<<"system number"<<endl;
for (int i=0;i<7;i++)
{
cout << sys[i];
}
if(equal(sys,user))
{
cout<<"You won !"<<endl;
}else {
cout<<"Try again :("<<endl;
}
}
return 0;
}
Explanation:
Using c++ rand function generate a random number between 0-9.
Formula for random number is rand (max-min +1)+min.
Run this in a loop for 7 time and store each number to an array index in sys array which is for system generated number.
now using string take input from user and check if input is x close program else use stringstream to parse input string to int and store it in user array. Stringstream is a cpp header file used for parsing and handling of strin g inputs. Run this program in loop for 7 times.
Now create a function of type boolean denoted by "bool". This will take both arr as parameter and check if both are equal or not. It compares eac index of user array against each index of sys array and if both values are not equal returns false.