Search This Blog

Saturday 12 May 2018

Introduction to Programming (CS201)-Guessing Game in C++

// Guesing game

#include <iostream>
#include <stdlib.h>

using namespace std; //Allows computer to output data

main() //main function begins
{
int i,z=0;
int c[100];


//initialize the array
for (i=0;i<10;i++)
{
c[i]=rand()%10;
}

int j=1;

cout << "You have five chance to guess the number\n";
int found=0;

do
{
cout << "Attempt: " << j << " Guess the number " << z;
cin >> z;


//loop to guess the number
for (i=0;i<10;i++)
{
if (z==c[i])
{
found=1;
break;
}

}
if (found==1)
{
cout << "You Won!! Number was " << c[i] << " at index " << i;
}
else
{
j++;
found=0;
}


} while (j<6 && found!=1);
if (found==0)
{
cout << "You Lost!! Number was not found";
}

}//end of main function

No comments:

Post a Comment