【发布时间】:2011-08-17 18:06:57
【问题描述】:
我正在用 c 编写一个简单的程序,这样我可以更好地理解该语言,但我遇到了一个奇怪的问题。 正如您从下面的代码中看到的那样,当我将 255 作为值插入时,我只有一个循环退出。问题是,当我选择第一个(插入选项)并插入名称后,程序会开始循环,并一直给我选择屏幕...
#include<stdio.h>
#include<stdlib.h>
struct student{
char *name;
int id;
};
void insertStudent(void);
struct student * init(void);
int main(){
struct student *p;
int selectionCode=0;
while(selectionCode!=255){
printf("\nInsert students:1");
printf("\nDisplay students:2");
printf("\nExit:255");
printf("\n\nEnter selection:");
scanf("%d",&selectionCode);
p=init();
switch(selectionCode){
case 1:
insertStudent();
//printf("1\n");
break;
case 2:
//printf("2\n");
break;
case 255:
break;
}
}
//p->name="stelios";
//p->id=0;
//printf("Name:%s ID:%d",p->name,p->id);
//free(p);
//p=NULL;
return 0;
}
struct student *init(void)
{
struct student *p;
p=(struct student *)malloc(sizeof(struct student));
return p;
}
void insertStudent(void){
struct student *p;
p=init();
printf("Enter Name:");
scanf("%s",p->name);//return 1;
printf("Enter ID:");
scanf("%d",&p->id);
//printf("test");
}
【问题讨论】:
-
是的,这将是。但是您不想每次都获得选择屏幕?
-
@Doug T.Nope 这不是家庭作业。我知道 Java,现在我正在学习 c...