C 需要帮助修复分割错误吗
我一秒钟前还在工作,但不小心把它弄坏了。谁能帮我修一下吗?我得到了一个分段错误,所以我假设我在某个点上弄错了指针。它应该根据用户输入生成一组随机数C 需要帮助修复分割错误吗,c,pointers,fault,seed,C,Pointers,Fault,Seed,我一秒钟前还在工作,但不小心把它弄坏了。谁能帮我修一下吗?我得到了一个分段错误,所以我假设我在某个点上弄错了指针。它应该根据用户输入生成一组随机数 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { unsigned int mySeed; // creates our variables unsigned
#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);
}
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
unsigned int mySeed;//创建变量
无符号整数抽头[2];
无符号整数温度[2];
无符号int-myToggle;
FILE*fp;//打开文件
fp=fopen(“random.txt”,“w”);//将要打开的文件设置为与此文件相同
int TAP输入=1;
整数计数=0;
int tap;
int myNewNumber=0;
mySeed=atoi(argv[1]);
if(atoi(argv[1])>0)//检查负输入时出错。
{
printf(“请输入要使用的抽头:”;//提示用户输入抽头,然后确保它们在范围内
while(轻触输入)
{
scanf(“%d”,点击(&T));
如果((点击>0)和&(点击<33))
{
*(抽头+计数)=抽头;
}
否则,如果(点击==-1)//当我们找到-1时,我们会这样做
{
TAP输入=0;
}
否则,如果(点击>32)
{
出口(0);
}
计数++;
}
printf(“要生成多少个数字:”);//提示用户输入要使用的数字的数量
scanf(“%d”,&myNewNumber);
while(myNewNumber<0)//检查正输入时出错
{
printf(“您希望生成多少个数字:”);
scanf(“%d”,&myNewNumber);
}
printf(“\n随机数:”);
while(myNewNumber)//创建与上一步中用户输入的数字相等的数字
{
temp[0]=mySeed;//使temp1成为种子
temp[1]=mySeed;//使temp2成为种子
temp[0]=(temp[0]>>点击[0])&1;//检查并设置位
temp[1]=(temp[1]>>点击[1])&1;//检查并设置位
myToggle=(temp[0]^temp[1]);//这里我们对temp1和temp2进行异或运算
mySeed=(mySeed这段代码:
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++;
}
在你的循环中
*(taps+count)=tap;
所以,如果你已经读了足够多的点击并一直在0
和33
之间找到它,直到你找到它-1
,计数将增加到足以让你的数组出界。我认为问题就在这一行,mySeed=atoi(argv[1]);
你必须这样做,
你可以把它的代码放在if条件下
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);
}
}
}
if(agrc>1)
{
mySeed=atoi(argv[1]);
---------------------
--------------------
}
我已经测试过了…它正在工作
#包括
#包括
#包括
int main(int argc,char*argv[])
{
unsigned int mySeed;//创建变量
无符号整数抽头[2];
无符号整数温度[2];
无符号int-myToggle;
FILE*fp;//打开文件
fp=fopen(“random.txt”,“w”);//将要打开的文件设置为与此文件相同
int TAP输入=1;
整数计数=0;
int tap;
int myNewNumber=0;
如果(agrc>1)
{
mySeed=atoi(argv[1]);
if(atoi(argv[1])>0)//检查负输入时出错。
{
printf(“请输入要使用的抽头:”;//提示用户输入抽头,然后确保它们在范围内
while(轻触输入)
{
scanf(“%d”,点击(&T));
如果((点击>0)和&(点击<33))
{
*(抽头+计数)=抽头;
}
否则,如果(点击==-1)//当我们找到-1时,我们会这样做
{
TAP输入=0;
}
否则,如果(点击>32)
{
出口(0);
}
计数++;
}
printf(“要生成多少个数字:”);//提示用户输入要使用的数字的数量
scanf(“%d”,&myNewNumber);
while(myNewNumber<0)//检查正输入时出错
{
printf(“您希望生成多少个数字:”);
scanf(“%d”,&myNewNumber);
}
printf(“\n随机数:”);
while(myNewNumber)//创建与上一步中用户输入的数字相等的数字
{
temp[0]=mySeed;//使temp1成为种子
temp[1]=mySeed;//使temp2成为种子
temp[0]=(temp[0]>>点击[0])&1;//检查并设置位
temp[1]=(temp[1]>>点击[1])&1;//检查并设置位
myToggle=(temp[0]^temp[1]);//这里我们对temp1和temp2进行异或运算
我的种子=(我的意思是,您希望使用调试器运行端程序,以至少了解它在何处崩溃。@alk…或在策略位置使用print语句。我从来没有到达程序中的第一个print语句。@JohnDoe然后您只需将诊断print语句上移。无论我将其放置的位置有多高,在到达目标之前,我仍然会遇到分段错误。)打印语句。我认为程序甚至不会深入到那行代码。
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);
}
}
}