Can someone help me get the first function written?

  • Thread starter rendio
  • Start date
  • Tags
    Function
In summary: GetInventory(const string[], int[], int){//ifstream ifin;ifin.open("CWC_Inventory.txt");ofstream ifout;ifout.open("prog7_out_inv.txt"); double totalcost; while (getline(ifin,totalcost)) //totalcost+=strtod(ifin.getsin()); //ifout.write(totalcost);
  • #1
rendio
10
0

Homework Statement


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.

Homework Equations


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

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.
 
Physics news on Phys.org
  • #2
I am usiing C++ in codeblocks if anyone needs to see what I have so far. It is not much. :(
 
  • #3
rendio said:

Homework Statement


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.

Homework Equations


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

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.
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.
 
  • #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:

The company takes inventory weekly and sends a report to the main office. They use a hand scanner to take inventory

and dump the scanned data into file (scan_inv.txt). The produced file has an Item Code and count on each line

separated by spaces. You are to write a program that reads the master data into parallel arrays then reads the inventory

file and produces an inventory report (prog8_out_<NetID>.txt) that groups the inventory by category and consists of

the following:

• Titie: Cubicle Widgets Inventory

• Category

• Column Headings: Item Code Cost Inventory Count Item Name

• Repeat of above for each category

• Total Cost of inventory on hand

Your program should utilize the following functions (YOU MUST USE THESE NAMES AND PROTOTYPES) with the

prototypes as specified below

int GetMaster(string [], string [], string [], double [], int) – This function will have as parameters 4 parallel arrays that

contain the category, item name, item code, and cost for each of the stock items. A 5th parameter will be the maximum

number of products. It will return the count of the total number of items read in from the master data file. It will open

and close the master data file and will return -1 if there is a file error. Note: CWC_ Master.txt is provided for you. See
scan_inv.txt:

Code:
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:
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:
#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;
        }
}
 
  • #5
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.
 
  • Like
Likes rendio
  • #6
Awesome! And thank you for redoing that for the third time. Sorry I did that wrong again.
 
  • #7
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.
 
  • #8
Ok, let me run the program and add the debugger messages.
 
  • #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
I see. Calling is the issue. Thanks, let me try again.
 
  • #12
jedishrfu said:
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.
To add to what jedishrfu said, I don't see a main function. Is there some code you don't show in this thread?
 
  • #13
Its there listed 10 lines in after the MAXITEM definition.
 
  • #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:
#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.
 
  • #15
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).
 
  • #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. :-(
 
  • #17
then use the \t for the tab character:

Code:
cout << "hello" <<"\t" << "world";
 

Related to Can someone help me get the first function written?

1. What is the purpose of a function in programming?

A function is a block of code that performs a specific task. It is used to organize code into smaller, reusable sections, making it easier to manage and maintain. Functions can also improve the readability and efficiency of a program.

2. How do I write the first function in a program?

The first step in writing a function is to define it using the "function" keyword, followed by the function name and a set of parentheses. The code to be executed by the function is then enclosed in curly brackets {} after the parentheses. For example:
function addNumbers(a, b) {
    return a + b;
}

3. What is the significance of the "return" statement in a function?

The "return" statement is used to specify the value that should be returned by the function when it is called. This value can be assigned to a variable or used in other parts of the program. If a return statement is not included in the function, it will return undefined by default.

4. Can someone provide an example of a simple function?

Here is an example of a function called "multiply" that takes in two numbers as parameters and returns their product:

function multiply(a, b) {
    return a * b;
}

// Calling the function and assigning the result to a variable
let product = multiply(5, 10);

// The value of product will now be 50

5. How can I call a function in my program?

To call a function, you simply use the function name followed by a set of parentheses containing any necessary arguments. For example, using the "multiply" function from the previous question:
let product = multiply(5, 10);

// The value of product will now be 50, which is the result of the function

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
743
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top