C:调试和维护;运行-不同的输出
我正在使用代码块16.01当我调试此代码时,它会显示正确的输出,但当我运行它时,它会显示错误的输出!如何解决这个问题C:调试和维护;运行-不同的输出,c,C,我正在使用代码块16.01当我调试此代码时,它会显示正确的输出,但当我运行它时,它会显示错误的输出!如何解决这个问题 int main() { char ch[100],var[100],val[100],tempVa[100]; int i = 0,j=0,count=0; while (1) { puts("Enter the expression (or (end) to exit):"); gets(ch);
int main()
{
char ch[100],var[100],val[100],tempVa[100];
int i = 0,j=0,count=0;
while (1)
{
puts("Enter the expression (or (end) to exit):");
gets(ch);
if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0)
exit(-1);
else if(2 == sscanf(ch,"%s = %s", var, val))
{ i = 0;
printf("Variable is : %s\t Value Before evaluating : %s\n",var, val);
while (i<=strlen(val))
{
while (val[i]!='-'&&val[i]!='%'&&val[i]!='/'&&val[i]!='*'&&val[i]!='+'&&i<strlen(val))
tempVa[j++]=val[i++];
i++;
for (count=0; count<strlen(tempVa); count++)
printf("%c", tempVa[count]);
for (count=strlen(tempVa); count>=0; count--)
tempVa[count]='\0';
j=0;
}
}
else
printf("Invalid!");
}
return 0;
}
intmain()
{
char ch[100]、var[100]、val[100]、tempVa[100];
int i=0,j=0,count=0;
而(1)
{
puts(“输入表达式(或(结束)以退出):”;
获取(ch);
如果(strcmp(ch,“end”)=0 | | strcmp(ch,“end”)=0)
出口(-1);
否则如果(2==sscanf(ch,“%s=%s”,var,val))
{i=0;
printf(“变量为:%s\t求值前的值:%s\n”,var,val);
而(i我测试了你的代码,它运行正常
编辑:
您应该导入string.h库(无论如何,您应该始终解决所有警告)
使用return-1,这就是main是int函数的原因
就像@joe在评论中所说的,您应该始终以“\0”结束字符串
代码:
#包括
#包括//编辑
int main()
{
char ch[100]、var[100]、val[100]、tempVa[100];
int i=0,j=0,count=0;
而(1)
{
放入(“\n输入表达式(或(结束)以退出):”;
获取(ch);
如果(strcmp(ch,“end”)=0 | | strcmp(ch,“end”)=0)
return-1;//编辑
否则如果(2==sscanf(ch,“%s=%s”,var,val))
{
i=0;
printf(“变量为:%s\t求值前的值:%s\n”,var,val);
而(i=0;计数--)
tempVa[count]='\0';
j=0;
}
}
其他的
printf(“无效!”);
}
返回0;
}
样本运行:
输入表达式(或(结束)退出):Hassan=Merna+Mohamed+Ahmed
变量为:评估前的Hassan值:Merna+Mohamed+Ahmed
MernaMohamedAhmed
输入表达式(或(结束)以退出):
您的图像无法工作。能否以文本格式写入输出?提示:将内容复制到其中后,您需要null terminatetempVa
。在调试中,它可能在开始时被清零,但在发行版中,它只是当时内存中的任何内容。@ErikW Run:debug:MernaMohamedAhmed@Joe`用于(计数=strlen(tempVa);count>=0;count--)tempVa[count]='\0';“我想我已经在这里将其置空了。另一张Run图片,有时有效,有时无效..我不知道原因是什么:(
#include <stdio.h>
#include <string.h> // edit
int main()
{
char ch[100], var[100], val[100], tempVa[100];
int i = 0, j = 0, count = 0;
while (1)
{
puts("\nEnter the expression (or (end) to exit):");
gets(ch);
if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0)
return -1; // edit
else if(2 == sscanf(ch, "%s = %s", var, val))
{
i = 0;
printf("Variable is : %s\t Value Before evaluating : %s\n", var, val);
while (i <= strlen(val))
{
while (val[i] != '-' && val[i] != '%' && val[i] != '/' && val[i] != '*' && val[i] != '+' && i < strlen(val))
tempVa[j++] = val[i++];
i++;
for (count = 0; count < strlen(tempVa); count++)
printf("%c", tempVa[count]);
for (count = strlen(tempVa); count >= 0; count--)
tempVa[count] = '\0';
j = 0;
}
}
else
printf("Invalid!");
}
return 0;
}