Thread: functions breaking other functions

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    functions breaking other functions

    Why would this happen??

    I have added a new function into my code, and this has produced some odd errors in another functions, like advising that some variables have not been declared when they have.

    I am completly lost, can anyone please advise as to what is going wrong?

    The new function I have added is:
    void delete_data( StockItem* );

    Compile log is as follows:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "D:\cpp\rearange.cpp" -o "D:\cpp\rearange.exe" -g3 -O0 -I"D:\data\c++\Dev-Cpp\include\c++" -I"D:\data\c++\Dev-Cpp\include\c++\mingw32" -I"D:\data\c++\Dev-Cpp\include\c++\backward" -I"D:\data\c++\Dev-Cpp\include" -L"D:\data\c++\Dev-Cpp\lib"
    D:/cpp/rearange.cpp: In function `void delete_data(StockItem*)':
    D:/cpp/rearange.cpp:135: parse error before `{' token
    D:/cpp/rearange.cpp:140: `ctr' undeclared (first use this function)
    D:/cpp/rearange.cpp:140: (Each undeclared identifier is reported only once for
    each function it appears in.)
    D:/cpp/rearange.cpp:140: `num_items' undeclared (first use this function)

    Execution terminated



    original working code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h> //for getch()
    
    using namespace std;
    
    const int NUM_OF_ITEMS = 125;
    
    struct StockItem
    {
        int code;
        char desc[20];
        float price;
    }stock[NUM_OF_ITEMS];
    
    /*struct bill_line
    {
        StockItem product;
        float weight;
        float cost;
    };
    
    struct bill
    {
        bill_line items[20];
        int num_lines;
    };*/
    
    void cls(void) { system("cls"); } //function designed to aid portability.
    
    void disp_menu( void );
    StockItem enter_data( void );
    void see_stock( StockItem*, int );
    void writeToDisk( StockItem*, int );
    int readFromDisk( StockItem* );
    
    
    int main()
    {
        int ans, ctr;
        int num_items = 0;
    
        do
        {
            do
            {
                disp_menu();
                cin >> ans;
            } while ((ans<1) || (ans>6));
    
            switch (ans)
            {
                case 1:
                    stock[num_items] = enter_data();
                    num_items++;
                    break;
                case 2:
                    see_stock(stock, num_items);
                    break;
                case 3:
                    writeToDisk( stock, num_items );
                    // NOT writing the whole array to disk.
                    break;
                case 4:
                    num_items = readFromDisk( stock );
                    // setting the number items we read from file
                    break;
                default:
                    break;
            }
        }while (ans!=5);
    
        return 0;
    }
    
    
    void disp_menu()
    {
        cout << "\n*** The Corner Shop ***\n\n";
        cout << "Select an option: \n\n";
        cout << "\t1. Enter new stock item " << endl;
        cout << "\t2. See current stock " << endl;
        cout << "\t3. Save stock to disk " << endl;
        cout << "\t4. Load stock from disk " << endl;
        cout << "\t5. Exit \n" << endl;
        cout << "option> ";
    }
    
    StockItem enter_data()
    {
        StockItem stock_item;
    
        cls();
        cout << "\n\nWhat is the product code: ";
        cin >> stock_item.code;
        cout << "What is the product description: ";
        cin.ignore();
        cin.getline(stock_item.desc, 20);
        cout << "What is the product price: ";
        cin >> stock_item.price;
        cls();
    
        return (stock_item);
    }
    
    void see_stock(StockItem stock[NUM_OF_ITEMS], int num_items)
    {
        int ctr;
    
        cls();
        cout << "\n\nHere is the stock listing:\n\n";
        for (ctr = 0; ctr < num_items; ++ctr)
        {
            cout << "Item " << ctr+1 << endl;
            cout << "Product Code: " << stock[ctr].code <<  endl;
            cout << "Product Description: " << stock[ctr].desc << endl;
            cout << "Price: " << stock[ctr].price << endl;
            cout << "------------------------------------------------\n";
        }
        cout << "\nHit any key to continue.....\n";
        getch();
        cls();
    }
    
    void writeToDisk( StockItem* array, int numOfItems )
    {
        ofstream outfile( "items.txt" );
        if (outfile.bad())
        {
            cout << "\n*** Error opening file ***\n";
        }
        for( int index = 0; index < numOfItems; index++ )
            outfile.write( (char*)&array[index], sizeof(StockItem) );
        cls();
    }
    
    int readFromDisk( StockItem* array )
    {
        int numOfItems = 0;
        ifstream infile( "items.txt" );
        if (infile.bad())
        {
            cout << "\n*** Error opening file ***\n";
        }
        StockItem tmp_stockItem;
        // read from the file until we reach EOF
        while( infile.read( (char*)&tmp_stockItem, sizeof(tmp_stockItem) ) )
        {
            // after a successfull read we store the the item into the array
            memcpy( &array[numOfItems], 
                &tmp_stockItem, 
                sizeof( array[numOfItems] ) );
            numOfItems++;
        }
        cls();
        // returning the number of array items we read from the file
        return numOfItems;
    }



    Broken code with new function:
    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h> //for getch()
    
    using namespace std;
    
    const int NUM_OF_ITEMS = 125;
    
    struct StockItem
    {
        int code;
        char desc[20];
        float price;
    }stock[NUM_OF_ITEMS];
    
    /*struct bill_line
    {
        StockItem product;
        float weight;
        float cost;
    };
    
    struct bill
    {
        bill_line items[20];
        int num_lines;
    };*/
    
    void cls(void) { system("cls"); } //function designed to aid portability.
    
    void disp_menu( void );
    StockItem enter_data( void );
    void delete_data( StockItem* );
    void see_stock( StockItem*, int );
    void writeToDisk( StockItem*, int );
    int readFromDisk( StockItem* );
    
    
    int main()
    {
        int ans, ctr;
        int num_items = 0;
    
        do
        {
            do
            {
                disp_menu();
                cin >> ans;
            } while ((ans<1) || (ans>6));
    
            switch (ans)
            {
                case 1:
                    stock[num_items] = enter_data();
                    num_items++;
                    break;
                case 2:
                    delete_data( stock );
                    break;
                case 3:
                    see_stock(stock, num_items);
                    break;
                case 4:
                    writeToDisk( stock, num_items );
                    // NOT writing the whole array to disk.
                    break;
                case 5:
                    num_items = readFromDisk( stock );
                    // setting the number items we read from file
                    break;
                default:
                    break;
            }
        }while (ans!=6);
    
        return 0;
    }
    
    
    void disp_menu()
    {
        cout << "\n*** The Corner Shop ***\n\n";
        cout << "Select an option: \n\n";
        cout << "\t1. Enter new stock item " << endl;
        cout << "\t2. Delete stock item " << endl;
        cout << "\t3. See current stock " << endl;
        cout << "\t4. Save stock to disk " << endl;
        cout << "\t5. Load stock from disk " << endl;
        cout << "\t6. Exit \n" << endl;
        cout << "option> ";
    }
    
    StockItem enter_data()
    {
        StockItem stock_item;
    
        cls();
        cout << "\n\nWhat is the product code: ";
        cin >> stock_item.code;
        cout << "What is the product description: ";
        cin.ignore();
        cin.getline(stock_item.desc, 20);
        cout << "What is the product price: ";
        cin >> stock_item.price;
        cls();
    
        return (stock_item);
    }
    
    void delete_data(StockItem stock[NUM_OF_ITEMS] )
    {
        StockItem stock_temp[NUM_OF_ITEMS];
        int item;
        
        cls();
        cout << "Enter item to delete ";
        cin >> item;
    
        for(int i = 0; i < NUM_OF_ITEMS; i++){
        {
            if(i == item)
                continue;
            stock_temp[i] = stock[i];
        }
        
        for(int j = 0; i < NUM_OF_ITEMS; j++)
            stock[j] = stock_temp[j];
        
        cls();
    }
    
    void see_stock(StockItem stock[NUM_OF_ITEMS], int num_items)
    {
        int ctr;
    
        cls();
        cout << "\n\nHere is the stock listing:\n\n";
        for (ctr = 0; ctr < num_items; ++ctr)
        {
            cout << "Item " << ctr+1 << endl;
            cout << "Product Code: " << stock[ctr].code <<  endl;
            cout << "Product Description: " << stock[ctr].desc << endl;
            cout << "Price: " << stock[ctr].price << endl;
            cout << "------------------------------------------------\n";
        }
        cout << "\nHit any key to continue.....\n";
        getch();
        cls();
    }
    
    void writeToDisk( StockItem* array, int numOfItems )
    {
        ofstream outfile( "items.txt" );
        if (outfile.bad())
        {
            cout << "\n*** Error opening file ***\n";
        }
        for( int index = 0; index < numOfItems; index++ )
            outfile.write( (char*)&array[index], sizeof(StockItem) );
        cls();
    }
    
    int readFromDisk( StockItem* array )
    {
        int numOfItems = 0;
        ifstream infile( "items.txt" );
        if (infile.bad())
        {
            cout << "\n*** Error opening file ***\n";
        }
        StockItem tmp_stockItem;
        // read from the file until we reach EOF
        while( infile.read( (char*)&tmp_stockItem, sizeof(tmp_stockItem) ) )
        {
            // after a successfull read we store the the item into the array
            memcpy( &array[numOfItems], 
                &tmp_stockItem, 
                sizeof( array[numOfItems] ) );
            numOfItems++;
        }
        cls();
        // returning the number of array items we read from the file
        return numOfItems;
    }
    Last edited by eth0; 01-11-2004 at 02:08 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The for loop inside your new function has mis-matched braces
    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.

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Damn.

    Its great how posts like these make you feel really stupid and embarassed.

    Hopefully spotting silly errors like this will become second nature in time.

    Thank you

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Talking Yeah, I guess you'll have to start-over with a new identity.

    Actually, this type of error can be very frustrating. I don't think that spotting mismatch errors ever becomes 2nd nature, but looking for stuff like this does.

    The problem is that the compiler gets confused. It knows that something is wrong, but it doesn't always know what you were trying to do. I once misplaced a bracket when doing a cut-and-paste, and the compiler reported over 100 errors!

    Misplaced & mismatched curly-bracket errors are probably the toughest to find, because they span many lines and are deeply nested. But, you can get weird errors with any punctuation-pair errors (quotes, parentheses, square brackets).

    Usually, the first error reported by the compiler will point to a line near the actual first error. But, often the error message reported by the compiler is not helpful.

    Sometimes you just have to comment-out code 'till you narrow-down the syntax error.

    You were smart to keep a copy of the working code!
    Last edited by DougDbug; 01-12-2004 at 04:54 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can eliminate an awful lot of those problems by slightly changing the way you enter your code.

    Take your for loop as an example, this is how I would have typed it in
    Code:
        for(int i = 0; i < NUM_OF_ITEMS; i++)
        {
        }
    One thing you'll note about this is that it will compile as it stands.

    Then move the cursor to where you want to add the loop body, and enter what you want to do

    Code:
        for(int i = 0; i < NUM_OF_ITEMS; i++)
        {
            if(i == item)
                continue;
            stock_temp[i] = stock[i];
        }
    The point is, by always typing in matching pairs of {} () [] "" '' /**/ before filling in the contents, you'll never have to worry about mis-matches again (or for that matter, getting them in the right place, if you have several nested } on your mental stack)
    It's a sure thing that if you have a couple of } floating around in your head that you need to type in at some point, any momentary distraction will cause you to forget something.

    I think a few extra cursor movements is well worth the whole range of potential errors it eliminates IMO
    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. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM