Few Simple Program Errors

This is a discussion on Few Simple Program Errors within the C Programming forums, part of the General Programming Boards category; Hey, I've been following a C book I hired from my local library and it asked me to make programs ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Few Simple Program Errors

    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!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Program 1
    You should be including <stdlib.h> and <time.h> for rand() and time().
    > srand(time());
    The book should be passing one argument to time():
    srand(time(NULL));

    Program 2
    > scanf("&#37;d", iYearBorn);
    scanf() requires an address:
    scanf("%d", &iYearBorn);

    Program 3
    Add: #include <stdlib.h> and <time.h>
    Hint: Die does not equal Dice.
    Last edited by swoopy; 05-22-2008 at 06:07 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    program 1:
    - when trying to debug programs that have things like logic errors (ie incorrect logic flow, such as if statements, etc) or seg. faults, it helps quite a bit to narrow down where the problem exactly is. to do this, put a 'printf' statement, say halfway through your program or function with the problem. run the code, if the statement prints, all code in that block above that line is working ("not crashing"). if it doesnt get printed, there is an error in the code above that line. so put another printf statement halfway between the code above the other printf statement and the start of that code block. keep repeating until you've pinpointed the line the error is on. sorry if this doesnt make sense, but if you know what a binary search is, its identical.

    anyways, the problem is with the srand line. as its very common to produce random values, do a search for using srand with time as it's seed, and youll notice one thing missing.

    program 2:
    Also returns a Segmentation fault on running, although if I enter an invalid entry, such as "Hello", the default: printf get's executed.
    - when using scanf, you have to pass references/pointers/addresses to where you want to store the value read. (ie the address of iYearBorn)
    - though not causing the crash, i would double check your if isdigit line; look closely

    for the last code segment, really read the compiler messages. it isnt lying. you dont, as it says, have anything declared as 'iDie1' or 'iDie2'.

    hope it helps

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Oh god lol now I see the dice error, silly mistake :P.

    I understand everything said, I'm going to try it out now, if I have further problems I'll let you know. Thanks for the replys/help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  4. Errors when building program
    By tay_highfield in forum C Programming
    Replies: 2
    Last Post: 03-07-2003, 10:08 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21