-2

I had this working a second ago but accidentally broke it. Can anyone help me fix it? I'm getting a Segmentation Fault so I assume I messed up the pointers at some point. It's supposed to generate a bunch of random numbers depending on user input.

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

int main(int argc, char *argv[])
{
    unsigned int mySeed; // creates our variables
    unsigned int taps[2];
    unsigned int temp[2];
    unsigned int myToggle;

    FILE *fp;//opens the file
    fp = fopen("random.txt", "w");//sets the file to open equal to this file
    int TapInputs = 1;
    int count = 0;
    int tap;
    int myNewNumber = 0;
    mySeed = atoi(argv[1]);

    if(atoi(argv[1]) > 0) //Error checking for negative inputs.
    {
    printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
    while(TapInputs)
    {
        scanf("%d",&tap);
        if((tap > 0)&&(tap < 33))
        {
            *(taps+count)=tap;
        }
        else if(tap == -1) // when we find -1 we do this
        {
            TapInputs = 0;
        }
        else if(tap > 32)
        {
            exit(0);
        }
        count++;
    }
    printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
    scanf("%d", &myNewNumber);
    while (myNewNumber < 0)// error checking for positive inputs
    {
        printf("How many numbers do you want to generate: ");
        scanf("%d", &myNewNumber); 
    }
    printf("\nRandom Numbers:");
    while(myNewNumber)//creates number equal to the user input number in the previous step
    {
    temp[0] = mySeed; // makes temp1 the seed
    temp[1] = mySeed; // makes temp2 the seed
    temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
    temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
    myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
    mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number

    fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file

    printf("\n%d", mySeed); // prints the number
    myNewNumber -= 1;
    }
    fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
    printf("\n");
    return 0;
    }
    else
    { // if the number the user input was 0 we will end our program
    exit(0);
    }
}

The fault happens immediately upon execution.

John Doe
  • 15
  • 1
  • 4
  • 1
    You want to run side program using a debugger to at least learn where it crashes. – alk Apr 21 '16 at 06:42
  • 1
    @alk ...or use print statements in strategic places. – August Karlstrom Apr 21 '16 at 07:02
  • Well I never reach the first print statement in the program. – John Doe Apr 21 '16 at 07:04
  • 1
    @JohnDoe Then you just move the diagnostic print statement up. – August Karlstrom Apr 21 '16 at 07:06
  • regardless of how high I place it I still get a segmentation fault before reaching the print statement. – John Doe Apr 21 '16 at 07:08
  • You also need to make sure *argc* >= 2 before you use *argv[1]*. – August Karlstrom Apr 21 '16 at 07:08
  • I really don't think it's anything like that. It was working fine until I decided to play with the variable names for readability. – John Doe Apr 21 '16 at 07:20
  • Just ***go*** for the debugger ... the one and only tool for developers (besides editor and compiler)! Do this ***now***! :-) – alk Apr 21 '16 at 07:22
  • Also please note that when printing to `stdout` (what `printf()` does) output most likely is not shown until a new-line (`\n`) has to be printed or `flush(stdout)` gets called. This is different when printing to `stderr` (where debug logging should go any ways).To use the latter replace `printf(...` by `fprintf(stderr, ...`. – alk Apr 21 '16 at 07:27

2 Answers2

0

This piece of code :

while(TapInputs)
{
    scanf("%d",&tap);
    if((tap > 0)&&(tap < 33))
    {
        *(taps+count)=tap;
    }
    else if(tap == -1) // when we find -1 we do this
    {
        TapInputs = 0;
    }
    else if(tap > 32)
    {
        exit(0);
    }
    count++;
}

is executed until you find TapInputs to be false, or in other words 0. This will happen only if you give -1 as an input to scanf("%d", &tap). Until then, you will keep reading and also incrementing count.

But some lines above, you have declared

unsigned int taps[2];

and in your while loop you do

*(taps+count)=tap;

So, if you have read tap enough times and keep finding it between 0 and 33, until you find it -1, count will have increased enough to get your array out of bounds.

Marievi
  • 4,951
  • 1
  • 16
  • 33
0

I think problem is in this line , mySeed = atoi(argv[1]); you have to do something like this, you can put the code from that in an if condition ,

if(agrc>1)
{
    mySeed = atoi(argv[1]);
    ---------------------
    --------------------     
}
I have tested...it is working


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

int main(int argc, char *argv[])
{
unsigned int mySeed; // creates our variables
unsigned int taps[2];
unsigned int temp[2];
unsigned int myToggle;

FILE *fp;//opens the file
fp = fopen("random.txt", "w");//sets the file to open equal to this file
int TapInputs = 1;
int count = 0;
int tap;
int myNewNumber = 0;
if(agrc>1)
{
mySeed = atoi(argv[1]);

if(atoi(argv[1]) > 0) //Error checking for negative inputs.
{
printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
while(TapInputs)
{
    scanf("%d",&tap);
    if((tap > 0)&&(tap < 33))
    {
        *(taps+count)=tap;
    }
    else if(tap == -1) // when we find -1 we do this
    {
        TapInputs = 0;
    }
    else if(tap > 32)
    {
        exit(0);
    }
    count++;
}
printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
scanf("%d", &myNewNumber);
while (myNewNumber < 0)// error checking for positive inputs
{
    printf("How many numbers do you want to generate: ");
    scanf("%d", &myNewNumber); 
}
printf("\nRandom Numbers:");
while(myNewNumber)//creates number equal to the user input number in the previous step
{
temp[0] = mySeed; // makes temp1 the seed
temp[1] = mySeed; // makes temp2 the seed
temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number

fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file

printf("\n%d", mySeed); // prints the number
myNewNumber -= 1;
}
fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
printf("\n");
return 0;
}
else
{ // if the number the user input was 0 we will end our program
exit(0);
}
}
}
BusyTraveller
  • 183
  • 3
  • 14