gpt4 book ai didi

c - 使用动态输入时分配的垃圾值;用C语言

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:35 24 4
gpt4 key购买 nike

问题在于,在输出中,属性 sp、bp、temp、cold、fever 和 stomach 被分配了垃圾值,而且为 symp[i] 输入的字符串没有正确存储。这不是完整的程序,它还有一些补充需要完成。

程序如下..头文件是stdio.h和conio.h

#include<stdio.h>
#include<conio.h>
void main()
{
char * symp[10];
int n=0,i,sp,dp,cold,fever,stomach;
float temp;
clrscr();
printf("Enter your body temperature: ");
scanf("%f",&temp);
printf("Enter your systolic BP: ");
scanf("%d",&sp);
printf("Enter your diastolic BP: ");
scanf("%d",&dp);
printf("Enter the no of symptoms: ");
scanf("%d",&n);
printf("Enter the symptoms you have, one by one\n");
fever=cold=stomach=0;
for(i=0;i<n;i++)
{
scanf("%s",symp[i]);
printf("i=%d\tn=%d\n",i,n);
if(symp[i]=="cough")
{ cold=cold+1;}
if(symp[i]=="sneezing")
{ cold=cold+1;}
if(symp[i]=="running_nose")
{ cold=cold+1;}
if(symp[i]=="headache")
{ cold=cold+1;fever=fever+1;}
if(symp[i]=="chill")
{ fever=fever+1; }
if(symp[i]=="weakness")
{ fever=fever+1; }
if(symp[i]=="stomach_pain")
{ stomach=stomach+1; }
if(symp[i]=="diarrhoea")
{ stomach=stomach+1;}
if(symp[i]=="vomiting")
{ stomach=stomach+1;fever=fever+1;}
if(symp[i]=="throat_pain")
{ cold=cold+1;}
if(symp[i]=="body_pain")
{ fever=fever+1;}
}
printf("Your temperature is: %f\n",temp);
printf("Your BP is: %d/%d\n",sp,dp);
printf("The symptoms are:\n");
for(i=0;i<n;i++)
{
printf("%s\n",symp[i]);
}
printf("cold: %d\nfever: %d\nstomach: %d",cold,fever,stomach);
getch();
}

最佳答案

你不能将 char[]== 的静态字符串进行比较,你必须使用 strcmpstrncmp,类似于:

 if (strcmp(symp[i], "headache"))

您还必须将一个地址传递给scanf("%s", ...),例如:

scanf("%s", &symp[i]);

关于c - 使用动态输入时分配的垃圾值;用C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29103286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com