Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

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();

On Feb 27, 2:53 pm, "Amali" <amalikarunanay@gmail.com> wrote:

I think my problem is in add and view functions.
Please help me.
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.

On Feb 27, 1:59 pm, "Amali" <amalikarunanay@gmail.com> wrote:

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/stdio.h/fopen.php?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

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>

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc