By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

Please help me to find the error

P: n/a
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of seats as 0 and
the flight no. is also repeating. If any can tell why is this please
help me.

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<memory.h>
#include<iostream.h>

void reserv(), add(), view();

void main()
{
short choice1;

clrscr();
gotoxy(5,5);
printf("\t\t\tASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t\t----------------------------------\n");
printf("\n");
printf("\t\t Main Menue\n");
printf("\n\n");

gotoxy(10,10);
printf("1. Reservation\n");
gotoxy(10,12);
printf("2. Flight List\n");
gotoxy(10,14);
printf("3. Help\n");
gotoxy(10,16);
printf("4. Exit\n");
printf("\n\n\n");
gotoxy(10,20);
printf("Enter ur choice: ");
scanf("%i",&choice1);
printf("\n");
printf("\n");
printf("\n");
switch(choice1) {
case 1:
reserv();
break;

case 2:
printf("Flight List\n");
break;

case 3:
printf("Helping window\n");
break;

case 4:
printf("Exit from the window\n");
break;

default:
printf("Invalid\n");

getch();
}
}

/*------------------------------
reservation---------------------------------*/
void reserv()
{
short choice2;
clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n");
printf("\t\t Reservation\n");
gotoxy(10,10);
printf("1. Booking a seat\n");
gotoxy(10,12);
printf("2. Edit details\n");
gotoxy(10,14);
printf("3. View Details\n");
gotoxy(10,16);
printf("4. Cancel Reservation\n");
gotoxy(10,18);
printf("5. Search Details\n");
gotoxy(10,20);
printf("6. Exit\n");
printf("\n\n\n");
gotoxy(10,22);
printf("Enter ur choice: ");
scanf("%i",&choice2);
printf("\n");
printf("\n");
printf("\n");
switch(choice2) {
case 1:
add(); /*----------add reservation-----------*/
break;

case 2:
printf("Edit name\n"); /*-----------edit reservation---------*/
break;

case 3:
view(); /*-----------view reservation----------*/
break;

case 4:
printf("Delete"); /*------------delete reservation---------*/
break;

case 5:
printf("Search \n"); /*------------search reservation----------
*/
break;

case 6:
printf("Exit from the window\n");
break;

default:
printf("invalid\n");
}
}

/*-------------------------------
Add-----------------------------------------*/

void add()
{
char name[40];
int res_num=0,up;
int seat;

char flight[6];
char pass[8];

FILE*sfile;

if((sfile=fopen("c:\\air.dat","a+"))==NULL)
res_num=1;

else { do{ fscanf(sfile,"%i\t %s \t\t%s \t%i \t%s",&res_num,&name,&pass,&seat,
&flight);
}while(!feof(sfile));
res_num+=1;
}

clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
gotoxy(25,6);
printf("Booking a seat");
printf("\n\n\n\n");

printf("Reservation no: %04i ",res_num);
printf("\nName : ");
fflush(stdin);
gets(name);
printf("passport no : ");
fflush(stdin);
gets(pass);
printf("number of seats: ");
scanf("%i",&seat);

printf("flight : ");
fflush(stdin);
gets(flight);

printf("\n\n Record Saved!");

fprintf(sfile,"%i \t%s \t\t%s \t%i \t%s\n",res_num,name,pass,seat,flight);
fclose(sfile);

do{ printf("\nPress [2] to go to reservation: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);

reserv();

}

/*-----------------------view
reservations------------------------------------*/
void view(void)
{
char name[40];
int res_num,up;
int seat;
char flight[6];
char parse[8];
FILE*sfile;

clrscr();

if((sfile=fopen("c:\\air.dat","a+"))==NULL)
printf("File Empty!");

else { printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n\n No Name\t\t NRIC \t No.of seats \t Flight\n");
printf("-------------------------------------------------\n");

while(!feof(sfile)) { fscanf(sfile,
"%04i \t%s \t\t%s \t%i \t%s",&res_num,&name,&parse,&seat,&flight);
if(feof(sfile))
break;
printf("%04i \t%s \t\t%s \t%i \t%s\n",res_num,name,parse,seat,flight);
}
}

fclose(sfile);

do{ printf("\n\n\nPress [2] to go to reservation: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);

reserv();
}

I think my problem is in add and view functions.
Please help me.

Oct 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Oct 30, 2:59 pm, Alami <al...@mymail.comwrote:
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of seats as 0 and
the flight no. is also repeating. If any can tell why is this please
help me.

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
not a standard C header
#include<memory.h>
You want stdlib.h, no doubt
#include<iostream.h>
If it were correct, this header would make it a C++ file, and not a C
file.
Of course, that would mean instead:
#include <iostream>
However, since you are not using a single feature of iostream, there
is not any need to include it anyway.
>
void reserv(), add(), view();

void main()
in both C and C++, main returns int, not void.
{
short choice1;

clrscr();
gotoxy(5,5);
From the C-FAQ:
19.4: How can I clear the screen?
How can I print text in color?
How can I move the cursor to a specific x, y position?

A: Such things depend on the terminal type (or display) you're
using. You will have to use a library such as termcap,
terminfo, or curses, or some system-specific routines, to
perform these operations. On MS-DOS systems, two functions
to look for are clrscr() and gotoxy().

For clearing the screen, a halfway portable solution is to
print
a form-feed character ('\f'), which will cause some displays
to
clear. Even more portable (albeit even more gunky) might be
to
print enough newlines to scroll everything away. As a last
resort, you could use system() (see question 19.27) to invoke
an operating system clear-screen command.

References: PCS Sec. 5.1.4 pp. 54-60, Sec. 5.1.5 pp. 60-62.
printf("\t\t\tASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t\t----------------------------------\n");
printf("\n");
printf("\t\t Main Menue\n");
printf("\n\n");

gotoxy(10,10);
printf("1. Reservation\n");
gotoxy(10,12);
printf("2. Flight List\n");
gotoxy(10,14);
printf("3. Help\n");
gotoxy(10,16);
printf("4. Exit\n");
printf("\n\n\n");
gotoxy(10,20);
printf("Enter ur choice: ");
scanf("%i",&choice1);
You should always check the return of scanf().
printf("\n");
printf("\n");
printf("\n");
switch(choice1) {
case 1:
reserv();
break;

case 2:
printf("Flight List\n");
break;

case 3:
printf("Helping window\n");
break;

case 4:
printf("Exit from the window\n");
break;

default:
printf("Invalid\n");

getch();
}

}

/*------------------------------
reservation---------------------------------*/
void reserv()
{
short choice2;
clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n");
printf("\t\t Reservation\n");
gotoxy(10,10);
printf("1. Booking a seat\n");
gotoxy(10,12);
printf("2. Edit details\n");
gotoxy(10,14);
printf("3. View Details\n");
gotoxy(10,16);
printf("4. Cancel Reservation\n");
gotoxy(10,18);
printf("5. Search Details\n");
gotoxy(10,20);
printf("6. Exit\n");
printf("\n\n\n");
gotoxy(10,22);
printf("Enter ur choice: ");
scanf("%i",&choice2);
printf("\n");
printf("\n");
printf("\n");
switch(choice2) {
case 1:
add(); /*----------add reservation-----------*/
break;

case 2:
printf("Edit name\n"); /*-----------edit reservation---------*/
break;

case 3:
view(); /*-----------view reservation----------*/
break;

case 4:
printf("Delete"); /*------------delete reservation---------*/
break;

case 5:
printf("Search \n"); /*------------search reservation----------
*/
break;

case 6:
printf("Exit from the window\n");
break;

default:
printf("invalid\n");
}

}

/*-------------------------------
Add-----------------------------------------*/

void add()
{
Did you know that when a function returns, automatic variables are
lost?
char name[40];
int res_num=0,up;
int seat;

char flight[6];
char pass[8];

FILE*sfile;

if((sfile=fopen("c:\\air.dat","a+"))==NULL)
res_num=1;

else { do{ fscanf(sfile,"%i\t %s \t\t%s \t%i \t%s",&res_num,&name,&pass,&seat,
&flight);
}while(!feof(sfile));
12.2: Why does the code

while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}

copy the last line twice?

A: In C, end-of-file is only indicated *after* an input routine
has
tried to read, and failed. (In other words, C's I/O is not
like
Pascal's.) Usually, you should just check the return value of
the input routine (in this case, fgets() will return NULL on
end-
of-file); often, you don't need to use feof() at all.

References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec.
7.9.7.1,
Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.
Also, fscanf() with a format specifier of %s is just as bad as gets().
res_num+=1;
}

clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
gotoxy(25,6);
printf("Booking a seat");
printf("\n\n\n\n");

printf("Reservation no: %04i ",res_num);
printf("\nName : ");
fflush(stdin);
gets(name);
printf("passport no : ");
fflush(stdin);
gets(pass);
>From the C-FAQ:
12.23: Why does everyone say not to use gets()?

A: Unlike fgets(), gets() cannot be told the size of the buffer
it's to read into, so it cannot be prevented from overflowing
that buffer. As a general rule, always use fgets(). See
question 7.1 for a code fragment illustrating the replacement
of
gets() with fgets().

References: Rationale Sec. 4.9.7.2; H&S Sec. 15.7 p. 356.
printf("number of seats: ");
scanf("%i",&seat);

printf("flight : ");
fflush(stdin);
gets(flight);

printf("\n\n Record Saved!");

fprintf(sfile,"%i \t%s \t\t%s \t%i \t%s\n",res_num,name,pass,seat,flight);
fclose(sfile);

do{ printf("\nPress [2] to go to reservation: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);

reserv();

}

/*-----------------------view
reservations------------------------------------*/
void view(void)
{
Did you know that when a function returns, automatic variables are
lost?
char name[40];
int res_num,up;
int seat;
char flight[6];
char parse[8];
FILE*sfile;

clrscr();

if((sfile=fopen("c:\\air.dat","a+"))==NULL)
printf("File Empty!");

else { printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n\n No Name\t\t NRIC \t No.of seats \t Flight\n");
printf("-------------------------------------------------\n");

while(!feof(sfile)) { fscanf(sfile,
"%04i \t%s \t\t%s \t%i \t%s",&res_num,&name,&parse,&seat,&flight);
if(feof(sfile))
break;
printf("%04i \t%s \t\t%s \t%i \t%s\n",res_num,name,parse,seat,flight);
}
}

fclose(sfile);

do{ printf("\n\n\nPress [2] to go to reservation: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);

reserv();

}

I think my problem is in add and view functions.
Please help me.
There is a long way to go with this project.

This might be a helpful start:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
From:
http://home.att.net/~jackklein/ctips01.html#safe_gets
*/
char *getsafe(char *buffer, int count)
{
char *result = buffer,
*np;
if ((buffer == NULL) || (count < 1))
result = NULL;
else if (count == 1)
*result = '\0';
else if ((result = fgets(buffer, count, stdin)) != NULL)
if (np = strchr(buffer, '\n'))
*np = '\0';
return result;
}

void reserv(void), add(void), view(void);

static const char *nl = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\ n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
#define gotoxy(x,y)
#define clrscr() puts(nl),puts(nl)

static char name[256];
static int res_num,
up;
static int seat;
static char flight[256];
static char pass[256];
static FILE *sfile;
Oct 30 '07 #2

P: n/a
Alami wrote:
>
I'm newdie in c programming. this is my first project in
programming. I have to write a program for a airline reservation.
this is what i have done yet. but when it runs it shows the number
of seats as 0 and the flight no. is also repeating. If any can
tell why is this please help me.

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<memory.h>
#include<iostream.h>
The last three of these files do not exist in standard C
>
void reserv(), add(), view();

void main()
This is illegal, main returns an int. Say and do so.

This newsgroup deals in the C language, as defined in the various C
standards (present and past). Things that are not defined in those
are off-topic. Similarly C++ things are off-topic (I suspect the
<iostream.h>). Please correct these things before posting code
here. There probably are more non-standardisms to correct, I
stopped reading.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 30 '07 #3

P: n/a
Alami wrote:
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of seats as 0 and
the flight no. is also repeating. If any can tell why is this please
help me.
<snip>

Did you not post this same program months ago, at least twice IIRC? Why
don't you redesign and rewrite the program?

Oct 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.