Segmentation Fault using the srand function

J

jtagpgmr

I am currently using the gcc compiler on a cygwin platform, I am a
beginner when it comes to programming in C and want to know why anytime
I run the .exe with the following code I get a "segmentation fault
(core dumped)" error:

#include <stdio.h>

main()

{

int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
srand(time());

printf("\nEnter number of questions to ask: ");
scanf("%d", &iNumQuestions);

for (x=0; x<iNumQuestions; x++) {


iRndNum1=rand()%10+1;
iRndNum2=rand()%10+1;

printf("\nWhat is %d x %d: ", iRndNum1, iRndNum2);
scanf("%d", &iResponse);

if (iResponse == iRndNum1*iRndNum2)
printf("\nCorrect!\n");

else
printf("\nThe correct answer was %d \n", iRndNum1*iRndNum2);

}

}
 
E

Eric Sosman

jtagpgmr wrote On 06/27/06 13:14,:
I am currently using the gcc compiler on a cygwin platform, I am a
beginner when it comes to programming in C and want to know why anytime
I run the .exe with the following code I get a "segmentation fault
(core dumped)" error:

#include <stdio.h>

main()

{

int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
srand(time());
[...]

This is the proximate cause of your problem: The time()
function requires an argument, but you didn't provide one.

Do not imagine that this is the only error in your
code. To learn about the others, ask gcc to be more verbose
in its diagnostics: Add "-W -Wall" to the other command-line
flags, and pay attention to what the compiler then tells you.
(If you're really brave, use "-W -Wall -ansi -pedantic" or
"-W -Wall -std=c99 -pedantic". It will be a learning
experience.)
 
J

jtagpgmr

What does the following mean?
implicit declaration of function srand
implicit declaration of function time
implicit declaration of function rand

Eric said:
jtagpgmr wrote On 06/27/06 13:14,:
I am currently using the gcc compiler on a cygwin platform, I am a
beginner when it comes to programming in C and want to know why anytime
I run the .exe with the following code I get a "segmentation fault
(core dumped)" error:

#include <stdio.h>

main()

{

int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
srand(time());
[...]

This is the proximate cause of your problem: The time()
function requires an argument, but you didn't provide one.

Do not imagine that this is the only error in your
code. To learn about the others, ask gcc to be more verbose
in its diagnostics: Add "-W -Wall" to the other command-line
flags, and pay attention to what the compiler then tells you.
(If you're really brave, use "-W -Wall -ansi -pedantic" or
"-W -Wall -std=c99 -pedantic". It will be a learning
experience.)
 
J

Jack Klein

jtagpgmr said:






It means you forgot to #include <stdlib.h>

....and had you included those headers, the compiler would have warned
you immediately that you were calling the time() function incorrectly.
 
J

jtagpgmr

Thanks for the help the follwing was wrong..
added: #include <stdlib.h>

omitted: srand(time())
inserted: srand(rand())

The funny thing is this program excercise was taken straight from a
book..
 
R

Richard Heathfield

jtagpgmr said:
Thanks for the help the follwing was wrong..
added: #include <stdlib.h>

omitted: srand(time())

Why not just *fix* it instead of removing it?
inserted: srand(rand())

Why would you want to do /that/?
The funny thing is this program excercise was taken straight from a
book..

Highly amusing.
 
S

Simon Biber

jtagpgmr said:
Thanks for the help the follwing was wrong..
added: #include <stdlib.h>

omitted: srand(time())

This can be fixed by supplying a null pointer argument to the time function:
srand(time(0));

This will work if time(0) returns a number that has a reasonable range
of different values when converted to an unsigned int. This is far from
guaranteed by the C Standard but tends to work in practise.
inserted: srand(rand())

That is useless. The whole reason you wanted to change the seed value
was to get a different set of random numbers each time you ran the
program. Now the rand() function will return the same number each time
you run the program, which goes into the seed, and then the following
sequence will also be the same.
The funny thing is this program excercise was taken straight from a
book..

Either they, or you, left out the 0 or NULL from the call to time.
 
J

Jack Klein

Thanks for the help the follwing was wrong..
added: #include <stdlib.h>

omitted: srand(time())
inserted: srand(rand())

The funny thing is this program excercise was taken straight from a
book..

Get a better book. http://www.accu.org is one place to look for good
books on C programming. There are a lot of bad books out there, in
fact the great majority of them.
 
J

jtagpgmr

ahh... got it.. that works just fantastic.. Thanks final draft

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()

{

int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
srand(time(0));

printf("\nEnter number of questions to ask: ");
scanf("%d", &iNumQuestions);

for (x=0; x<iNumQuestions; x++) {


iRndNum1=rand()%10+1;
iRndNum2=rand()%10+1;

printf("\nWhat is %d x %d: ", iRndNum1, iRndNum2);
scanf("%d", &iResponse);

if (iResponse == iRndNum1*iRndNum2)
printf("\nCorrect!\n");

else
printf("\nThe correct answer was %d \n", iRndNum1*iRndNum2);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top