Thread: Problem in a C program

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    11

    Problem in a C program

    Hello,

    I'm learning C with help of a book and one of the challenges is to create a number guessing program.

    The compiler doesn't give me an error but when i run the program and enter a number
    it exits with "Segmentation fault (core dumped)" error:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    
    {
    	int iRandom = 0;
    	char cResponse = '\0';
    	srand(time());
    	
    	iRandom = (rand() % 10) +1;
    	
    	printf("\nGuess a number between 1 and 10: ");
    	scanf("%c", cResponse);
    	
    	if isdigit(cResponse)
    		{
    			if (cResponse == iRandom)
    				printf("\nCongratulations, you guessed correct!");
    			else
    				printf("\nSorry, you guessed wrong.");
    				printf("The correct number was: %d", iRandom);
    		}
    	else
    		printf("You did not enter a number");
    	}
    Can anyone help please?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Include time.h.
    Enable your compiler warning and pay attention to them.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Hi matt,

    Your program doesn't compile and contain a lot of errors, see comments below:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>	// srand() and rand()
    #include <time.h>	// time()
    
    int main() 
    //main() // 	warning: control reaches end of non-void function
    {
    	int iRandom = 0;
    	char cResponse = '\0';
    	//srand(time());
    	srand(time(NULL));
    	
    	iRandom = (rand() % 10) +1;
    	
    	printf("\nGuess a number between 1 and 10: ");
    	scanf("%c", &cResponse);	// You must pass an adress
    	
    	if (isdigit(cResponse))
    	// if isdigit(cResponse) // error: expected ‘(’ before ‘isdigit’
    		{
    			if (cResponse == iRandom)
    				printf("\nCongratulations, you guessed correct!");
    			else
    				printf("\nSorry, you guessed wrong.\n");
    				printf("The correct number was: %d", iRandom);
    		}
    	else
    		printf("You did not enter a number");
    		
    	return 0;
    	}
    Regards.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    Thanks evariste

    it works fine now

    but gcc compiler DID compile it even with all those errors
    and didn't even give me a warning

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Use at least
    Code:
    gcc -Wall -pedantic -o my_output_file <my_input_files>

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    Quote Originally Posted by rags_to_riches View Post
    Use at least
    Code:
    gcc -Wall -pedantic -o my_output_file <my_input_files>
    Okay, thanks

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matt345 View Post
    Thanks evariste

    it works fine now

    but gcc compiler DID compile it even with all those errors
    and didn't even give me a warning
    You could not have gotten gcc to compile something without parentheses around the condition of an if-statement.

    You may have had an a.out (or whatever executable you created) running around from a previous attempt, if you didn't delete it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  3. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM