2016-04-21 2 views
-2

Je l'ai fait fonctionner il y a une seconde mais je l'ai accidentellement cassé. Quelqu'un peut-il m'aider à le réparer? Je reçois une erreur de segmentation, donc je suppose que j'ai raté les pointeurs à un moment donné. Il est censé générer un tas de nombres aléatoires en fonction de l'entrée de l'utilisateur.Besoin d'aide pour réparer Segmentation Fault

#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); 
    } 
} 

La panne survient immédiatement après l'exécution.

+1

Vous voulez exécuter le programme de côté à l'aide d'un débogueur apprendre à au moins où il se bloque. – alk

+1

@alk ... ou utilisez des instructions d'impression dans des endroits stratégiques. –

+0

Eh bien, je n'atteins jamais la première déclaration d'impression dans le programme. –

Répondre

0

Ce morceau de 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++; 
} 

est exécuté jusqu'à ce que vous trouverez TapInputs être false, autrement dit 0. Cela n'arrivera que si vous donnez -1 comme entrée à scanf("%d", &tap). Jusque-là, vous continuerez à lire et incrémentantcount.

Mais quelques lignes ci-dessus, vous avez déclaré

unsigned int taps[2]; 

et dans votre boucle while vous

*(taps+count)=tap; 

Donc, si vous avez lu tap suffisamment de fois et de garder le trouver entre 0 et 33 , jusqu'à ce que vous trouviez -1, count aura suffisamment augmenté pour obtenir votre tableau hors limites.

+0

Je ne pense pas que le programme même descend à cette ligne de code. –

0

Je pense que le problème est dans cette ligne, mySeed = atoi (argv [1]); vous devez faire quelque chose comme ça, vous pouvez mettre le code de ce dans un état si,

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); 
} 
} 
}