c언어 파일 입출력 질문 드립니다
조회수 531회
pragma warning(disable:4996)
include
include
include
using namespace std;
struct book { int id; char name[30]; }; struct book books[10]; struct member { int id; char name[30]; }; struct member members[5];
void main() { int n; int id; int i;
char fname[] = "book.txt";
FILE* fp;
fp = fopen("book.txt", "w");
if (fp == NULL)
{
printf("파일 에러");
}
else
{
for (i = 0; i < 3; i++)
{
fprintf(fp, "%d\t", i + 1);
printf("%d. 책 번호 : \b", i + 1);
scanf("&s", books[i].id);
fprintf(fp, "%s\t", books[i].id);
printf("%d. 책 이름 : \b", i + 1);
scanf_s("&s", books[i].name);
fprintf(fp, "%s\t", books[i].name);
}
} fclose(fp);
while (true) {
cout << "Select the menu" << endl;
cin >> n;
switch (n) {
case 1:
break;
case 2:
cout << "Enter books id" << endl;
cin >> id;
for (i = 0; i < 3; i++) {
if (books[i].id == id)
cout << books[i].id << books[i].name << endl;
}break;
case 3:
cout << "Enter books id" << endl;
cin >> id;
for (i = 0; i < 3; i++) {
if (books[i].id == id) {
cout << "enter new data" << endl;
cin >> books[i].id >> books[i].name;
cout << books[i].id << books[i].name << endl;
}break;
case 4:
cout << "Enter books id" << endl;
cin >> id;
for (i = 0; i < 3; i++)
{
if (books[i].id == id) {
books[i].id == NULL;
books[i].name == NULL;
cout << "It's data is deleted" << endl;
}
}
case 5:
cout << "Enter new data" << endl;
}
}
}
}
이런식으로 코드를 작성해 보았습니다. 다름이 아니라 파일에 데이터값을 입력을 한 후 select menu로 넘어가야 하는데 1번의 책 번호를 입력하자마자 바로 1번의 책 이름은 입력도 받아지지 않은채 저절로 select menu로 넘어가게 됩니다. 어떻게 해야 할까요? 또한 case 5는 새로운 책을 추가하는 것인데 코드작성방법 부탁드립니다.. case 4에서는 data를 삭제해야하는건데 그것도 맞는지 한번 확인 부탁드려ㅑ요~
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력