2016-04-21 15 views
-2

Ich hatte das funktioniert vor einer Sekunde, aber versehentlich brach es. Kann mir jemand helfen, es zu reparieren? Ich bekomme eine Segmentierungsfehler, also nehme ich an, dass ich irgendwann die Zeiger durcheinander gebracht habe. Es soll eine Reihe von Zufallszahlen abhängig von Benutzereingaben generieren.Brauchen Sie Hilfe beheben Segmentierung Fehler

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

Der Fehler tritt sofort bei der Ausführung auf.

+1

Sie möchten Seiten Programm auszuführen mit einem Debugger zumindest lernen, wo es stürzt ab. – alk

+1

@alk ... oder verwenden Sie Druckanweisungen an strategischen Orten. –

+0

Nun, ich erreiche nie die erste Druckanweisung im Programm. –

Antwort

0

Dieses Stück 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++; 
} 

bis TapInputs finden ausgeführt wird false, oder mit anderen Worten 0 zu sein. Dies geschieht nur, wenn Sie -1 als Eingabe an scanf("%d", &tap) geben. Bis dahin werden Sie weiterhin lesen und auch inkrementierencount.

Aber einige Zeilen weiter oben, haben Sie

erklärt
unsigned int taps[2]; 

und in Ihrer while Schleife tun Sie

*(taps+count)=tap; 

Also, wenn Sie tap oft genug gelesen haben, und halten Sie es zwischen 0 finden und 33 , bis Sie es finden -1, count wird genug erhöht haben, um Ihr Array außerhalb der Grenzen zu bekommen.

+0

Ich glaube nicht, das Programm kommt sogar auf diese Codezeile zurück. –

0

Ich denke, Problem ist in dieser Zeile, mySeed = atoi (argv [1]); Sie haben so etwas zu tun, Sie den Code aus, dass in einem Zustand, wenn setzen,

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