【发布时间】:2018-04-03 14:22:40
【问题描述】:
我正在尝试编写一个程序来比较用户输入的内容与存储在指针中的单词。代码如下
int c_s(char*, char*);
int main()
{
printf("Hate crime reporting system\n\n\n");
printf("If the crime you are reporting is an emergency,\nplease call 999, do
not proceed any further with this form\n\n\n\nPlease press enter to confirm
you have read the above and continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }
int a;
long long int g;
char *b, *e;
char *c = "witness";
char *d = "yes";
*c = (long long int) &g;
printf("Are you a victim or witness of the crime?\nPlease answer
victim/witness\n");}
scanf("%s", b);
int r = strcmp (b, c);
if(r == 0){
printf("Do you know who the witness is? Please answer yes/no\n");}
scanf("%s", d);
int f = strcmp(e, d);
if (d = "no") goto NEXT;
if(e == 0){
printf("Please enter their details including phone number and
address");}
NEXT:
当用户对“您是受害者还是证人”的问题回答“证人”时,我希望代码继续并询问下一个问题,如果他们回答“是或否”,则再次相同问题“你知道证人是谁吗?”。当我运行这段代码时,我得到一个溢出错误。我是编码新手,所以如果有人能提供如何完成这项工作的示例代码,我将不胜感激。我不确定是否我使用了错误的指针还是我应该使用数组?
还有人可以解释一下我将如何使这个 if 语句,这意味着如果用户输入“受害者”作为上述问题的答案,程序将继续“下一步:”
【问题讨论】:
-
没有与指针
b关联的内存。在有内存之前,您不能将数据放入该指针中。 -
*c = (long long int) &g;- 哎呀