Hey. I am writing a program with menu using curses.h. It works well BUT stuck when i add info(e.g. name, author). I rack one's brains a lot but have no idea why it happens.
P.S. interesting thing. If you add after line 60 system("cls"); it will works, but if you add 3 record or more to file program will quit with dump record
And sorry for my bad English.
[SPOILER]
#include <curses.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include "my_header.h"
#define FILENAME "books.txt"
int i, g_Choice = 0,x,y;
int books_count , admin_count ;
char cKey, newstroke[100];
FILE *booklist;
char *main_menu[] = {
"1. Admin menu",
"2. Tasks",
"3. Display all",
"4. Exit",
};
char *task_menu[] = {
" Task 1",
" Task 2",
" Task 3",
};
char *admin_menu[] = {
" Register an Admin",
" Enter as Admin",
" Add",
" Delete",
};
struct admin
{
char login[32];
int password;
} *adm;
struct book
{
char name[33];
char author[33];
int year;
char publisher[33];
float price;
} *b;
void init()
{
initscr();// Initiliazation
cbreak(); // Line buffering disabled]
start_color();
keypad(stdscr,TRUE);
attron(A_BOLD); // Bold text
}
void menu(char **what_menu, int n_choices)
{
int menu_pointer = 1;
int ch = 0;
g_Choice = 0;
show_menu(menu_pointer, n_choices, what_menu); // Look my_header.h
while(TRUE)
{
ch = getch();
switch(ch)
{ case KEY_UP:
if(menu_pointer == 1)
menu_pointer = n_choices;
else
--menu_pointer;
break;
case KEY_DOWN:
if(menu_pointer == n_choices)
menu_pointer = 1;
else
++menu_pointer;
break;
case 10: // "Enter" key
g_Choice = menu_pointer;
break;
}
show_menu(menu_pointer, n_choices,what_menu);
if(g_Choice != 0) /* Choice is done,come out of the infinite loop */
break;
}
refresh();
}
void module_add()
{
clear();
mvprintw(1,1,"Please Enter the information about the book. ");
refresh();
booklist = fopen(FILENAME, "a+");
books_count = precache(booklist);
printf("%d",books_count);
for(i=0,cKey='0';cKey!='N';i++,books_count++)
{
printf("\n\n\nName: "); gets(b[i].name);
printf("\nAuthor: "); gets(b[i].author);
printf("\nPublisher: "); gets(b[i].publisher);
printf("\nYear: "); scanf("%d",&b[i].year);
printf("\nPrice: "); scanf("%f",&b[i].price);
printf("Continue ? (type Y/N) "); scanf("%s",&cKey);
fprintf(booklist,"\n %d. %s ==== %s ==== %s ==== %d ==== %f",
books_count+1,b[i].name, b[i].author,b[i].publisher, b[i].year,b[i].price);
fflush(stdin); // Clear the buf
}
fclose(booklist);
}
int main()
{
b = new book[books_count];
adm = new admin[admin_count];
init();
do
{
menu(main_menu, 4);
switch (g_Choice)
{
case 1 : menu(admin_menu,sizeof(admin_menu)/sizeof(char *));
switch(g_Choice)
{
case 3: module_add(); break;
}
break;
case 2 : menu(task_menu, sizeof(task_menu)/sizeof(char *)); break;
default : clear(); printf("Not available yet"); break;
}
}
while(TRUE);
getch();
return 0;
}
[/SPOILER]