Rolling dice with over 7 digits - c

My aim is to make a program that will throw two dice until the top faces of the two dice total to a number.
However, in my code, i roll a dice with over 7 digits. Help me please, Thank you!
My code:
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int intRandom(int a=1,int b=6)
{
int n;
n=a+rand()%b;
return n;
}
int main()
{
int i,n,x,y; /*n:total, i:count*/
printf("Dice Thrower\n================\n");
printf("Total sought: ");
do
{
scanf("%d",&n);
} while (n<2 && n>12);
{
i=1;
srand(time(NULL));
do
{
x=intRandom(2,6);
y=intRandom(6,2);
printf("Result of throw %d: %d + %d\n",i,x,y);
if ((x+y)==n) printf("\nYou got your total in %d throws!\n",i);
fflush(stdin);
i++;
} while ((x+y)!=n);
}
getch();
}

Change the random number generation to
x=intRandom(1,6);
y=intRandom(1,6);

Related

When entering an integer it keeps executing the else statement, trying to search user's input by isdigit if its a number, don't understand why

#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}

how to display a timer every second while my code is running on linux

I'm working on a simple game where you guess a random number between 0 and 100. You have 7 seconds and 6 tries to guess it but I want to display the countdown timer while the game is running.
(00:07 00:06 00:05......00:00)
I tried to use gotoxy function to display the timer on the side but the displaying was a mess because gotoxy put the cursor on the next line after it's called. Or maybe I didn't use it right.
Is there a way to display the time (in seconds) without having a messy display?
Here is my try:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
/*void gotoxy(int x, int y){
printf("%c[%d;%df",0x1b,y,x);
}*/
void timer();
int c=6;
void main ()
{
system("clear");
int a, num=200;
signal(SIGALRM,timer);
srand(time(NULL));
a=rand()%101;
int i=1;
printf("try to guess a number between 0 and 100 in 7 seconds , you have only 6 tries \n\t good luck\n");
alarm(1);
//gotoxy(50,2);
printf("00:07\n");
while ((a != num)&&(i<=7)) {
if (i==7) printf("no more tries !!!! \n");
else printf("try: %d \n ",i);
printf("give a number \n ");
scanf("%d",&num);
if (a>num)
printf("it's more ! \n ");
else if (a<num)
printf("it's less ! \n ");
else
printf("\t\tGreat ! you won !!! \n ");
i++;
}
}
void timer(){
if(c<0){
printf ("you lost \n");
exit(1);
}
else{
//gotoxy(50,2);
printf("00:0%d\n",c);
c--;
alarm(1);
}
}

Odd And Even Counter

I want to change this codes to get 20 Numbers from input and count how many are Odd and how many are Even? please can anyone help?!
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is an odd number",n);
odd++;
}
else
{
printf("%d is an even number",n);
even++;
}
printf("\n odd%d / even%d",odd,even);
}
Here is a function that solves your problem:
Tip: You need a loop to take your input 20 times.
void countForJHikaam(){
int n,i;
int odd=0;
int even=0;
for(i=0;i<20;i++){
scanf("%d\n",&n);
if(n%2==0){
even++;
}else{odd++;}
}
printf("Odds: %d, Evens: %d",odd,even);
}
It won't really help you in learning. Now go learn what a function is.
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
while(scanf("%d",&n))
(n%2) ? (++odd) : (++even);
printf("\n odd%d / even%d",odd,even);
}
#include<stdio.h>
main()
{
int odd=0,even=0,no,count=20;
printf("Enter the 20 numbers...\n");
here:
scanf("%d",&no);
(no%2==0)? odd++ : even++ ;`
count--;
if(count>0)
goto here;
printf("No of odd numbers... :%d\n",odd);
printf("No of even numbers... :%d\n",even);
}

do While loop. It doesn't work

I'm really new to C and I can't get this while loop to work. It just exits the loop for no apparent reason.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int min;
int max;
int random;
int guess;
char user='x';
printf("Please insert min number:");
scanf("%d",&min);
printf("Please insert max number:");
scanf("%d",&max);
random = (rand()%(max-min))+ min;
do
{
printf("please insert your guess:");
scanf("%d",&guess);
if(guess==random)
{
printf("YOU WON");
user=='y';
}
else if(guess!=random)
{
printf("UNLUCKY,would you like to play again:x for yes");
user=='x';
}
}while(user=='x');
}
The following is a comparison, not an assignment:
user=='y';
You need to change it to:
user='y';
(Note the single =.)
The same goes for the other assignment.

how can I increment in this code

Write a program that displays a new random permutation of the integers 0 to 9 at the request of its user. For example, the program’s output could be as follows:
Your program should prints how many 7 was printed when user type no.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i , total , r;
char ans;
srand(time(NULL));
do{
for( i=0 ; i < 10 ; i++)
{
r= (rand()%(9-1+1)) + 1;
printf ("%d ",r);
}
total =0;
if (r==7) // Here how can I correct this so total will increase every time
{ // there is a 7 in the string
total++;
}
printf("\nAnother permutation: y/n?\n");
scanf(" %c",&ans);
if (ans != 'y')
{
printf("Bye!\n");
printf("The number of 7's is: %d", total);
}
}while(ans=='y');
return 1;
}
I have a problem with my code. How can I increment the 7's shown in this program after != 'y'.
Set total=0 before entering into the do-while loop, to get the correct total.

Resources