Hey, I've been following a C book I hired from my local library and it asked me to make programs such as the following, but each program gives errors when running or compiling. I have no idea what's wrong, I can't see the error, so it'd be great if you could help me out, thanks!
program 1:
Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
int iRandNum = 0;
int iUserGuess = 0;
srand(time());
iRandNum = (rand() % 10) + 1;
printf("Guess the number from 1 to 10\n");
printf("hi\n");
scanf("%i", &iUserGuess);
if (isdigit(iUserGuess) == 0)
{
printf("Invalid Entry, Not a number!\n");
}
else if (isdigit(iUserGuess) && iUserGuess == iRandNum)
{
printf("You guessed it!\n");
}
else if (isdigit(iUserGuess) && iUserGuess != iRandNum)
{
printf("Wrong Guess! Number was: %i\n", iRandNum);
}
return 0;
}
Returns a Segmentation Fault on running.
Program 2:
Code:
#include <stdio.h>
int main()
{
int iYearBorn = 0;
printf("\nEnter year born, either 2000, 2001, 2002:");
scanf("%d", iYearBorn);
if (isdigit(iYearBorn == 0))
{
printf("Invalid Entry. Program Terminating.");
return 0;
}
switch(iYearBorn)
{
case 2000:
printf("2000 is the year of luck!");
break;
case 2001:
printf("Peace be with you 2001");
break;
case 2002:
printf("2002 is uber leet pwnage dudez");
break;
default:
printf("No data on that year!");
}
return 0;
}
Also returns a Segmentation fault on running, although if I enter an invalid entry, such as "Hello", the default: printf get's executed.
Code:
#include <stdio.h>
int main()
{
int iDice1 = 0;
int iDice2 = 0;
int iDiceSum = 0;
srand(time());
iDice1 = (rand() % 6) + 1;
iDice2 = (rand() % 6) + 1;
iDiceSum = iDice1 + iDice2;
printf("If the sum f both die is 7 or 11 you win!\n\n");
printf("Die 1 rolled a %d\n", iDie1);
printf("Die 2 rolled a %d\n", iDie2);
printf("\nThe sum of both rolls is %d\n\n", iDiceSum);
if (iDiceSum == 7 || iDiceSum == 11)
{
printf("You Win!");
}
else
{
printf("You Lose!");
}
return 0;
}
Few errors for this one when compiling, they are as follows:
Code:
diegame.c: In function ‘main’:
diegame.c:16: error: ‘iDie1’ undeclared (first use in this function)
diegame.c:16: error: (Each undeclared identifier is reported only once
diegame.c:16: error: for each function it appears in.)
diegame.c:17: error: ‘iDie2’ undeclared (first use in this function)
Thanks a lot guys!