Code:
#include<stdio.h>#include<stdlib.h>
#include<time.h>
main()
{
float choice=0, result=0, cnt=0;
int ans;
time_t t;
do
{
srand(time(&t));
printf("pick a number from 1 to 4:\n");
scanf("%f", &choice);
if (choice<=4 && choice>=0)
{
result=(rand()%3)+1;
}
else
{
printf("I'm sorry, Please pick a number within the stated range");
continue;
}
if (choice==result)
{
printf("YAY!!!! You got it right!\n!");
cnt= cnt + 1 ;
printf("Your score is %d\n", cnt);
printf("Would you like to try again?\n");
}
else
{
printf("OOOOHHHH!!!! Better luck next time.\n\n " );
printf("Your score is %d\n", cnt);
printf("Try again?\n\n");
}
printf("Please answer with yes=0 or no=1.\n\n\n");
scanf(" %d", &ans);
}while(ans= 1);
return 0;
}
Above is my program for rolling a random number between 1 and 4. When I try to keep going by pressing '0', the program still ends.
I've been self teaching myself C and have been doing well so far up to this point so perhaps it's something subtle.
Also, could someone guide me on how I would introduce a string into the "While" statement. I initially tried to have the user type in "yes" or "no" but to no luck.
Thank you for your time.