Thread: Database Revamped: 'Expected ';' before ')' token error'

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    28

    Database Revamped: 'Expected ';' before ')' token' error

    Hi, some of you might remember me from a database project I was working on ages ago, or, you might not .

    Anyways, I've recently picked up my C++ projects again, and set about redoing the code for the Database program.
    All was going well until I got weird errors that I didn't seem to be able to solve, and I was hoping someone on this board could help me.

    Alright, here's the code:
    Code:
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <clrscr.h> // Yesyes, a custom header thingy, but no credit 2 me, it's taken from the faq.
    
    using namespace std;
    /* 
       30-12-05 20:38
       Thanks people of the cboard forum!!!
       Special thanks:
               xhi
               xhi
               xhi
               xhi
               *Repeat above 200000 times*
               Me
    */
    struct Database {
           int id;
           string name;
           int age;
    };
    
    int main() {
        Database emp;
        int choice;
        string name = "database/Member";
        string dat = ".dat";
        string sid;
        ...
    
        ...
                   } //Bad code starts below I think
        if (choice == 1){
                   int id = 0;
                   ifstream fin;
                   stringstream s;
                   fin.open("database/Member1.dat");
                   for(!fin.bad()) {
                                    string filename = " ";
                                    id = id + 1;
                                    s << id;
                                    s >> sid;
                                    filename = name + sid + txt;
                                    fin.open(filename.c_str(), ios::binary);
                                    fin.read((char *)(&emp), sizeof(emp));
                                    cout << "Id: " << emp.id << endl << "Name: " << emp.name << endl << "Age: " << emp.age << "\n\n";
                                    cin.get();
                                    fin.close();
                                    ifstream fin;
                                    }
                   }
    }
    And these are the errors I get:
    • 67 C:\Documents and Settings\Tim\Mijn documenten\My C++\FinFoutDbase.cpp expected `;' before ')' token
    • 80 C:\Documents and Settings\Tim\Mijn documenten\My C++\FinFoutDbase.cpp expected primary-expression before '}' token
    • 80 C:\Documents and Settings\Tim\Mijn documenten\My C++\FinFoutDbase.cpp expected `;' before '}' token
    • 80 C:\Documents and Settings\Tim\Mijn documenten\My C++\FinFoutDbase.cpp expected primary-expression before '}' token


    I get more then those, but basically they are all the same.

    The compiler/editor I use is:

    Bloodshed Dev C++ version 4.9.9.2

    I have absolutely no idea as to where these erorrs are coming from, or how to solve them. Any ideas are welcome.
    Last edited by krageon; 07-07-2006 at 06:52 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Code:
    for(!fin.bad())
    I'm sure that's not what you want to do

    Probably, if(!fin.bad())
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Nope, I want it to go through all the possible files in the database folder, so if there are 5, 5 are shown on screen. Your method would only show the first, am I correct?

    Thanks.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The problem is that for statement is an error. The for statement header takes the form of:

    for (init-statement; condition; expression)

    Please refer to C++ Reference or any other reference describing the for loop.

    Meanwhile, if what you describe is what you want to do, then you shold consider a while loop.
    Last edited by Mario F.; 07-07-2006 at 06:52 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    *Slaps self on forehead*

    How could I be so short-sighted. Thanks for the help, It is compiling now.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    28

    Database Revamp: File reading bug.


    [Edit]
    Found the answer, modified code to reflect.
    [/Edit]


    Hello, I have another problem with my Database project, and I was wondering if someone could give me some pointers.

    This is the code:

    Code:
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <clrscr.h> // Yesyes, a custom header thingy, but no credit 2 me, it's taken from the faq.
    
    using namespace std;
    /* 
       30-12-05 20:38
       Thanks people of the cboard forum!!!
       Special thanks:
               xhi
               xhi
               xhi
               xhi
               *Repeat above 200000 times*
               Me
    */
    struct Database {
           int id;
           char name[31];
           int age;
    };
    
    int main() {
        Database emp;
        int choice;
        string name = "database/Member";
        string dat = ".dat";
        string sid;
        cout << "Read(1) or write(2)?";
        cin >> choice;
        cin.ignore();
        ...
    
        ...
        if (choice == 1){
                   int id = 1;
                   int x = 0;
                   ifstream fin;
                   for(;;id++) {
                                    stringstream s;
                                    s << id;
                                    s >> sid;
                                    string filename = " ";
                                    filename = name + sid + dat;
                                    fin.open(filename.c_str(), ios::binary);
                                    if (fin.fail()) {
                                                    break;
                                                    }
                                    fin.read((char *)(&emp), sizeof(emp));
                                    cout << "Id: " << emp.id << endl << "Name: " << emp.name << endl << "Age: " << emp.age << "\n\n";
                                    cin.get();
                                    fin.close();
                                    ifstream fin;
                                    }
                   }
    }
    The read function is supposed to read all the files in the dir, and then stop reading, but it doesn't. It reads all the files in the dir, but when it gets to the last file, it just shows it over and over again. Any ideas as to ho to solve this?

    TIA
    Last edited by krageon; 07-07-2006 at 10:26 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Threads merged - you didn't need a new thread just to show you added a ; or two
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Sorry, I'll keep that in mind in the future

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The read function is supposed to read all the files in the dir, and then stop reading, but it doesn't. It reads all the files in the dir, but when it gets to the last file, it just shows it over and over again. Any ideas as to ho to solve this?
    Are you quite sure the problem is not in the ... ... part (may-be a bad eternal loop).

    Apart from that there are a few strange things:
    - where is int x = 0 used?
    - what's the point of string filename = " "; filename = sth else; ?
    - what is the use of the second ifstream fin declaration? If I'm not mistaken it goes out of scope right away and it's not used at all.

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I think the ... ... is used to signify that some code is missing which isn't relavent to the problem .... perhaps, I dunno.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (fin.fail())
    This is set only after I/O has failed (according to http://www.cplusplus.com/ref/iostream/ifstream/)
    Perhaps using is_open() instead would be more meaningful.

    > for(;;id++)
    Also, if you already know how many files you have, then why not count that many rather than just running into a wall and hoping you'll stop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM