Jun 6, 2018 at 11:10pm UTC
guys can someone please help in the while(ans=='y) even it is not equal to y it goes on the loop please help thanks in advance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand((unsigned )time(0));
char ans;
int a , b , gameplay=1 ,score=0;
int arr[10000] ,arr2[10000];
do
{
cout<<"I WANT TO PLAY A GAME!!\n" ;
cout<<"************************\n" ;
cout<<"WELCOME TO MEMORY GAME\n" ;
cout<<"========================\n" ;
for (a=0; a<10000; a++)
{
score++;
int num = rand()%10;
arr[a]=num;
cout<<"MEMORIZE THE NUMBER: " <<endl;
Sleep(1000);
cout<<arr[a]<<endl;
Sleep(1500);
system("cls" );
cout<<"ENTER NUMBER HERE: " <<endl;
for (b=0; b<score; b++)
{
cin>>arr2[b];
if (arr[b] != arr2[b])
{
cout<<"GAME OVER!!\n" <<endl;
cout<<"YOUR SCORE IS: " <<score-1<<endl;
cout<<"Do you want to play again? (y/n): " ;
cin>>ans;
gameplay++;
cout<<"GAME NUMBER: " <<gameplay<<endl;
Sleep(2000);
system("cls" );
}
}
}
}while (ans == 'y' );
getch();
return 0;
}
Last edited on Jun 6, 2018 at 11:13pm UTC
Jun 7, 2018 at 8:56am UTC
You're asking the user if they want to play again inside a for loop. You will be asking the user over and over again, inside that for loop. Asking them if they want to play again should be the LAST thing you do, between line 51 and 52.
Indent your code properly so you can see your own loops clearly.