Thread: Everything LOOKS Okay, But Will Not Compile

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    United States
    Posts
    10

    Everything LOOKS Okay, But Will Not Compile

    I Am Fairly New To C++ (A Few Weeks Into It), And Decided To Make A Project... My Only Problem Is That The Compiler Gives Me This Error: 28 C:\...\databaseCreator.cpp expected `;' before "record"
    I Have Checked My Code & I See Nothing Wrong... Can Anyone Help Me?

    Addional Information:
    Using Dev-C++

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    struct Account;
    void box(string content);
    
    struct Account
    {
           char fName[81],
                mName[81],
                lName[81],
                password[81];
           double balance;
    };
    
    int main()
    {
        int number;
        box("Universal Bank: Account Database Constructor");
        cout << "This Program Creates A Database To Store Account Information." << endl
             << "\tInstructions: To Create A Database, Simply Input The Number Of Accounts" << endl
             << "\tYou Woult Like Stored In The Database. (ie - For 50 Accounts, Enter '50')" << endl
             << "WARNING: Creating A New Database Will Overwrite Previously Created Ones!" << endl
             << "No. Of Accounts: ";
        cin  >> number;
        fstream Account ("Accounts.dat", ios::out | ios::binary);
        Account record = {"", "", "", "", 0.0}; //This Is Line 28!!!
        
        for (int i = 0; i < number; i++)
        {
            cout << "Now Creating Account Database..." <<endl
                 << "Writing Record " << i << " of" << number << endl;
            Account.write(reinterpret_cast<char *>(&record), sizeof(record));
        }
        Account.close();
        cout << "Creation Of Accounts.dat Complete";
        return 0;
    }
    
    void box(string content)
    {    
        char cornerA = 0xC9;
        char cornerB = 0xBB;
        char cornerC = 0xC8;
        char cornerD = 0xBC;
        char perpendicular = 0xBA;
        char horizontal = 0xCD;
         
        cout << cornerA;
        for (int c = 0; c < content.length() + 2; c++)
            cout << horizontal;
        cout << cornerB << endl;
        cout << perpendicular << " " << content << " " << perpendicular << endl;
        cout << cornerC;
        for (int c = 0; c < content.length() + 2; c++)
            cout << horizontal;
        cout << cornerD << endl << endl;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    struct Account
    
    fstream Account
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
            cout << "Now Creating Account Database..." <<endl;
    Try compiling again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start by dropping the "Lets Write Like This With Caps Style"
    http://www.catb.org/~esr/faqs/smart-...html#writewell
    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.

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    United States
    Posts
    10
    I do not belive that is the problem.
    I have other files with both the "struct" and "fstream" having the same name.
    Thank You Though!

    Any Other Suggestions?

  6. #6
    Registered User
    Join Date
    May 2006
    Location
    United States
    Posts
    10
    Sorry about the caps, Salem.
    Guess I was just excited! :P
    Thank you...

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    United States
    Posts
    10
    citizen, thanks for your suggestion, but that didn't seem to work either.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, but Magos is correct, you have other more important problems. Don't name two separate things the same to avoid ambiguity.

    Also when I try to compile that, I ran a syntax check. Dev C++ reports that record is undefined (first use in this function). When you define structs, you need to use
    Code:
    struct Account

  9. #9
    Registered User
    Join Date
    May 2006
    Location
    United States
    Posts
    10
    Thank you for the suggestion to change the names... greatly appreciated! but... i also need to figure out what is wrong with my code, that is producing that error. so if you have any suggestions on that, it would be even more appreciated

    Thanks again!

  10. #10
    Registered User
    Join Date
    May 2006
    Location
    United States
    Posts
    10
    My appologies to Magos & Citizen... It was the names.
    Thank You Very Much For Your Help!
    -Ari.Patrick

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    When I change the name of the fstream the code compiles OK with Dev-C++. It seems to be the naming conflict: the compiler doesn't understand what you mean by the error line, assuming Account refers to the fstream. As
    Code:
    Account;
    would be a line of code that compiles (although it is meaningless), it may guess that you are missing a ; after it. But then record would be undeclared...

    You may be able to use the same name for different things if they are in a different scope, but they are not here...

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. Basically what happened here is that the name Account has been defined on the global scope when you defined the struct Account.

    Your line 27 is perfectly valid. Although a serious mistake. It creates an fstream named Account. However, within this scope your struct stopped being visible as soon as you used its name to define some other object. Had you tried to do this on the global scope and then another thing would have happened. You would be greeted with a redeclaration attempt error.

    On line 28 what happened is that the copiler got confused. Since Account is no longer a struct but an fstream, you can imagine why

    Basically the golden rule of never naming a local object the same as you named a global object is good advice. Never do it. As you just saw, you stopped having access to your global object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM