473,503 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me to catch this error

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<string.h>
void reserv(void);
void add(void);
void view(void);

void main(void)

{
int 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(void)
{
int 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(void)
{
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();

}

Feb 27 '07 #1
22 3218
On Feb 27, 2:53 pm, "Amali" <amalikarunanay...@gmail.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>
#include<string.h>
void reserv(void);
void add(void);
void view(void);

void main(void)

{
int 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(void)
{
int 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(void)
{
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();

}- Hide quoted text -

- Show quoted text -
I think my problem is in add and view functions.
Please help me.

Feb 27 '07 #2
Amali said:
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>

The main function returns int, not void. The scanf function returns a
useful value, which you should check. The behaviour of fflush is
defined only for streams open for output or update, not for input. The
gets function is dangerous and should never, ever be used. Use fgets
instead (and read your documentation to understand how it works). Your
fscanf loop keeps overwriting previously-read values until it hits the
end of the file, which almost certainly is not what you want - look up
arrays. The feof function is almost never the right way to control a
file-reading loop. Check the return value of the file-reading function
instead.

Fix those, and let us know whether that fixed your problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 27 '07 #3
On Feb 27, 1:59 pm, "Amali" <amalikarunanay...@gmail.comwrote:
On Feb 27, 2:53 pm, "Amali" <amalikarunanay...@gmail.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>
#include<string.h>
void reserv(void);
void add(void);
void view(void);
void main(void)
{
int 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(void)
{
int 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(void)
{
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();
}- Hide quoted text -
- Show quoted text -

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

(1) conio.h, clrscr, gotoxy [and I think getch() too] are not
portable. They are not working in my gcc compiler.
(2) When you open a file in a+ mode, by default file stream is at the
end thats the reason you are not able to read the records or view the
records as stream is already at the end. Go here for details on fopen.

http://www.codecogs.com/reference/st...hp?alias=fopen

When I changed the code to open the file in "r" mode I could view the
recodds. IMHO it would be better if you use just "\t" as delimiter and
not some complex string.

(3) fflush does not work on input streams. So fix it before you can
add.

Again many a times debugging by self gives more revelations about the
mistake and the learning from this way is going to be more permanent!

Arvind
purohit underscore arvind 8 yahoo dot com

Feb 27 '07 #4
Amali 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<string.h>
void reserv(void);
void add(void);
void view(void);

void main(void)
Well, so far you have used a nonexistent header (conio.h) and
failed to correctly declare main as returning an int. No further
reading need be done.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 27 '07 #5
On Feb 27, 5:30 pm, CBFalconer <cbfalco...@yahoo.comwrote:
Amali 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<string.h>
void reserv(void);
void add(void);
void view(void);
void main(void)

Well, so far you have used a nonexistent header (conio.h) and
failed to correctly declare main as returning an int. No further
reading need be done.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>- Hide quoted text -

- Show quoted text -
Thank u very much!!!!!

Feb 27 '07 #6
Groovy hepcat Amali was jivin' on 27 Feb 2007 00:53:59 -0800 in
comp.lang.c.
Please help me to catch this error's a cool scene! Dig it!
>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>
Non-standard, non-portable, useless header.
>#include<string.h>
void reserv(void);
void add(void);
void view(void);

void main(void)
That should be int main(void). The fact that you have used void
main() indicates to me that you have not lurked here for any length of
time nor read the FAQ. This is very rude! When entering a newsgroup
for the first time it is customary (and expected) to read a month or
two of posts before posting (lurk) and also to read the newsgroup's
FAQ list. Please don't post to comp.lang.c again until you have done
so. You'll find the FAQ at http://www.eskimo.com/~scs/C-faq/top.html.
>{
int choice1;

clrscr();
gotoxy(5,5);
Non-standard, non-portable, useless function calls.
> 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);
Non-standard, non-portable, useless function call.
> printf("1. Reservation\n");
gotoxy(10,12);
Non-standard, non-portable, useless function call.
> printf("2. Flight List\n");
gotoxy(10,14);
Non-standard, non-portable, useless function call.
> printf("3. Help\n");
gotoxy(10,16);
Non-standard, non-portable, useless function call.
> printf("4. Exit\n");
printf("\n\n\n");
gotoxy(10,20);
Non-standard, non-portable, useless function call.
> printf("Enter ur choice: ");
scanf("%i",&choice1);
The FAQ will tell you why scanf() is best to avoid, and what to use
instead.
> 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();
Non-standard, non-portable, useless function call.
> }
}
/*------------------------------
reservation---------------------------------*/
void reserv(void)
{
int choice2;
clrscr();
gotoxy(5,3);
Non-standard, non-portable, useless function calls.
> printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n");
printf("\t\t Reservation\n");
gotoxy(10,10);
Non-standard, non-portable, useless function call.
> printf("1. Booking a seat\n");
gotoxy(10,12);
Non-standard, non-portable, useless function call.
> printf("2. Edit details\n");
gotoxy(10,14);
Non-standard, non-portable, useless function call.
> printf("3. View Details\n");
gotoxy(10,16);
Non-standard, non-portable, useless function call.
> printf("4. Cancel Reservation\n");
gotoxy(10,18);
Non-standard, non-portable, useless function call.
> printf("5. Search Details\n");
gotoxy(10,20);
Non-standard, non-portable, useless function call.
> printf("6. Exit\n");
printf("\n\n\n");
gotoxy(10,22);
Non-standard, non-portable, useless function call.
> printf("Enter ur choice: ");
scanf("%i",&choice2);
See above.
> 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(void)
{
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)
If the file can be opened, it is set to append, for writing at the
end, and possibly reading at the end. Before you can be sure of
reading anything useful, you may need to reposition the file position
indicator for this stream.
> res_num=1;

else
{
do{
fscanf(sfile,"%i\t %s \t\t%s \t%i \t%s
All those tab escape sequences and spaces in the format string are
not necessary. If you need to skip white space, a single space will do
in the format string.
Also, be careful of posting long lines here. They have a tendancy to
wrap. You can break long strings up into shorter ones accross multiple
lines, and they will be concatenated by the preprocessor. For example:

"This is a string. "
"And this is part of the same string, "
"even though it's broken into multiple "
"string literals."

The above four string literals will be concatenated into a single
string.
>",&res_num,&name,&pass,&seat,
Since name, pass and flight are arrays of char and decay into
pointers to their initial elements, the & operators applied these are
entirely superfluous. In fact, they're just plain wrong. Leave them
out.
Also, you'll no doubt want to limit the number of characters that
can be stored in each of those arrays. So you're going to need field
width specifiers in your format string.
> &flight);
You're not doing anything with the data you've just read in. Why
bother reading it in, then?
> }while(!feof(sfile));
Using feof() isn't the best way of controlling an input loop. The
FAQ says why.
Remember, fscanf() returns a value. Use that to control your loop.
For example:

while(5 == fscanf(sfile, "%i %39s %7s %i %5s",
&res_name, name, pass, &seat, flight))
;
> res_num+=1;

}
Here ends your else clause. What follows is, then, always executed,
regardless of the if(...) statement above. Even things that rely on
the else clause being executed, such as closing the file (since the
else clause being executed relys on the file being successfully
opened).
> clrscr();
gotoxy(5,3);
Non-standard, non-portable, useless function calls.
> printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
gotoxy(25,6);
Non-standard, non-portable, useless function call.
> printf("Booking a seat");
printf("\n\n\n\n");

printf("Reservation no: %04i ",res_num);
printf("\nName : ");
fflush(stdin);
BANG! Undefined behaviour! fflush() is defined to flush output to an
output stream. Passing an input stream, such as stdin, to fflush()
causes undefined behaviour.
> gets(name);
NEVER, never, never, never, never use gets(). It can never be used
safely. Read the FAQ, and use fgets() instead.
> printf("passport no : ");
fflush(stdin);
gets(pass);
See above.
> printf("number of seats: ");
scanf("%i",&seat);
See above.
> printf("flight : ");
fflush(stdin);
gets(flight);
See above.
> 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);
See above.
> do{
printf("\nPress [2] to go to reservation: ");
scanf("%i",&up);
See above.
> if(up!=2)
printf("Invalid Entry");
}while(up!=2);
reserv();
You're calling reserv() here? Are you sure that's what you want to
do? I see no reason reserv() should be recursive. Perhaps you just
want to return here, which would put you back in reserv() at the point
after add() was called.
>}
/*-----------------------view
reservations------------------------------------*/
void view(void)
{
char name[40];
int res_num,up;
int seat;
char flight[6];
char parse[8];
FILE*sfile;

clrscr();
Non-standard, non-portable, useless function call.
> 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))
See above.
> {
fscanf(sfile,"%04i \t%s \t\t%s \t%i \t
%s",&res_num,&name,&parse,&seat,&flight);
See above.
> if(feof(sfile))
break;
If you'd use fscanf()'s return value to control the loop, you
wouldn't have to bother with this needless feof() test here. You
wouldn't need to bother with feof() at all. See the FAQ.
> printf("%04i \t%s \t\t%s \t%i \t%s
\n",res_num,name,parse,seat,flight);
Poor indentation, confusing, making it harder to read and understand
the code. Please indent consistently. Don't mix tabs and spaces,
especially in Usenet. In fact, spaces are much prefered.
Also, see above about line wrapping and string concatenation.
> }
}

fclose(sfile);
Since this fclose() call is outside the else clause of the if(...)
statement, it is executed whether the file could be opened or not.
BANG! Undefined behaviour again, if the file failed to open!
> do{
printf("\n\n\nPress [2] to go to reservation: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);
What's all this rigmarole in aide of? Why annoy the user by making
him/her press "2" and "Enter" (without telling him/her about the
latter)? Just return. If you must wait, then just wait for "Enter".
For example:

printf("Press \"Enter\" to continue...\n");
skip2nl(stdin);

where skip2nl() could be defined as:

void skip2nl(FILE *fp)
{
int c;

while(EOF != (c = fgetc(fp)) && '\n' != c)
;
}

This would skip all input up to EOF or a newline in a given input
stream. This could be a very useful function, not just for waiting for
"Enter" to be pressed. But all your fflush(stdin)s can be replaced
with skip2nl(stdin). In fact, you could even make it better by
returning a value, so it could then indicate whether EOF has been
read. All you'd need to do is have it return c. Then a calling
function could test the return value for EOF, and take some
appropriate action. (But I digress.)
> reserv();
Again you're recursing into reserv(). What for? No doubt you really
just need to return.
>}
--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Mar 3 '07 #7
On Mar 3, 7:36 am, phayw...@alphalink.com.au.NO.SPAM (Peter "Shaggy"
Haywood) wrote:
Groovy hepcat Amali was jivin' on 27 Feb 2007 00:53:59 -0800 in
comp.lang.c.
Please help me to catch this error's a cool scene! Dig it!
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>

Non-standard, non-portable, useless header.
#include<string.h>
void reserv(void);
void add(void);
void view(void);
void main(void)

That should be int main(void). The fact that you have used void
main() indicates to me that you have not lurked here for any length of
time nor read the FAQ. This is very rude! When entering a newsgroup
for the first time it is customary (and expected) to read a month or
two of posts before posting (lurk) and also to read the newsgroup's
FAQ list. Please don't post to comp.lang.c again until you have done
so. You'll find the FAQ athttp://www.eskimo.com/~scs/C-faq/top.html.
{
int choice1;
clrscr();
gotoxy(5,5);

Non-standard, non-portable, useless function calls.
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);

Non-standard, non-portable, useless function call.
printf("1. Reservation\n");
gotoxy(10,12);

Non-standard, non-portable, useless function call.
printf("2. Flight List\n");
gotoxy(10,14);

Non-standard, non-portable, useless function call.
printf("3. Help\n");
gotoxy(10,16);

Non-standard, non-portable, useless function call.
printf("4. Exit\n");
printf("\n\n\n");
gotoxy(10,20);

Non-standard, non-portable, useless function call.
printf("Enter ur choice: ");
scanf("%i",&choice1);

The FAQ will tell you why scanf() is best to avoid, and what to use
instead.
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();

Non-standard, non-portable, useless function call.
}
}
/*------------------------------
reservation---------------------------------*/
void reserv(void)
{
int choice2;
clrscr();
gotoxy(5,3);

Non-standard, non-portable, useless function calls.
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n");
printf("\t\t Reservation\n");
gotoxy(10,10);

Non-standard, non-portable, useless function call.
printf("1. Booking a seat\n");
gotoxy(10,12);

Non-standard, non-portable, useless function call.
printf("2. Edit details\n");
gotoxy(10,14);

Non-standard, non-portable, useless function call.
printf("3. View Details\n");
gotoxy(10,16);

Non-standard, non-portable, useless function call.
printf("4. Cancel Reservation\n");
gotoxy(10,18);

Non-standard, non-portable, useless function call.
printf("5. Search Details\n");
gotoxy(10,20);

Non-standard, non-portable, useless function call.
printf("6. Exit\n");
printf("\n\n\n");
gotoxy(10,22);

Non-standard, non-portable, useless function call.
printf("Enter ur choice: ");
scanf("%i",&choice2);

See above.
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(void)
{
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)

If the file can be opened, it is set to append, for writing at the
end, and possibly reading at the end. Before you can be sure of
reading anything useful, you may need to reposition the file position
indicator for this stream.
res_num=1;
else
{
do{
fscanf(sfile,"%i\t %s \t\t%s \t%i \t%s

All those tab escape sequences and spaces in the format string are
not necessary. If you need to skip white space, a single space will do
in the format string.
Also, be careful of posting long lines here. They have a tendancy to
wrap. You can break long strings up into shorter ones accross multiple
lines, and they will be concatenated by the preprocessor. For example:

"This is a string. "
"And this is part of the same string, "
"even though it's broken into multiple "
"string literals."

The above four string literals will be concatenated into a single
string.
",&res_num,&name,&pass,&seat,

Since name, pass and flight are arrays of char and decay into
pointers to their initial elements, the & operators applied these are
entirely superfluous. In fact, they're just plain wrong. Leave them
out.
Also, you'll no doubt want to limit the number of characters that
can be stored in each of those arrays. So you're going to need field
width specifiers in your format string.
&flight);

You're not doing anything with the data you've just read in. Why
bother reading it in, then?
}while(!feof(sfile));

Using feof() isn't the best way of controlling an input loop. The
FAQ says why.
Remember, fscanf() returns a value. Use that to control your loop.
For example:

while(5 == fscanf(sfile, "%i %39s %7s %i %5s",
&res_name, name, pass, &seat, flight))
;
res_num+=1;
}

Here ends your else clause. What follows is, then, always executed,
regardless of the if(...) statement above. Even things that rely on
the else clause being executed, such as closing the file (since the
else clause being executed relys on the file being successfully
opened).
clrscr();
gotoxy(5,3);

Non-standard, non-portable, useless function calls.
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
gotoxy(25,6);

Non-standard, non-portable, useless function call.
printf("Booking a seat");
printf("\n\n\n\n");
printf("Reservation no: %04i ",res_num);
printf("\nName : ");
fflush(stdin);

BANG! Undefined behaviour! fflush() is defined to flush output to an
output stream. Passing an input stream, such as stdin, to fflush()
causes undefined behaviour.
gets(name);

NEVER, never, never, never, never use gets(). It can never be used
safely. Read the FAQ, and use fgets() instead.
printf("passport no : ");
fflush(stdin);
gets(pass);

See above.
printf("number of seats: ");
scanf("%i",&seat);

See above.
printf("flight : ");
fflush(stdin);
gets(flight);

See above.
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);

See above.
do{
printf("\nPress [2] to go to reservation: ");
scanf("%i",&up);

See above.
if(up!=2)
printf("Invalid Entry");
}while(up!=2);
reserv();

You're calling reserv() here? Are you sure that's what you want to
do? I see no reason reserv() should be recursive. Perhaps you just
want to return here, which would put you back in reserv() at the point
after add() was called.
}
/*-----------------------view
reservations------------------------------------*/
void view(void)
{
char name[40];
int res_num,up;
int seat;
char flight[6];
char parse[8];
FILE*sfile;
clrscr();

Non-standard, non-portable, useless function call.
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))

See above.
{
fscanf(sfile,"%04i \t%s \t\t%s \t%i \t
%s",&res_num,&name,&parse,&seat,&flight);

See above.
if(feof(sfile))
break;

If you'd use fscanf()'s return value to control the loop, you
wouldn't have to bother with this needless feof() test here. You
wouldn't need to bother with feof() at all. See the FAQ.
printf("%04i \t%s \t\t%s \t%i \t%s
\n",res_num,name,parse,seat,flight);

Poor indentation, confusing, making it harder to read and understand
the code. Please indent consistently. Don't mix tabs and spaces,
especially in Usenet. In fact, spaces are much prefered.
Also, see above about line wrapping and string concatenation.
}
}
fclose(sfile);

Since this fclose() call is outside the else clause of the if(...)
statement, it is executed whether the file could be opened or not.
BANG! Undefined behaviour again, if the file failed to open!
do{
printf("\n\n\nPress [2] to go to reservation: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);

What's all this rigmarole in aide of? Why annoy the user by making
him/her press "2" and "Enter" (without telling him/her about the
latter)? Just return. If you must wait, then just wait for "Enter".
For example:

printf("Press \"Enter\" to continue...\n");
skip2nl(stdin);

where skip2nl() could be defined as:

void skip2nl(FILE *fp)
{
int c;

while(EOF != (c = fgetc(fp)) && '\n' != c)
;

}

This would skip all input up to EOF or a newline in a given input
stream. This could be a very useful function, not just for waiting for
"Enter" to be pressed. But all your fflush(stdin)s can be replaced
with skip2nl(stdin). In fact, you could even make it better by
returning a value, so it could then indicate whether EOF has been
read. All you'd need to do is have it return c. Then a calling
function could test the return value for EOF, and take some
appropriate action. (But I digress.)
reserv();

Again you're recursing into reserv(). What for? No doubt you really
just need to return.
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Thank u so much. I got kind of idea what r the areas i should study
first. before i haven't any idea about "while(!feof(sfile))" these
kind of things because in the class also we did only typing the
programs in our txt book. So i haven't any idea about some codings.
But now can study and what said and then do it.

Mar 4 '07 #8

Amali wrote:
On Mar 3, 7:36 am, phayw...@alphalink.com.au.NO.SPAM (Peter "Shaggy"
Haywood) wrote:
<~450 lines of quoted text snipped>
Thank u so much. I got kind of idea what r the areas i should study
first. before i haven't any idea about "while(!feof(sfile))" these
kind of things because in the class also we did only typing the
programs in our txt book. So i haven't any idea about some codings.
But now can study and what said and then do it.
If you just want to say thank you, you don't need to quote the whole
of the previous article. Some people still have slow modem connections
and they might get annoyed at having to download a 500 kb message just
to find your thanks at the end. If something isn't relavent to your
reply, you can snip it, especially signature blocks that begin after a
'-- '.

Mar 5 '07 #9
santosh wrote:
Amali wrote:

<~450 lines of quoted text snipped>
>Thank u so much. I got kind of idea what r the areas i should study
first. before i haven't any idea about "while(!feof(sfile))" these
kind of things because in the class also we did only typing the
programs in our txt book. So i haven't any idea about some codings.
But now can study and what said and then do it.

If you just want to say thank you, you don't need to quote the whole
of the previous article. Some people still have slow modem connections
and they might get annoyed at having to download a 500 kb message just
to find your thanks at the end. If something isn't relavent to your
reply, you can snip it, especially signature blocks that begin after a
'-- '.
In addition Amali should refrain from using childish abbreviations,
such as 'u' and 'r', which only serve to make his posts
unintelligible.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 5 '07 #10
santosh <sa*********@gmail.comwrote:
>Some people still have slow modem connections and they might get
annoyed at having to download a 500 kb message just to find your thanks
at the end.
And some people with fast connections might still get annoyed at needing
to page down forever. :)

Plus, it's Just Bad Form and violates all the etiquette rules on the
matter, and negates the gains made by quoting in context.

-Beej

Mar 5 '07 #11
On Feb 27, 3:44 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Amali said:
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>

The main function returns int, not void. The scanf function returns a
useful value, which you should check. The behaviour of fflush is
defined only for streams open for output or update, not for input. The
gets function is dangerous and should never, ever be used. Use fgets
instead (and read your documentation to understand how it works). Your
fscanf loop keeps overwriting previously-read values until it hits the
end of the file, which almost certainly is not what you want - look up
arrays. The feof function is almost never the right way to control a
file-reading loop. Check the return value of the file-reading function
instead.

Fix those, and let us know whether that fixed your problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>

/*-----The Prototypes-----*/

void menu(void);
void reserv(void);
void add(void);
void edit(void);
void view(void);
void search(void);
void del(void);
void exit(void);
void help (void);
void fly(void);
void flyadd(void);
void viewfly(void);

/*--------------------------------Welcome---------------------------*/

void main(void)
{
int ans1;

clrscr();
gotoxy(30,8);
printf("WELCOME!!!\n\n");
gotoxy(15,10);
printf("ASIAN RAFFAYA AIRLINE RESERVATION COMPANY");
gotoxy(15,11);
printf("-----------------------------------------");
printf("\n\n\n");

do{
printf("\n\t\tpress [1] to go to Main menu: ");
scanf("%i",&ans1);
if(ans1!=1)
printf("\n\t\tInvalid Entry!\n\n");
}while(ans1!=1);

menu();
}

/*-----------------------Menu---------------------------------*/

void menu(void)

{
int 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("\tEnter your choice[1-4]: ");
scanf("%i",&choice1);
printf("\n");
switch(choice1)
{
case 1:
reserv(); /*-----Reservation--*/ break;

case 2:
fly(); /*------Flight menu---*/
break;

case 3:
help(); /*---------help------*/
break;

case 4:
exit(); /*--------exit----------*/
break;

default:
printf("\t\tInvalid Entry!\n");
printf("\t\tEnter your choice[1-4]: ");
scanf("%i",&choice1);
getch();
}
}
/*------------------------------reservation---------------------*/
void reserv(void)
{
int 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 your choice[1-6]: ");
scanf("%i",&choice2);
printf("\n");
printf("\n");

switch(choice2)
{
case 1:
add(); /*--add reservation--*/

break;
case 2:
edit(); /*-----------edit reservation-------------*/
break;

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

case 4:
del(); /*------------delete reservation---------*/
break;

case 5:
search(); /*------------search reservation---------*/
break;

case 6:
menu(); /*-------------Back to menu---------------
*/
break;

default:
printf("\t\tInvalid Entry!\n\n");
printf("\tEnter your choice[1-6]: ");
scanf("%i",&choice2);

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

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

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

FILE*sfile;

if((sfile=fopen("c:\\air.txt","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();

}
/*-------------------------------Edit
Details---------------------------------*/
void edit(void)
{

char name[40],Target[40];
int res_num,Found=0;
int seat,up;
char flight[6];
char pass[8];
FILE *temp=fopen("c:\\temp.txt","w");
FILE *sfile=fopen("c:\\air.txt","r");

clrscr();

printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n\n\t\t Edit Details");
printf("\n\t\t -----------------");
printf("\n\n\tEnter name to edit: ");
fflush(stdin);
gets(Target);

while(!feof(sfile))
{
fscanf(sfile,"\n%04i \t%s \t\t%s \t%i \t
%s",&res_num,&name,&pass,&seat,&flight);
if(strcmp(Target,name)==0)
{
Found=1;
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);

fprintf(temp,"\n%04i \t%s \t\t%s \t%i \t%s
\n",res_num,name,pass,seat,flight);
}
else
{
fprintf(temp,"\n%04i \t%s \t\t%s \t%i \t%s
\n",res_num,name,pass,seat,flight);
}
}
if(Found==1)
printf("\n\tRedord has been changed\n");
else
printf("\nRecord not found\n");
fclose(sfile);
fclose(temp);
remove("c:\\air.txt");
rename("c:\\temp.txt","c:\\air.txt");
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 pass[8];
FILE*sfile;
clrscr();

if((sfile=fopen("c:\\air.txt","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,&pass,&seat,&flight);
if(feof(sfile))
break;

printf("%04i \t%s \t\t%s \t%i \t%s
\n",res_num,name,pass,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();

}

/*------------------------Search
reservation---------------------------------*/

void search(void)
{
char name[40],Target[40];
int res_num,Found=0;
int seat,up;
char flight[6];
char pass[8];
FILE*sfile;

if((sfile=fopen("c:\\air.txt","r"))==NULL)
printf("File Empty");

else
{
clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n\n\tEnter Name to search: ");
fflush(stdin);
gets(Target);
while(!feof(sfile)&& Found==0)
{
fscanf(sfile,"%i\t %s \t\t%s \t%i \t%s
",&res_num,&name,&pass,&seat,
&flight);
if(strcmp(Target,name)==0)
Found=1;
}
if(Found)
{
printf("\n\nNo : %04i\n",res_num);
printf("Name : %s\n",name);
printf("NIRC no : %s\n",pass);
printf("Number of seats: %i\n",seat);
printf("Flight : %s\n",flight);

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

/*--------------------------Delete
reservation--------------------------------*/

void del(void)
{
char name[40],Target[40];
int res_num,Found=0;
int seat,up;
char flight[6];
char pass[8];
FILE*sfile, *temp;

temp=fopen("c:\\temp.txt","w");
if((sfile=fopen("c:\\air.txt","r"))==NULL)
printf("File Empty!");

else
{
clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n\n\tEnter name to delete: ");
fflush(stdin);
gets(Target);
while(!feof(sfile))
{
fscanf(sfile,"%04i \t%s \t\t%s \t%i \t
%s",&res_num,&name,&pass,&seat,&flight);
if(feof(sfile))
break;
if(strcmp(Target,name)!=0)
fprintf(temp,"%04i \t%s \t\t%s \t%i \t%s
\n",res_num,name,pass,seat,flight);
else
{
Found=1;
printf("\n\nNo : %04i\n",res_num);
printf("Name : %s\n",name);
printf("NIRC no : %s\n",pass);
printf("Number of seats: %i\n",seat);
printf("Flight : %s\n",flight);
}
}
if(!Found)
printf("Record not found");
}
printf("\nRecord deleted.");
fclose(sfile);
fclose(temp);
remove("c:\\air.txt");
rename("c:\\temp.txt","c:\\air.txt");

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

}
/*-------------------------Flight
List----------------------------------------*/

void fly(void)
{
int choice3;

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

gotoxy(10,10);
printf("1. Add flight\n");
gotoxy(10,12);
printf("2. Flight List\n");
gotoxy(10,14);
printf("3. Exit\n");
printf("\n\n\n");
gotoxy(10,18);
printf("\tEnter your choice[1-3]: ");
scanf("%i",&choice3);
printf("\n");
switch(choice3)
{
case 1:
flyadd(); /*--------Add flight---------*/

break;

case 2:
viewfly(); /*---------Flight list--------*/
break;

case 3:
menu(); /*-----go back to main menu-----*/
break;

default:
printf("\t\tInvalid Entry!\n");
printf("\t\tEnter your choice[1-3]: ");
scanf("%i",&choice3);
getch();
}
}

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

void flyadd(void)
{
char flight_no[40];
int num=0;
int num_seat,up;

char des[40];
float price;

FILE*supfile;

if((supfile=fopen("c:\\flight.txt","a+"))==NULL)
num=1;

else
{
do{
fscanf(supfile,"%i %s %i %f %s ",&num,&flight_no,&num_seat,&price,
&des);

}while(!feof(supfile));
num+=1;

}
clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
gotoxy(25,6);
printf("Add Flight");
printf("\n\n\n\n");

printf("No: %i ",num);
printf("\nFlight No : ");
fflush(stdin);
gets(flight_no);
printf("seats available : ");
scanf("%i",&num_seat);
printf("Price : ");
scanf("%f",&price);

printf("Destination : ");
fflush(stdin);
gets(des);
printf("\n\n Record Saved!");

fprintf(supfile,"%i %s %i %.2f %s
\n",num,flight_no,num_seat,price,des);
fclose(supfile);
do{
printf("\nPress [2] to go to flight menue: ");
scanf("%i",&up);
if(up!=2)
printf("Invalid Entry");
}while(up!=2);
fly();

}

/*----------------------------View Flight
Details-----------------------------*/

void viewfly(void)
{
char flight_no[40];
int num=0;
int num_seat,up;
char des[40];
float price;

FILE*supfile;
clrscr();

if((supfile=fopen("c:\\flight.txt","a+"))==NULL)
printf("File Empty!");

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

while(!feof(supfile))
{
fscanf(supfile,"%i \t %s \t %i \t%f %s
",&num,&flight_no,&num_seat,&price,
&des);
if(feof(supfile))
break;

printf("%i \t %s \t %i \t%.2f %s
\n",num,flight_no,num_seat,price,des);
}
}

fclose(supfile);

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

}

/*--------------------------------Help-------------------------------
*/

void help(void)

{
int up;

clrscr();
gotoxy(5,3);
printf("\t\t ASIAN RAFFAYA AIRLINE RESERVATION\n");
printf("\t\t ----------------------------------\n");
printf("\n");
printf("\t\t Help\n");
printf("\t\t --------\n\n");
printf("\n\nThis module provides some fundamental helps for the
user.. ");
printf("\nMain menu shows all the obtainable functions like
RESERVATION, FLIGHT LIST.\n");
printf("Reservation screen helps to add,edit,view,search and delete
reservations.\n");
printf("Flight Menu allow to add and display details about flights
available.\n");

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

menu();
}
/*--------------------------------
Exit---------------------------------------*/
void exit(void)
{
clrscr();
gotoxy(23,10);
printf(" THANK YOU");
gotoxy(23,13);
printf("**** Enjoy your trip ****");

}

Mar 12 '07 #12
Amali wrote:
On Feb 27, 3:44 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Amali said:
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>

The main function returns int, not void. The scanf function returns a
useful value, which you should check. The behaviour of fflush is
defined only for streams open for output or update, not for input. The
gets function is dangerous and should never, ever be used. Use fgets
instead (and read your documentation to understand how it works). Your
fscanf loop keeps overwriting previously-read values until it hits the
end of the file, which almost certainly is not what you want - look up
arrays. The feof function is almost never the right way to control a
file-reading loop. Check the return value of the file-reading function
instead.

Fix those, and let us know whether that fixed your problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.
<snip mess>

You really are incapable of learning, aren't you? You don't seem to
have corrected any of the discrepancies pointed out to you earlier.
Maybe you should consider giving a rest to programming?

Mar 12 '07 #13
"Amali" <am***************@gmail.comwrote:
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.
Does your teacher know what he's doing? If so:
void main(void)
Automatic fail.

Richard
Mar 12 '07 #14
On Mar 12, 2:21 pm, "santosh" <santosh....@gmail.comwrote:
Amali wrote:
On Feb 27, 3:44 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Amali said:
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>
The main function returns int, not void. The scanf function returns a
useful value, which you should check. The behaviour of fflush is
defined only for streams open for output or update, not for input. The
gets function is dangerous and should never, ever be used. Use fgets
instead (and read your documentation to understand how it works). Your
fscanf loop keeps overwriting previously-read values until it hits the
end of the file, which almost certainly is not what you want - look up
arrays. The feof function is almost never the right way to control a
file-reading loop. Check the return value of the file-reading function
instead.
Fix those, and let us know whether that fixed your problem.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.

<snip mess>

You really are incapable of learning, aren't you? You don't seem to
have corrected any of the discrepancies pointed out to you earlier.
Maybe you should consider giving a rest to programming?- Hide quoted text -

- Show quoted text -
I got what they pointed out.but in this project they are mostly
considering about only the documentation. And it is no enought to this
project what we learnt in the class. Now i need to learn more about c
programming.

Mar 12 '07 #15
On Mar 12, 4:00 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"Amali" <amalikarunanay...@gmail.comwrote:
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.

Does your teacher know what he's doing? If so:
void main(void)

Automatic fail.

Richard
Why it must int main. accoding to my knowladege it is not returning
any thing. Its only calling to another function.

Mar 12 '07 #16
Amali said:
Now i need to learn more about c programming.
I posted this stuff before, but you appear to have missed it, so here we
go again.

The main function returns int, not void. The scanf function returns a
useful value, which you should check. The behaviour of fflush is
defined only for streams open for output or update, not for input. The
gets function is dangerous and should never, ever be used. Use fgets
instead (and read your documentation to understand how it works). Your
fscanf loop keeps overwriting previously-read values until it hits the
end of the file, which almost certainly is not what you want - look up
arrays. The feof function is almost never the right way to control a
file-reading loop. Check the return value of the file-reading function
instead.

Fix those, and let us know whether that fixed your problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 12 '07 #17
Amali said:
Why it must int main.
Because that's the way the language is defined.
accoding to my knowladege it is not returning any thing.
It should be returning an int. Not returning anything from main is a bug
in your program - one of many.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 12 '07 #18
Amali wrote:
On Mar 12, 4:00 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"Amali" <amalikarunanay...@gmail.comwrote:
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.
Does your teacher know what he's doing? If so:
void main(void)
Automatic fail.

Why it must int main.
Because, that's how the Standard for C says it should be declared.
accoding to my knowladege it is not returning
any thing. Its only calling to another function.
It *should* return an int value. That's one of the numerous problems
in your program. Unless you're prepared to learn when there's an
opportunity, you'll find programming very frustrating and, ultimately,
a failure.

Mar 12 '07 #19
"Amali" <am***************@gmail.comwrote:
On Mar 12, 4:00 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"Amali" <amalikarunanay...@gmail.comwrote:
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.
Does your teacher know what he's doing? If so:
void main(void)
Automatic fail.

Why it must int main. accoding to my knowladege it is not returning
any thing. Its only calling to another function.
RTFFAQ.

<http://c-faq.com/ansi/maindecl.html>
<http://c-faq.com/ansi/voidmain.html>
<http://c-faq.com/ansi/voidmain3.html>
<http://c-faq.com/ansi/voidmainexamp.html>
<http://c-faq.com/ansi/voidmainbooks.html>

HTH; HAND.

Richard
Mar 12 '07 #20
Amali wrote:
On Mar 12, 2:21 pm, "santosh" <santosh....@gmail.comwrote:
Amali wrote:
<snip>
I submited my project and its working. I haven't done many things in
the class so i have to do my project accoding to the text book. but
these days i'm studing more about c programming. Please tell me some
links to get tutorial in c programming. Anyway i'll send you my
codings. Thank you.
<snip mess>

You really are incapable of learning, aren't you? You don't seem to
have corrected any of the discrepancies pointed out to you earlier.
Maybe you should consider giving a rest to programming?

I got what they pointed out.but in this project they are mostly
considering about only the documentation. And it is no enought to this
project what we learnt in the class. Now i need to learn more about c
programming.
What you want to do can be done with just the basics of C and a few
Standard library functions. The problem is that you're unwilling to
correct the errors and other ill-advised constructs in your code that
posters in this thread have repeatedly pointed out to you.

Your program, as it stands, is simply incorrect. If it appears to
work, be assured that it's only so by pure chance. At a minimum
correct the following:

1. Change the line 'void main(void)' to 'int main(void)' and place a
'return 0;' statement at the very end of the main function.

2. Remove all instances of 'fflush(stdin)'. It's undefined.

3. Replace all uses of gets(s) with fgets(s, SIZE_OF_S, stdin).

4. Check for end-of-file with the return value of the function you use
to read. It'll return EOF if end-of-file occurs.

5. Correct your use of fscanf. It keeps overwriting previous values.
Use an array instead of a single variable for holding the input.

If you don't know how to use any of the features I've listed above, or
how to correct your program, please ask, but simply posting the same
incorrect code again and again is not helping anyone.

Mar 12 '07 #21
On Mar 12, 11:15 am, "Amali" <amalikarunanay...@gmail.comwrote:
On Mar 12, 4:00 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
[snip]
Does your teacher know what he's doing? If so:
void main(void)
Automatic fail.
[snip]
Why it must int main. accoding to my knowladege it is not returning
any thing. Its only calling to another function.
main() returns an int value because that's how it is defined.

Very few C programs run as the sole activity of a computer. Those that
do are called "standalone" programs, and there are special rules
around the language features that are open to them.

The rest of the C program universe (a /much/ larger set of programs)
all run within the confines of some system (or systems) that arranges
for their execution and cleans up after they complete. Often, these
systems arrange subsequent processing based on whether or not the C
program accomplished it's task. Of course, this requires that the C
program be able to tell this larger environment whether it was
successful or not. That's what the int returned by main() is all
about.

The return value from main() provides this indicator of success or
failure. It can take one of three values (within the confines of the C
standard): EXIT_SUCCESS (which indicates that the C program succeeded
in it's task), EXIT_FAILURE (which indicates that the C program failed
in it's task), and 0 (which is interpreted as success, like
EXIT_SUCCESS). You will find EXIT_SUCCESS and EXIT_FAILURE defined in
the stdlib.h header.

Even in your (evidently) MSDOS environment, the environment can (and
frequently does) make decisions and take actions based on the success
or failure of the programs it initiates. Failure to return such an
indicator not only violates the C language standard, but endangers
your operating environment. If you do not return a value, there's no
telling what the environment will think happened, and it may make the
wrong decisions regarding futher processing.

So, besides the fact that you are required by the language to return a
value from main(), it is also a good idea to return a value from
main() no matter what your environment. If the value is unnecessary,
your environment will ignore it, but if the value /is/ necessary and
your program /does not/ return it, your environment may malfunction
and take the wrong actions.

HTH
--
Lew
Mar 12 '07 #22
Thank you. I got what you said and I'll try again.

Mar 12 '07 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2583
by: kaiwing18 | last post by:
Hi , I have a problem relate to java and database. Could anyone answer me ?Please see the following code. import java.sql.*; public class Result { public static void main(String args) {
0
2523
by: Ali | last post by:
I'm trying to write a really basic chat program that allows 2 client programs to send messages to each other via a server. I've managed to write the code so that both clients can connect to the...
3
3357
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression...
5
3864
by: Reza | last post by:
How can I change the following code to the equivalent in C# Public Class EventSink : Implements IListEventSink Public Sub OnEvent(ByVal listEvent As Microsoft.SharePoint.SPListEvent)...
1
3421
by: Chua Wen Ching | last post by:
Hi there, I have some problems when reading XML file. 1. First this, is what i did, cause i can't seem to read "sub elements or tags" values, so i place those values into attributes like this....
4
2425
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
11
3714
by: Nurit N | last post by:
This is the third newsgroup that I'm posting my problem. I'm sorry for the multiple posts but the matter becoming urgent. I hope this is the right place for it... I have created a very...
2
4141
by: Suhas Vengilat | last post by:
Hi All, I have a couple of stored procedures written in SQL 2000. Given below one sp. ***************************************************** ALTER PROCEDURE . ( @AcTypeName varchar(50),...
1
2886
by: CodeSeeker | last post by:
I have an application, which uses pop3 to read the messages from the mailbox, and it has been working fine for so many year. We recently have started changing this application to use java mail IMAP 4...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.