【发布时间】:2011-06-01 10:16:14
【问题描述】:
我正在用 C 编写我的第一个程序,它给我带来了很多问题。 这相当简单;输入一个数字,输出将是斐波那契数列中的相应项,其中第一项和第二项为 1。只要我没有将除数字以外的任何东西作为输入,它最初就可以工作;字母或特殊字符导致分段错误。为了解决这个问题,我尝试拒绝所有非数字输入,并且由于我找不到执行此操作的函数,所以我自己制作了。不幸的是,它现在在给出数字输入时会出现分段错误,并且所有非数字输入都被读取为 26。
带有迂腐警告的编译器 gcc 只抱怨我的 cmets。 我使用 GDB 将分段错误缩小到:
return strtol(c, n, 10);
我们将不胜感激任何有助于识别问题并在下次避免问题方面的帮助。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
calcTerm(); //Run calcTerm()
return 0; //Return value & exit
}
int fibTerm(int term)
{
//Declare Variables
int a = 0;
int b = 1;
int next;
int x;
//Calculate the sequence
for (x = 0; x <= (term - 2); x++)
{
next = a + b;
a = b;
b = next;
}
return next; //return the requested output
}
int calcTerm()
{
//declare variables
int in;
char rawIn[256];
char **n;
int out;
printf("Input the term you want to find:\n"); //request input
//get input
fgets(rawIn, 3, stdin);
//define variables
in = isNumeric(rawIn); /*strtol(rawIn, n, 10) works*/
out = fibTerm(in);
//print result
printf("Term %i " "is %i", in, out);
}
int isNumeric(char test[256])
{
//declare variables
char validChars[10] = "0123456789"; //valid input characters
char *c = test;
char **n;
if (strpbrk(test, validChars)) //if input contains only valid characters ?
{
return strtol(c, n, 10); //return the input as an integer
//segmentation fault; strtol_l.c: no such file
}
else
{
printf("Please only input numbers."); //error message
}
}
【问题讨论】:
-
该程序对我来说运行正确
-
更新后的代码对我来说很好;这里和那里有一些奇怪的地方(例如在
isNumeric中使用sscanf,它基本上用c中找到的字符串的第一个单词覆盖validChars),但总的来说还可以。请注意,您当前的代码从 (0, 1) 开始计算斐波那契数列,而不是从 (1, 1) 开始。 -
我会尽力解决这个问题...感谢您的帮助。
标签: c segmentation-fault strtol