Can any experts let me know where i go wrong in here ?

Pages: 12
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include <vector>                          // vector Library
#include <string>
#include<fstream>                         // file stream Library
#include<iomanip>
#include<cstdlib>
#include<cstring>



using namespace std;

#ifndef BOOKING
#define BOOKING

class include_function {                           // new class called include funtrion

public:

    long datetoday (string date)
    {
        string day, month, year;
        long totalday;
        if ((date[2]=='/' && date[5]=='/') && (date.length() == 10))            // date to day calculation
        {
           day = day + date[0]+ date[1];
           month = month + date[3] + date[4];
           year = year + date[6] + date[7] + date[8] + date[9];
           totalday = atoi(day.c_str()) + ((30 + ((atoi(month.c_str()) % 9) %2)) * (atoi(month.c_str()))) + (atoi(year.c_str()) * 365);
           return totalday;
        }
        else
        cout << "Invalid Date..";

        return 0;

    }

    string read_input(string filename,int line, char delimeter)
    {
        string str1, str2;
        char next;
        int a=0;
        ifstream fin;
        fin.open(filename.c_str());
        if (fin.fail()) {cout << "Data file disappear!" << endl; exit(1);}
        do{
            fin.get(next);
            if (next==delimeter){
               str2=str1;
               a++;
               str1="";}
            else
               str1 = str1+next;
        }while ((!(fin.eof())) && (!(a>line)));
        return str2;
    }

string get_data(string data_row,char delimeter,int number_occur)
{
       string ret,retemp;
       char ctmp;
       int a=0,k=0;
       data_row=data_row+delimeter;
       do
       {
            if (data_row[a]==delimeter)
            {
                ret=retemp;
                retemp="";
                k++;
            }
            else
            {
                ctmp=data_row[a];
                retemp=retemp+ctmp;
            }
            a++;
       } while((a<strlen(data_row.c_str()))&&!(k==number_occur));
       return ret;
}


};


//////////////

While i compile it.. i only get the message data file disappear.
whats wrong with it ?
Last edited on
am i using d fstream correctly ???
is my if / else / while statement is being use correctly ???
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include <vector>                          // vector Library
#include <string>
#include<fstream>                         // file stream Library
#include<iomanip>
#include<cstdlib>
#include<cstring>



using namespace std;

#ifndef BOOKING
#define BOOKING

class include_function {                           // new class called include funtrion

public:

    long datetoday (string date)
    {
        string day, month, year;
        long totalday;
        if ((date[2]=='/' && date[5]=='/') && (date.length() == 10))            // date to day calculation
        {
           day = day + date[0]+ date[1];
           month = month + date[3] + date[4];
           year = year + date[6] + date[7] + date[8] + date[9];
           totalday = atoi(day.c_str()) + ((30 + ((atoi(month.c_str()) % 9) %2)) * (atoi(month.c_str()))) + (atoi(year.c_str()) * 365);
           return totalday;
        }
        else
        cout << "Invalid Date..";

        return 0;

    }

    string read_input(string filename,int line, char delimeter)
    {
        string str1, str2;
        char next;
        int a=0;
        ifstream fin;
        fin.open(filename.c_str());
        if (fin.fail()) {cout << "Data file disappear!" << endl; exit(1);}
        else if
            (fin.get(next));
           else if (next==delimeter){
               str2=str1;
               a++;
               str1="";}
            else{
               str1 = str1+next;
            }

        while ((!(fin.eof())) && (!(a>line)));
        return str2;
    }
    }

 get_data(string data_row,char delimeter,int number_occur)
{
       string ret,retemp;
       char ctmp;
       int a=0,k=0;
       data_row=data_row+delimeter;

       {
            if (data_row[a]==delimeter)
            {
                ret=retemp;
                retemp="";
                k++;
            }
            else
            {
                ctmp=data_row[a];
                retemp=retemp+ctmp;
            }
            a++;
       } while((a<strlen(data_row.c_str()))&&!(k==number_occur));
       return ret;
}


};


ive edited the codes abit .... but im compiling more error....
nobody is taking a look at my codes ????
It's been only 34 minutes!

We don't have nearly enough information about the problem. What are your errors, how are you using your class, and what's the path of the file you're trying to access? Those will be good to start.

-Albatross
lol. i keep refreshing sorry for my impatience.

actually i have no idea where is d error coming from .

is it in my if/else statement?

it should store information for the data that has been collected to a file call Booking.dat

is my read input & get data correct ?

sorry for troubling and thanks for helping out
im using Code Blocks to compile.

this program is actually design for a room booking system that will store information gathered in a file.

But when i compile it. it says data file disappeared.
so im guessing my problem should be somewhere the code i just pasted.
can you guys double check it for me ?

Thanks again for the help. Cheers
So it's not a compiler error; it's something you're getting after you run your program.

I still don't know how you're using your class.

Finally... maybe it might be a good idea to have a !fin.is_open() check instead of fin.fail() check?

-Albatross
right off the bat you don't have #endif to close the #ifndef

string read_input() has an extra '}'

get_data() has no type ... I think it should be string or something


.. just for a few... do you look at errors? I used code blocks and they told me that
your data file disappeared? yeah, I have a lot of errors on this code, but didn't see that... maybe you aren't pasting enough.
well, I said a lot of errors, but when I fixed those, it compiled... so that much
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include <vector>                          // vector Library
#include <string>
#include<fstream>                         // file stream Library
#include<iomanip>
#include<cstdlib>
#include<cstring>



using namespace std;

#ifndef BOOKING
#define BOOKING

class include_function {                           // new class called include funtrion

public:

    long datetoday (string date)
    {
        string day, month, year;
        long totalday;
        if ((date[2]=='/' && date[5]=='/') && (date.length() == 10))            // date to day calculation
        {
           day = day + date[0]+ date[1];
           month = month + date[3] + date[4];
           year = year + date[6] + date[7] + date[8] + date[9];
           totalday = atoi(day.c_str()) + ((30 + ((atoi(month.c_str()) % 9) %2)) * (atoi(month.c_str()))) + (atoi(year.c_str()) * 365);
           return totalday;
        }
        else
        cout << "Invalid Date..";

        return 0;

    }

    string read_input(string filename,int line, char delimeter)
    {
        string str1, str2;
        char next;
        int a=0;
        fstream fin;
        fin.open(filename.c_str());
        if (!fin.is_open()) {cout << "Data file disappear!" << endl; exit(1);}
        do{

            if (next==delimeter){
               str2=str1;
               a++;
               str1="";}
            else
               str1 = str1+next;
        }while ((!(fin.eof())) && (!(a>line)));
        return str2;
    }

string get_data(string data_row,char delimeter,int number_occur)
{
       string ret,retemp;
       char ctmp;
       int a=0,k=0;
       data_row=data_row+delimeter;
       do
       {
            if (data_row[a]==delimeter)
            {
                ret=retemp;
                retemp="";
                k++;
            }
            else
            {
                ctmp=data_row[a];
                retemp=retemp+ctmp;
            }
            a++;
       } while((a<strlen(data_row.c_str()))&&!(k==number_occur));
       return ret;
}


};


@cranium= yeah ive done correcting all those mistakes... i can compile it. but it dont work.
@Albatross= i did what u say. but still d same, should i copy paste all d coding here ?
See, I read the first code piece you gave, which at first glance looked alright... O_o

Again, we need more information on the problem. How you're using the code in main(), where your file is actually located, there's a lot that helps.

-Albatross
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
   void save_allbooking()                      // Save All Booking Function
      {
           int c=0; //counter
           char d1=200, d2=201;
           ofstream fout ("booking.dat");
           cout << setw (58) << "=====================================" << endl;
           cout << setw (58) << "       SAVE ALL BOOKING RECORD       " << endl;
           cout << setw (58) << "=====================================" << endl;
           cout<< endl;
           while ((c < booking.size()))
           {
                  fout << booking[c].time << d1 << booking[c].date << d1 << booking[c].id << d1 << booking[c].timestart << d1
                       << booking[c].timeend << d1 << booking[c].roomid << d1 << booking[c].organizer << d1<< booking[c].num_participate << d2;
                  cout << endl;
                  c++;
           }
      };


     void display_allbooking()                                // Display All Booking Function
     {
           int c=0; //counter
           cout << setw (58) << "===========================================" << endl;
           cout << setw (58) << "        DISPLAY ALL EXISTEING BOOKING       " << endl;
           cout << setw (58) << "===========================================" << endl;
           cout<< endl;
           cout<< "No  Time      Date\tId\tStart\t  End\t RoomId\t  Organizer   people " << endl;
           while ((c < booking.size()))
           {
                 cout << c+1 << "  " << booking[c].time << "   " << booking[c].date << "  " << booking[c].id << "     " << booking[c].timestart << "    "
                       << booking[c].timeend << "   " << booking[c].roomid << "     " << booking[c].organizer << "\t" << booking[c].num_participate << endl;
                  cout <<endl;
                  c++;
           }
      };

      void make_booking(string type)                         // Make Booking Function
      {
           int booksize = booking.size();
           int num=0;
           booking.resize(booksize+1);
           allroom room;
           room.display_availroom(); //display available room
           cout << setw (58) << "===========================================" << endl;
           cout << setw (58) << "              MAKE NEW BOOKING             " << endl;
           cout << setw (58) << "===========================================" << endl;
           cout<< endl;
           cout << "Enter the number of the room that you want to book: ";
           cin >> num;
           num-=1;
           if (num < room.room.size()){
           booking[booksize].roomid = room.room[num].id;
           if (((type=="student") && (room.room[num].type=="exam_hall")) ||  ((type=="student") && (room.room[num].type=="design_studio")) || ((type=="student") && (room.room[num].type=="meeting_room")))
           {                                                                                           // From 499 - 502 line, indicate whether is student or staff
           cout << "You can book main hall, lecture room and tutorial room only."<< endl<<endl;
           return;
           }
           cout << " What time do you want to book?" << endl << "Time: ";
           cin >> booking[booksize].time;
           cout << " When do you want to book?" << endl << "Date: ";
           cin >> booking[booksize].date;
           cout << " What is your id number?" << endl << "ID: ";
           cin >> booking[booksize].id;
           cout << " What time do you want to start using?" << endl << "Timestart: ";
           cin >> booking[booksize].timestart;
           cout << " What time do you want to stop using?" << endl << "Timeend: ";
           cin >> booking[booksize].timeend;
           cout << " From what organizer are u from?" << endl << "Organizer: ";
           cin >> booking[booksize].organizer;
           cout << " How many participants are there?" << endl << " Number of participate: ";
           cin >> booking[booksize].num_participate;
           cout<<endl;
           save_allbooking();
           }
           else
           cout << "Invalid room" << endl;

      };


      void delete_booking()                                                // Delete Booking Function
      {
          string post;
          long cur_day=0;
          int position;
          display_allbooking();
          cout<<endl;
          cout << setw (58) << "=====================================" << endl;
          cout << setw (58) << "      DELETE EXISTING BOOKING        " << endl;
          cout << setw (58) << "=====================================" << endl;
          cout<< endl;
          cout<<"Enter a date to delete all previous booking from the date: \n Format (dd/mm/yyyy): ";
          cin>> post;
          cur_day = datetoday(post);
          position = atoi(post.c_str());
          for (int k=0; k<booking.size()-1; k++)
          {
              if ((cur_day > datetoday(booking[k].date)) && (datetoday(booking[k].date)>0))
              {
                  for (int i=k; i<booking.size()-1; i++)            //perform delete
                  {
                      booking[i]=booking[i+1];
                      booking.pop_back();
                  }
              }
          }
          save_allbooking();

      };

     
#endif 

/////part of the coding
Last edited on
I agree with Albatross, waiting for the rest of the code for further info... do you not know how to call the class or include it in the file where main is located?
it should save in a file call booking.dat
@cranium=using :: ??
Last edited on
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class bookingclass                                            // New Class called Booking
{
      public:
      string time;
      string date;
      string id;
      string timestart;
      string timeend;
      string roomid;
      string organizer;
      string num_participate;
};

//========================================================================================

class allbooking : public include_function
{
      public:
      vector <bookingclass> booking;
      allbooking()
      {
             ifstream fin ("booking.dat"); //loading.............(database)
             int c1=0;
             char d1=200, d2=201;
             string datarow = read_input("booking.dat",c1,d2);
             while (datarow!="")
             {
                   booking.resize(booking.size()+1);
                   booking[c1].time=get_data(datarow,d1,1);
                   booking[c1].date=get_data(datarow,d1,2);
                   booking[c1].id=get_data(datarow,d1,3);
                   booking[c1].timestart=get_data(datarow,d1,4);
                   booking[c1].timeend=get_data(datarow,d1,5);
                   booking[c1].roomid=get_data(datarow,d1,6);
                   booking[c1].organizer=get_data(datarow,d1,7);
                   booking[c1].num_participate=get_data(datarow,d1,8);
                   c1++;
                   datarow = read_input("booking.dat",c1,d2);

             }
      };



      void save_allbooking()                                       // Save All Booking Function
      {
           int c=0; //counter
           char d1=200, d2=201;
           ofstream fout ("booking.dat");
           cout << setw (58) << "=====================================" << endl;
           cout << setw (58) << "       SAVE ALL BOOKING RECORD       " << endl;
           cout << setw (58) << "=====================================" << endl;
           cout<< endl;
           while ((c < booking.size()))
           {
                  fout << booking[c].time << d1 << booking[c].date << d1 << booking[c].id << d1 << booking[c].timestart << d1
                       << booking[c].timeend << d1 << booking[c].roomid << d1 << booking[c].organizer << d1<< booking[c].num_participate << d2;
                  cout << endl;
                  c++;
           }
      };


///part of the coding

wow, this code is going all over the place... maybe upload it somewhere, for ease... what's the site... uh pastebin or something... hold up... Yeah, http://pastebin.com/ is what people seem to be using
Pages: 12