【发布时间】:2012-07-15 16:11:52
【问题描述】:
我正在开发一个代数应用程序,它与图形计算器的功能非常相似。
struct quotient NewQuotient()
{
struct quotient temp;
printf("Enter the numerator\n");
scanf("%d", &temp.numerator);
printf("Enter the denominator\n");
scanf("%d", &temp.denominator);
return temp;
}
char NewVarname()
{
char temp;
printf("Enter the variable letter: \n");
scanf("%c", &temp);
return temp;
}
struct term NewTerm()
{
struct term temp;
printf("Enter the coefficient: ");
temp.coefficient = NewQuotient();
printf("Enter the variable name: \n");
temp.varname = NewVarname();
printf("Enter the power: ");
temp.power = NewQuotient();
return temp;
}
程序得到了系数和幂的商就好了,但是获取变量名有问题。我认为在 NewQuotient 中的 scanf 语句之后缓冲区中有一个空字符,但如果有,我不知道如何找到它们或如何修复它们。任何帮助表示赞赏。
【问题讨论】:
-
不应该
scanf("%c", temp.varname);是scanf("%c", &(temp.varname));吗? -
你为什么在一个地方使用gets()而在另一个地方使用scanf()?除此之外,如果您要突出显示您输入的部分,以便更容易区分,这可能会有所帮助。
-
@Aj_76 我根据我的理解格式化了问题;如果我猜错了,请回复和/或澄清。
-
如果我使用 printf("%s", entry),它最终会转储核心。谢谢anatolyg,我忘了说清楚输入/输出。重要的是程序提示输入电源,但用户没有机会输入任何内容
-
不要使用gets() 或scanf()。使用 fgets()。您的应用程序可能不会像这样更改,但这是一个很好的做法。它将使您免于所有未来的缓冲区溢出错误和头发撕裂。