1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can someone help me get the first function written?

  1. Apr 13, 2015 #1
    1. The problem statement, all variables and given/known data
    So I am stuck on getting started with the first function. The instructions and a sample output are there. I don't know if I am calling the function right at all.

    2. Relevant equations
    I think the only equations in this program are basic math adding the inventory

    3. The attempt at a solution
    I have tried looking at samples on line but I just can't figure this out. This is our first real programming assignment due tomr. Thanks.
     
  2. jcsd
  3. Apr 13, 2015 #2
    I am usiing C++ in codeblocks if anyone needs to see what I have so far. It is not much. :(
     
  4. Apr 13, 2015 #3

    Mark44

    Staff: Mentor

    I have edited your post, deleting the three files you attached. All three are text files, so please insert the text of these files directly into the post. It is irritating to have to open an attachment to see what you have done instead of being able to view your work right here on the page.

    When you do this, please surround the code from each file with a [ code ] and [ /code ] pair of tags.
     
  5. Apr 13, 2015 #4
    Fantastic sir, Thank you for the information and I apologize for not following the directions for posting. Could anyone please help me get the first function started on here? I think after I get this first one I will be able to do getinventory and getlocation on my own. My professor wants us to do this first:


    scan_inv.txt:

    Code (Text):

    udmtd01  8
    udst01   10
    dgsr01   2
    cwms01   7
    cwwdc01  4
    cwrl01   0
    rcft01   12
    wulp01   2
    wups01   4
    dgfh01   15
    rcsrb01  6
    rcpg01   10
    udpb01   8
    dgzb01   13
    wuff01   1
    wuzc01   7
    dgnc01   8
    cwmb01   2
    rcbc01   11
    udcc01   6
    udmh01   5
    dgdh01   0
    wuct01   2
    dgbe01   9
    dgdb01   6
     
    CWC_Master.txt:
    Code (Text):

    Cubicle_Warfare
    Marshmallow Shooter
    cwms01
    19.99
    Cubicle_Warfare
    Wooden Desk Catapult
    cwwdc01
    29.99
    Cubicle_Warfare
    NERF 3-Dart Mini Blaster
    cwmb01
    9.99
    Cubicle_Warfare
    USB Rocket Launcher
    cwrl01
    129.99
    Remote_Control
    Doctor Who Flying TARDIS
    rcft01
    59.99
    Remote_Control
    Sphero Robotic Ball
    rcsrb01
    129.99
    Remote_Control
    Bubble Copter
    rcbc01
    44.99
    Remote_Control
    PacMan & Ghost
    rcpg01
    39.99
    USB_Devices
    Star Wars Mimobot Thumb Drives
    udmtd01
    19.99
    USB_Devices
    R2-D9 USB Car Charger
    udcc01
    39.99
    USB_Devices
    USB Squirming Tentacle
    udst01
    14.99
    USB_Devices
    14 Port USB Monster Hub
    udmh01
    49.99
    USB_Devices
    USB Plasma Ball
    udpb01
    9.99
    Desktop_Gadgets
    Solar Roach
    dgsr01
    3.98
    Desktop_Gadgets
    Zombie Bobblehead
    dgzb01
    7.98
    Desktop_Gadgets
    Desktop Horseshoes
    dgdh01
    8.98
    Desktop_Gadgets
    Desktop Basketball
    dgdb01
    8.98
    Desktop_Gadgets
    Finger Hockey
    dgfh01
    8.98
    Desktop_Gadgets
    Newton's Cradle
    dgnc01
    4.98
    Desktop_Gadgets
    Japanese Blinking Eyeballs
    dgbe01
    39.98
    Wind-ups
    Walking Robot Pencil Sharpener
    wups01
    12.98
    Wind-ups
    Chomping Teeth
    wuct01
    .98
    Wind-ups
    Flipping Frogs
    wuff01
    4.99
    Wind-ups
    Zecar Flywheel Car
    wuzc01
    18
    Wind-ups
    Le Pinch Wind Up
    wulp01
    20
     
    C++ Codeblocks
    Code (C):

    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;

    int GetMaster (string[], string[], string[], double[], int);

    bool GetInventory(const string[], int[], int);

    int GetLocation(string, const string[], int);

    bool CreatInventoryReport(const string[], const string[], const string[], const double[],
                              const int[], int);

    double CalcValueOnHand(const double[],const int[], int);

    const int MAXITEM = 100;
    int main()
    {
        string category [MAXITEM];
        string item [MAXITEM];
        string code [MAXITEM];
        double cost [MAXITEM];
        int maxproducts [MAXITEM];

    }



    int GetMaster(string category[], string item[], string code[], double cost[], int maxproducts)
    {
    ifstream ifin;
    ifin.open("CWC_Master.txt");
    ofstream ifout;
    ifout.open("prog7_out_rmd55.txt");

        int i = 0;
        while (GetMaster(i< MAXITEM > i++))
          {
            //ifin>> category[I];
            ifin.getline>> item[I];
            ifin>> code[I];
            ifin>> cost[I];
            ifin.getline>> maxproducts[I];

          }
            else (!ifin)
            {
                return -1;
            }
    }

     
     
  6. Apr 13, 2015 #5

    jedishrfu

    Staff: Mentor

    I edited your post using the code tags that Mark44 had suggested making it more readable. If you try to edit it you will see how it is done.
     
  7. Apr 13, 2015 #6
    Awesome! And thank you for redoing that for the third time. Sorry I did that wrong again.
     
  8. Apr 13, 2015 #7

    jedishrfu

    Staff: Mentor

    When you run the program what happens?

    If you don't know how to use the C++ debugger then insert some print statements in your program.

    Remember all C++ programs start executing the main() method first.
     
  9. Apr 13, 2015 #8
    Ok, let me run the program and add the debugger messages.
     
  10. Apr 13, 2015 #9
    I can't figure how to use code blocks debugger.

    But the errors are:

    "cannot convert bool to std::string"

    and

    "no match for 'operator>>' overloaded operator type."
     
  11. Apr 13, 2015 #10

    jedishrfu

    Staff: Mentor

  12. Apr 13, 2015 #11
    I see. Calling is the issue. Thanks, let me try again.
     
  13. Apr 13, 2015 #12

    Mark44

    Staff: Mentor

    To add to what jedishrfu said, I don't see a main function. Is there some code you don't show in this thread?
     
  14. Apr 13, 2015 #13

    jedishrfu

    Staff: Mentor

    Its there listed 10 lines in after the MAXITEM definition.
     
  15. Apr 14, 2015 #14
    Thanks a lot you guys! All the replies helped a lot. I was trying to take hints from you all without asking to much. What is the point if I can't solve the problem alone, right? So after figuring out working with my int main(), I finally got everything to work.

    NOW my print out looks like crap and I can't figure out how to get it into a format that is readable. Making this look like it was formatted in MS Word is how Dr. X wants it, with categories and items under. This is tough; he said wouldn't take off so much for our printout looking bad, but I kinda want to know how to do it.

    Thanks for everything so far!!!

    Code (Text):

    #include <string>
     #include <iostream>
     #include <iomanip>
     #include <fstream>

     using namespace std;

    // Function prototypes.
     int GetMaster (string[], string[], string[], double[], int);
     bool GetInventory(const string[], int[], int);
     int GetLocation(string, const string[], int);
     bool CreateInventoryReport(const string[], const string[], const string[], const double[],
                               const int[], int);

     double CalcValueOnHand(const double[],const int[], int);

     // Global declarations.
     const int MAXITEM = 100;

     ifstream fin;
     ofstream fout;

     int main()
     {
        // Variables & declarations.
        string category[MAXITEM];
        string itemName [MAXITEM];
        string code [MAXITEM];
        double cost [MAXITEM];
        int inventory[MAXITEM];

        string itemCode;

        int maxproducts;
        int index;

        maxproducts = GetMaster(category, itemName, code, cost, maxproducts);
        GetInventory(code, inventory, maxproducts);
        CreateInventoryReport(category, itemName, code, cost, inventory, maxproducts);

     }

     int GetMaster(string category[], string itemName[], string code[], double cost[], int maxproducts)
     {
        int counter = 0;

        fin.open("CWC_Master.txt");

        if(fin)
            {
                while(!fin.eof())
                    {
                        getline(fin, category[counter]);
                        getline(fin, itemName[counter]);
                        getline(fin, code[counter]);
                        fin >> cost[counter];
                        fin.ignore();
                        maxproducts = counter;
                        counter++;
                    }
                fin.close();
                return maxproducts;
            }

        else
            {
                cout << "Error! \"CWC_Master.txt\" was not found!\n";
                return -1;
            }
     }

     bool GetInventory(const string code[], int inventory[], int maxproducts)
     {
        fin.open("scan_inv.txt");
        string itemCode;
        int index;

        if(fin)
            {
                for(int y = 0; y < maxproducts; y++)
                    {
                      index = GetLocation(itemCode, code, maxproducts);
                      fin >> inventory[index];
                    }
                fin.close();
                return true;
            }
        else
            {
                cout << "Error! \"scan_inv.txt\" was not found!\n";
                return false;
            }
     }

     int GetLocation(string itemCode, const string code[], int maxproducts)
     {
        fin >> itemCode;
        for(int x = 0; x < maxproducts; x++)
        {
            if(itemCode == code[x])
                {
                    return x;
                }
        }
     }

     bool CreateInventoryReport(const string category[], const string itemName[],
                               const string code[], const double cost[], const int inventory[],
                               int maxproducts)
     {
        fout.open("prog8_out_.txt");

        if(fout)
            {
                int sum;
                sum = CalcValueOnHand(cost, inventory, maxproducts);
                fout << setprecision(3);
                fout << setw(50) << "Cubicle Widgets Inventory\n";
                fout << category[0] << endl;


                for(int z = 0; z < maxproducts; z++)
                    {
                        if(category[0] == category[z])
                            {
                                fout << "Item Code\t\t" << "Cost\t\t" << "Inventory\t\t" << "Item Name\n";
                                fout << code[z] << "\t\t$" << cost[z] << "\t\t" << inventory[z] << "\t\t" << itemName[z] << endl << endl;
                            }
                        else if (category[z] != category[z+1])
                            {
                                fout << category[0] << endl;
                                fout << "Item Code\t\t" << "Cost\t\t" << "Inventory\t\t" << "Item Name\n\n";
                            }
                        else if (category[z] == category[z+1])
                            {
                                fout << code[z] << "\t\t$" << cost[z] << "\t\t" << inventory[z] << "\t\t" << itemName[z];
                            }
                    }
            }
     }

     double CalcValueOnHand(const double cost[], const int inventory[], int maxproducts)
     {
        int sum = 0;
        for(int i = 0; i < maxproducts; i++)
            {
                sum += (cost[i]*inventory[i]);
            }
        return sum;
     }
     
    the two text files are still at the top of this forum. This happens to be the hardest part for all of us in class.
     
  16. Apr 14, 2015 #15

    jedishrfu

    Staff: Mentor

    You might be able to print tab characters to line things up or better yet use the sprintf function to create a character string of your output line to print using the c formatting capability (aka printf format codes).
     
  17. Apr 14, 2015 #16
    cool. I will check sprintf out. I know if it isn't something covered in class, he will take off a lot of points claiming that the command or way we are programming is something we don't "know" because we haven't learned it. And I don't think he has sprintf anywhere in his power points. :-(
     
  18. Apr 14, 2015 #17

    jedishrfu

    Staff: Mentor

    then use the \t for the tab character:

    Code (C):

    cout << "hello" <<"\t" << "world";
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Can someone help me get the first function written?
Loading...