LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2008, 07:48 AM   #1
thunyiwe
LQ Newbie
 
Registered: Nov 2006
Distribution: RHEL4,5,6&7,Debian sarge,etch & Fedora 18,19,20
Posts: 28

Rep: Reputation: 15
compile error


HI When I try to compile my program this is the error that I am getting
Please note Iam still new to C.


DataBase.c: In function ‘main’:
DataBase.c:54: error: expected ‘while’ before ‘menu’
DataBase.c:173: error: expected declaration or statement at end of input

Here is the program.



/* An eletronic card catalog--3rd Imporvement */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 100

int menu(void);
void display(int i);
void author_search(void);
void title_search(void);
void enter(void);
void save(void);
void load(void);

struct book_type {
unsigned date; //Coptright date
unsigned char ed; //edition
unsigned pages; //length of book
};

struct catalog {
char name[80]; //author name
char title[80]; //title
char pub[80]; //publisher
struct book_type book; //mechanical info
} cat[MAX];

int top = 0; //last location used

int main(void)
{
int choice;

load(); //read catalog

do {
choice = menu();
switch(choice) {
case 1: enter(); //enter books
break;
case 2: author_search(); //search by author
break;
case 3: title_search(); // search by title
break;
case 4: save();
break;
}
while(choice!=5);
return 0;
}

/*Return a menu selection */
menu(void)
{
int i;
char str[80];

printf("Card catalog.\n");
printf(" 1. Enter\n");
printf(" 2. Search by author.\n");
printf(" 3. Search by title.\n");
printf(" 4. Save catalog.\n");
printf(" 5. Quit");

do {
printf("Choose your selection: ");
gets(str);
i = atoi(str);
printf("\n");
} while(i<1 || i>5);
return i;
}

/*Enter books into database */
void enter(void)
{
int i;
char temp[80];

for(i=top; i<MAX; i++) {
printf("Enter author name Enter to quit): ");
gets(cat[i].name);
if(!*cat[i].name) break;
printf("Enter title: ");
gets(cat[i].title);
printf("Enter publisher: ");
gets(cat[i].pub);
printf("Enter Copyright date: ");
gets(temp);
cat[i].book.date = (unsigned) atoi(temp);
printf("Enter edition: ");
gets(temp);
cat[i].book.ed = (unsigned char) atoi(temp);
printf("Enter number of pages: ");
gets(temp);
cat[i].book.pages = (unsigned) atoi(temp);
}
top = i;
}

/*Search by author */
void search(void)
{
char name[80];
int i, found;
printf("Name: ");
gets(name);
found = 0 ;
for(i=0; i<top; i++)
if(!strcmp(name, cat[i].name)) {
display(i);
found = 1;
printf("\n");
}

if(!found) printf("Not Found!\n");
}

/* Display catalog entry */
void display(int i)
{
printf("%s\n", cat[i].title);
printf("by %s\n", cat[i].name);
printf("Published by %s\n", cat[i].pub);
printf("Copyright: %u, edition: %u\n", cat[i].book.date, cat[i].book.ed);
printf("Pages: %u\n", cat[i].book.pages);
}

/* Load the catalog file */
void load(void)
{
FILE *fp;

if((fp = fopen("catalog", "rb"))==NULL) {
printf("Catalog file not on the disk.\n");

return;
}

if(fread(&top, sizeof top, 1, fp) != 1) { //read count
printf("Error reading count");
exit(1);
}
if(fread(cat, sizeof cat, 1, fp) != 1) { // read data
printf("Error reading catalog data.\n");
exit(1);
}

fclose (fp);
}

/*Save the catalog file */
void save(void)
{
FILE *fp;

if((fp = fopen("catalog", "wb"))==NULL) {
printf("Cannot open catalog file.\n");
exit(1);
}

if(fwrite(&top, sizeof top, 1, fp) !=1) { //write count
printf("error writing count.\n");
exit(1);
}
if(fwrite(cat, sizeof cat, 1, fp) !=1) { // write data
printf("Error writing catalog data.\n");
exit(1);
}

fclose (fp);
}
 
Old 11-12-2008, 12:29 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by thunyiwe View Post
Code:
	do {
		choice = menu();
		switch(choice) {
			case 1: enter();		//enter books
				break;
			case 2: author_search();	//search by author
				break;
			case 3:	title_search();		// search by title
				break;
			case 4:	save();	
				break;
			}
		while(choice!=5);
	return 0;
}
Firstly, you didn't need to include all that code, only the relevant bit. Secondly, please use code tags to preserve indentation.

The problem appears to be that you're missing a curly brace (}). The one above your "while(choice != 5)" matches with the one on the line "switch(choice) {", so you need one to match with the do in your do while loop, i.e. do while loops look like this:

Code:
do {
 // Code in here
} while(condition)
 
Old 11-26-2008, 11:12 AM   #3
nehaandrew
Member
 
Registered: Nov 2008
Posts: 53

Rep: Reputation: 15
The error reported is generally encountered when there are syntax errors (like missing } and ; in the code). Using an IDE (like Eclipse) should help you zero in on the fix for such simple issues fast.


Linux

Last edited by nehaandrew; 11-30-2008 at 01:19 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Asterisk compile error make: *** [makeopts] Error 1 crmexclusive Debian 3 02-24-2012 06:33 PM
Conmpile error wen compile php:configure: error: libpng.(a|so) not found tanveer Linux - Software 5 02-03-2009 06:13 AM
rpm compile time error:make error ashmita04 Red Hat 1 07-09-2007 03:44 AM
Compile madwifi, ... compile error , how can i do. ERBRMN Linux - Networking 3 03-08-2006 07:56 PM
compile error: #error unknown processor family kmack2001 Linux - Newbie 0 02-14-2004 11:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration