CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2008
    Posts
    3

    Unhappy need help with a c++ program :(

    Hello people,

    was wondering if you could help me with the error in this program.

    Code:
     #include <iostream>
    using namespace std;
    
    struct part
    {
        int itemNo;
        string make;
        string description;
        float cost;
        int qty;
    }   parts[21]={{6510, "Axle", "FORD", 435.6, 30},{7657, "Oil Seal", "HOLDEN", 132.8, 5},{6800, "Front bearing", "MAZDA", 238.5, 12},{6901, "Car Seat", "FORD", 345.8, 8},{8800, "Rear Mirror", "TOYOTA", 56.25, 9},{7654, "LH Front Door", "VOLVO", 675.39, 12},{6990, "Rocker Cover", "ALFA ROMEO", 120.65, 7},{7865, "Cam Shaft", "MAZDA", 98.7, 6},{7906, "Fuel Injector", "PORSCHE", 25.8, 28},{8765, "Hose", "FORD", 15.2, 2},{9721, "Cooling Fan", "VW", 68.4, 9},{6844, "LH Front Shock Absorber", "HYUNDAI", 135.7, 22},{8901, "Front Engine Mount", "TOYOTA", 34.7, 10},{7899, "Main bearing", "HOLDEN", 34.78, 6},{6834, "Piston", "HOLDEN", 52.34, 12},{8722, "Piston Rings", "FORD", 23.67, 20},{9110, "Synchomesh", "PORSCHE", 62.8, 28},{8392, "Hydraulic Oil - 1l", "AUDI", 12.1, 20},{7953, "Brake Fluid - 500 ml", "MAZDA", 8.99, 25},{8395, "Oil Filter", "AUDI", 10.5, 17},{8309, "Rear Main Seal", "VW", 13.67, 16}};
    
    struct orderedPart
    {
        int itemNo;
        string make;
        string description;
        float cost;
        int qty;
    } orderedParts[100];
    int main()
    {
        int totalquantitysofar=0;
        float costsofar=0;
        int counter=0;
        int quantity;
        int target=0;
        while (target!=9999)
        {
                cout<<"Please enter the order number. Thank you :-) :";
                cin>>target;
                for (int i=0;i<=21;i++)
                {
                    if (target==parts[i].itemNo)
                    {
                        cout<<"Item number: "<< parts[i].itemNo<<endl;
                        cout<<"Make: "<<parts[i].make<<endl;
                        cout<<"Description: "<<parts[i].description<<endl;
                        cout<<"Cost: "<<parts[i].cost<<endl;
                        cout<<"Quantity in stock: "<<parts[i].qty<<endl;
                        cout<<"Please enter quantity to be ordered: ";
                        cin>>quantity;
                        if (quantity>parts[i].qty)
                        {
                            cout<<"Please enter a valid quantity number: ";
                            cin>> quantity;
                            if (quantity>parts[i].qty)
                            {
                                cout<<"Quantity still invalid! Terminating order!"<<endl;
                            }
                        } cout<<"Order accepted. Total Dollar value: "<<parts[i].cost*quantity<<endl;
                        orderedParts[counter].itemNo=parts[i].itemNo;
                        orderedParts[counter].make=parts[i].make;
                        orderedParts[counter].description=parts[i].description;
                        orderedParts[counter].cost=parts[i].cost;
                        orderedParts[counter].qty=quantity;
                        totalquantitysofar+=quantity;
                        costsofar+=parts[i].cost*quantity;
                    }
                }
        } while (int j=0;j<counter;j++)
        {
            cout<<"Item number: "<<orderedParts[j].itemNo<<endl;
            cout<<"Make: "<<orderedParts[j].make<<endl;
            cout<<"Description: "<<orderedParts[j].description<<endl;
            cout<<"Total cost of order: "<<orderedParts[j].cost<<endl;
            cout<<"Quantity ordered: "<<orderedParts[j].qty<<endl;
        }cout<<"Total quantity of products ordered: "<<totalquantitysofar<<endl;
        cout<<"Total cost of products ordered: "<<costsofar<<endl;
    }
    the program gives me an error in the last while loop. evrything else works fine. i just can't seem to understand why it won't let me run it. the error is something like "j was not declared in this scope" and a whole bunch of other lines all pointing to line 61.when i edit the line out. the program works fine. please help. thanks
    Last edited by sidh25; September 20th, 2008 at 11:53 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: need help with a c++ program :(

    Please format your code properly and post it within [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: need help with a c++ program :(

    Change
    Code:
    while (int j=0;j<counter;j++)
    to
    Code:
    for (int j=0;j<counter;j++)
    - Alon

  4. #4
    Join Date
    Sep 2008
    Posts
    3

    Unhappy Re: need help with a c++ program :(

    thanx...this is my new code... it works.. but it doesnt show me the last while loop in the output...
    Code:
    #include <iostream>
    using namespace std;
    
    struct part
    {
        int itemNo;
        string make;
        string description;
        float cost;
        int qty;
    }   parts[21]={{6510, "Axle", "FORD", 435.6, 30},{7657, "Oil Seal", "HOLDEN", 132.8, 5},{6800, "Front bearing", "MAZDA", 238.5, 12},{6901, "Car Seat", "FORD", 345.8, 8},{8800, "Rear Mirror", "TOYOTA", 56.25, 9},{7654, "LH Front Door", "VOLVO", 675.39, 12},{6990, "Rocker Cover", "ALFA ROMEO", 120.65, 7},{7865, "Cam Shaft", "MAZDA", 98.7, 6},{7906, "Fuel Injector", "PORSCHE", 25.8, 28},{8765, "Hose", "FORD", 15.2, 2},{9721, "Cooling Fan", "VW", 68.4, 9},{6844, "LH Front Shock Absorber", "HYUNDAI", 135.7, 22},{8901, "Front Engine Mount", "TOYOTA", 34.7, 10},{7899, "Main bearing", "HOLDEN", 34.78, 6},{6834, "Piston", "HOLDEN", 52.34, 12},{8722, "Piston Rings", "FORD", 23.67, 20},{9110, "Synchomesh", "PORSCHE", 62.8, 28},{8392, "Hydraulic Oil - 1l", "AUDI", 12.1, 20},{7953, "Brake Fluid - 500 ml", "MAZDA", 8.99, 25},{8395, "Oil Filter", "AUDI", 10.5, 17},{8309, "Rear Main Seal", "VW", 13.67, 16}};
    
    struct orderedPart
    {
        int itemNo;
        string make;
        string description;
        float cost;
        int qty;
    } orderedParts[100];
    int main()
    {
        int totalquantitysofar=0;
        float costsofar=0;
        int counter=0;
        int quantity;
        int target=0;
        while (target!=9999)
        {
                cout<<"Please enter the order number. Thank you :-) :";
                cin>>target;
                for (int i=0;i<=21;i++)
                {
                    if (target==parts[i].itemNo)
                    {
                        cout<<"Item number: "<< parts[i].itemNo<<endl;
                        cout<<"Make: "<<parts[i].make<<endl;
                        cout<<"Description: "<<parts[i].description<<endl;
                        cout<<"Cost: "<<parts[i].cost<<endl;
                        cout<<"Quantity in stock: "<<parts[i].qty<<endl;
                        cout<<"Please enter quantity to be ordered: ";
                        cin>>quantity;
                        if (quantity>parts[i].qty)
                        {
                            cout<<"Please enter a valid quantity number: ";
                            cin>> quantity;
                            if (quantity>parts[i].qty)
                            {
                                cout<<"Quantity still invalid! Terminating order!"<<endl;
                            }
                        } cout<<"Order accepted. Total Dollar value: "<<parts[i].cost*quantity<<endl;
                        orderedParts[counter].itemNo=parts[i].itemNo;
                        orderedParts[counter].make=parts[i].make;
                        orderedParts[counter].description=parts[i].description;
                        orderedParts[counter].cost=parts[i].cost;
                        orderedParts[counter].qty=quantity;
                        totalquantitysofar+=quantity;
                        costsofar+=parts[i].cost*quantity;
                        parts[i].qty=parts[i].qty-quantity;
                    }
                }
        } for (int j=0;j<counter;j++)
        {
            cout<<"Item number: "<<orderedParts[j].itemNo<<endl;
            cout<<"Make: "<<orderedParts[j].make<<endl;
            cout<<"Description: "<<orderedParts[j].description<<endl;
            cout<<"Total cost of order: "<<orderedParts[j].cost<<endl;
            cout<<"Quantity ordered: "<<orderedParts[j].qty<<endl;
        }cout<<"Total quantity of products ordered: "<<totalquantitysofar<<endl;
        cout<<"Total cost of products ordered: "<<costsofar<<endl;
    }
    Last edited by sidh25; September 21st, 2008 at 12:03 AM.

  5. #5
    Join Date
    Oct 2006
    Posts
    616

    Re: need help with a c++ program :(

    The problem is marked in red:
    Quote Originally Posted by sidh25
    thanx...this is my new code... it works.. but it doesnt show me the last while loop in the output...
    Code:
    #include <iostream>
    using namespace std;
    
    struct part
    {
        int itemNo;
        string make;
        string description;
        float cost;
        int qty;
    }   parts[21]={{6510, "Axle", "FORD", 435.6, 30},{7657, "Oil Seal", "HOLDEN", 132.8, 5},{6800, "Front bearing", "MAZDA", 238.5, 12},{6901, "Car Seat", "FORD", 345.8, 8},{8800, "Rear Mirror", "TOYOTA", 56.25, 9},{7654, "LH Front Door", "VOLVO", 675.39, 12},{6990, "Rocker Cover", "ALFA ROMEO", 120.65, 7},{7865, "Cam Shaft", "MAZDA", 98.7, 6},{7906, "Fuel Injector", "PORSCHE", 25.8, 28},{8765, "Hose", "FORD", 15.2, 2},{9721, "Cooling Fan", "VW", 68.4, 9},{6844, "LH Front Shock Absorber", "HYUNDAI", 135.7, 22},{8901, "Front Engine Mount", "TOYOTA", 34.7, 10},{7899, "Main bearing", "HOLDEN", 34.78, 6},{6834, "Piston", "HOLDEN", 52.34, 12},{8722, "Piston Rings", "FORD", 23.67, 20},{9110, "Synchomesh", "PORSCHE", 62.8, 28},{8392, "Hydraulic Oil - 1l", "AUDI", 12.1, 20},{7953, "Brake Fluid - 500 ml", "MAZDA", 8.99, 25},{8395, "Oil Filter", "AUDI", 10.5, 17},{8309, "Rear Main Seal", "VW", 13.67, 16}};
    
    struct orderedPart
    {
        int itemNo;
        string make;
        string description;
        float cost;
        int qty;
    } orderedParts[100];
    int main()
    {
        int totalquantitysofar=0;
        float costsofar=0;
        int counter=0;
        int quantity;
        int target=0;
        while (target!=9999)
        {
                cout<<"Please enter the order number. Thank you :-) :";
                cin>>target;
                for (int i=0;i<=21;i++)
                {
                    if (target==parts[i].itemNo)
                    {
                        cout<<"Item number: "<< parts[i].itemNo<<endl;
                        cout<<"Make: "<<parts[i].make<<endl;
                        cout<<"Description: "<<parts[i].description<<endl;
                        cout<<"Cost: "<<parts[i].cost<<endl;
                        cout<<"Quantity in stock: "<<parts[i].qty<<endl;
                        cout<<"Please enter quantity to be ordered: ";
                        cin>>quantity;
                        if (quantity>parts[i].qty)
                        {
                            cout<<"Please enter a valid quantity number: ";
                            cin>> quantity;
                            if (quantity>parts[i].qty)
                            {
                                cout<<"Quantity still invalid! Terminating order!"<<endl;
                            }
                        } cout<<"Order accepted. Total Dollar value: "<<parts[i].cost*quantity<<endl;
                        orderedParts[counter].itemNo=parts[i].itemNo;
                        orderedParts[counter].make=parts[i].make;
                        orderedParts[counter].description=parts[i].description;
                        orderedParts[counter].cost=parts[i].cost;
                        orderedParts[counter].qty=quantity;
                        totalquantitysofar+=quantity;
                        costsofar+=parts[i].cost*quantity;
                        parts[i].qty=parts[i].qty-quantity;
                    }
                }
        } for (int j=0;j<counter;j++)
        {
            cout<<"Item number: "<<orderedParts[j].itemNo<<endl;
            cout<<"Make: "<<orderedParts[j].make<<endl;
            cout<<"Description: "<<orderedParts[j].description<<endl;
            cout<<"Total cost of order: "<<orderedParts[j].cost<<endl;
            cout<<"Quantity ordered: "<<orderedParts[j].qty<<endl;
        }cout<<"Total quantity of products ordered: "<<totalquantitysofar<<endl;
        cout<<"Total cost of products ordered: "<<costsofar<<endl;
    }
    So, where do you increase the counter variable ? you don't.
    Note that you use a static array of size 100, so when you post the 101 order - your program is going to crash due to access violation.

    Regards,
    Zachm

  6. #6
    Join Date
    Sep 2008
    Posts
    3

    Talking Re: need help with a c++ program :(

    yayyy it worked hurrayyyyy!!! thank yooz soo much so how do u suggest i make that into a dynamic array size rather than static??i thought about that.. but 2 much work

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: need help with a c++ program :(

    so how do u suggest i make that into a dynamic array size rather than static?
    Use a std::vector.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured