我正在运行此程序,并在输入我的名字后继续收到错误
" program1.exe已停止工作"
不知道为什么有任何帮助?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void create();
//void edit();
//void delete();
int main()
{
char choice;
printf("\n\t\t **********************\n\n");
printf("\n\t\t Train Booking Application\n");
printf("\n\t\t **********************\n\n");
printf("Select 1 to create a booking,\tSelect 2 to Edit booking,\tSelect 3 to Delete a booking\n");
scanf("%d",&choice);
if (choice == 1){
create();
}
return 0;
}
void create(){
char Fname,Sname;
printf("Please enter your First name:\n");
scanf ("%s",Fname);
printf("Please enter your Second name:\n");
}
答案 0 :(得分:0)
char用于读取一个字符。请改用字符串。 %d用于读取整数,但是您定义了。使用
int choice;
或使用
scanf("%s", &choice);
尝试使用开关进行选择
switch(choice){
case 1:
create();
break;
case default:
//default statement(s)
}
答案 1 :(得分:0)
请注意编译器警告。它会为你发现问题。建议改进计划意见:
#include <stdio.h>
#include <stdlib.h>
//#include <iostream> // C++ include, not needed!
void create();
int main()
{
int choice; // changed to int to match scanf("%d", char is to small to hold an `int`
printf("\n\t\t **********************\n\n");
printf("\n\t\t Train Booking Application\n");
printf("\n\t\t **********************\n\n");
printf("Select 1 to create a booking,\tSelect 2 to Edit booking,\tSelect 3 to Delete a booking\n");
scanf("%d", &choice);
switch (choice){ // switch is a better choice than nested ifs
case 1:
create();
break;
default:
printf("not implemented yet...\n");
break;
}
return 0;
}
void create(){
char Fname[256],Sname[256]; // changed to arrays to match scanf ("%s"
// char Fname, Sname with %s would destroy memory
printf("Please enter your First name:\n");
scanf ("%s",Fname);
printf("Please enter your Second name:\n");
scanf ("%s",Sname);
}
答案 2 :(得分:-1)
由于char只存储一个字符并且您的名字长于此字符,请尝试使用 char Fname [] 来存储您的全名。