Binary File Errors


For a big project for school I have to make an airline reservation simulator but I have run into a problem. I want to save the the flight code and its location in a binary file so that I can obtain a code according to the location but this happens:
imgur.com/a/xhrCB (link to current output and expected output)

Here is the source class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 class file
{
    private:

    char code[8];
    char from[20];
    public:
        void input();
        void show();
        char *getn()
        {
            return code;
        }
};file fileobj;

void file::input()
{
    clrscr();
    cout<<"Enter Code+ no: ";
    gets(code);
    int A=strlen(code);
    if(A==6)
        strcat(code,"  ");
    else
        strcat(code," ");
    cout<<"Enter Location: ";
    gets(from);
    int a=20-strlen(from);
    for(int i=0;i<a;i++)
          strcat(from," ");
}
void file::show()
{
    cout<<"Code: ";puts(code);
    cout<<"\t\tLocation: ";puts(from);
}
c-strings are null terminated, you need to reserve enough space to include the '\0' character.
(by the way, `gets()' is dangerous for this reason, don't use it)

1
2
3
4
char code[8];
    int A=strlen(code);
    if(A==6)
        strcat(code,"  ");
you are too short.

However, I don't see the need to fill with spaces ¿what are you doing that for?

> imgur.com/a/xhrCB (link to current output and expected output)
use text to show text next time.
However, I don't see the need to fill with spaces ¿what are you doing that for?


to make the following table fit properly easily

1
2
3
4
cout<<"--------------------------------------------------------------\n";
 cout<<"| TIME | FLIGHT  |        FROM          | COUNTER | BOARDING |\n";
 cout<<"|      |  CODE   |                      |         |   GATE   |\n";
 cout<<"--------------------------------------------------------------\n";


Increased the size by 1 and the program is back to normal

thankyou
Last edited on
to make the following table fit properly easily


Please note that in general, it's a good idea to keep the representation of data (the way you store it) independent from the presentation of the data (the way you display it). Otherwise it's too easy to get boxed in if the requirements change.

For example, what if someone decides that they also want your data displayed in a table with different field widths, so you need a way to put it into both tables. Now the padding that you've added to the representation for the first table gets in the way of displaying it in the second table.

For a school project it probably won't make a difference, but in the real world it does.

will keep in mind for later as this is a school project and it wont make much of a difference :P
Topic archived. No new replies allowed.