Originally Posted by
rewclaus
Here it is: <...>
First of all, you sould learn how to configure your compiler for a better analysis :
Code:
Compiling MAIN.C:
Error MAIN.C 28: main must have a return type of int
Warning MAIN.C 63: 'list' is declared but never used
Warning MAIN.C 105: Code has no effect
Warning MAIN.C 122: Void functions may not return a value
Warning MAIN.C 135: Code has no effect
Warning MAIN.C 145: Both return and return with a value used
Warning MAIN.C 162: Void functions may not return a value
Warning MAIN.C 166: Both return and return with a value used
Warning MAIN.C 178: 'ans' is assigned a value that is never used
That said, there are several flaws in your code, mainly a bad use of the input functions. I recommend to use solid input functions based on fgets() or fgetc(), and to stick them into some personal library.
Here is a revisited version of your code using such a library code (ful references given further).
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "ed/inc/io.h"
struct cd_struct
{
char title[30];
char artist[30];
};
#define MAX 50
#define BELL '\a'
#define FILENAME "CDs.dat"
/******************************/
static void clear_sc (void)
{
#if 1
/* MS-DOS/Windows */
system ("cls");
#else
/* Unix-like */
system ("clear");
#endif
}
/******************************/
static void disp_menu (void)
{
clear_sc ();
printf ("\t\t*** CD LIST MANAGER ***\n"
"\t\t ---------------\n\n\n\n"
"Do you want to:\n\n\n"
"\t1. Add a CD to your collection\n\n\n"
"\t2. Print info of CDs in your collection\n\n\n"
"\t3. Exit this program\n\n\n"
"What is your choice? ");
}
/******************************/
static void err_msg (char err_msg[])
{
printf ("\n\n%s\n", err_msg);
printf ("%c", BELL);
}
/******************************/
static int add_it (struct cd_struct const *p_cditem)
{
int ok = 1;
FILE *fp = fopen (FILENAME, "ab");
if (fp == NULL)
{
err_msg ("*** Disk error--please check disk drive ***");
ok = 0;
}
else
{
fwrite (p_cditem, sizeof *p_cditem, 1, fp);
fclose (fp);
}
return ok;
}
/******************************/
static void enter_cd (void)
{
struct cd_struct cditem;
int answ;
do
{
printf ("\n\n\n\n\nWhat is the TITLE? ");
get_s (cditem.title, sizeof cditem.title);
printf ("Who is the Artist? ");
get_s (cditem.artist, sizeof cditem.artist);
add_it (&cditem);
printf ("\n\nDo you want to enter another CD? (Y/N) ");
answ = get_c ();
}
while (toupper (answ) == 'Y');
}
/******************************/
static void pause_sc (void)
{
printf ("\nPress the Enter key to continue...");
(void) get_c ();
}
/******************************/
static void pr_data (struct cd_struct const *p_cditem)
{
printf ("\nTitle : %-30s\n", p_cditem->title);
printf ("Artist : %-30s\n", p_cditem->artist);
}
/******************************/
static int print_cd (void)
{
int ok = 1;
struct cd_struct cditem;
int linectr = 0;
size_t s = sizeof (struct cd_struct);
FILE *fp = fopen (FILENAME, "rb");
if (fp == NULL)
{
err_msg ("*** Read error--ensure file exists ***");
ok = 0;
}
else
{
do
{
if (fread (&cditem, sizeof (struct cd_struct), 1, fp) != s)
{
if (feof (fp))
{
break;
}
}
if (linectr > 20)
{
pause_sc ();
linectr = 0;
}
pr_data (&cditem);
linectr += 4;
}
while (!feof (fp));
fclose (fp);
printf ("\n- End of Collection -");
pause_sc ();
}
return ok;
}
/******************************/
int main (void)
{
int num;
do
{
disp_menu ();
{
long x;
get_l (&x);
num = (int) x;
}
switch (num)
{
case 1:
{
enter_cd ();
break;
}
case 2:
{
print_cd ();
break;
}
case 3:
{
break;
}
default:
{
err_msg ("*** You need to enter");
printf (" 1 through 3 ***");
break;
}
}
}
while (num != 3);
return;
}
The missing code is here:
http://mapage.noos.fr/emdel/clib.htm
Module IO
Feel free to ask for details.
Note also that storing raw structures is not portable in C. It only works by chance on a very closed system (same machine, same compiler, same options, same phases of moon...)
The usual way is to use a text format.