我正在用c写一个简单的程序,所以我可以更好地理解语言,但是我有一个奇怪的问题。 正如你从下面的代码中看到的,我只有一个循环,当我插入255作为一个值时它退出。问题是,当我选择第一(插入选项)后,我插入一个名字在节目开始像一个循环,并给了我所有的时间选择画面......C中的循环问题
#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");
}
是的,这将是。但是你不想每次都选择屏幕? – 2011-05-03 14:05:29
@Doug T.Nope它不是作业。我知道Java,现在我正在学习C ... – Stelios 2011-05-03 14:10:32