Can someone help me get the first function written?

  • Thread starter Thread starter rendio
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
The discussion revolves around a programming assignment where the user is struggling to write the first function in C++. They are required to read inventory data from a file and produce a formatted report, but they are unsure about function calls and syntax errors in their code. Users suggest debugging techniques, such as using print statements and checking the main function's structure. The original poster acknowledges the help and expresses a desire to improve the formatting of their output report. The conversation highlights common challenges faced by beginners in programming assignments.
rendio
Messages
10
Reaction score
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
I am usiing C++ in codeblocks if anyone needs to see what I have so far. It is not much. :(
 
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.
 
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;
        }
}
 
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
Awesome! And thank you for redoing that for the third time. Sorry I did that wrong again.
 
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.
 
Ok, let me run the program and add the debugger messages.
 
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";
 

Similar threads

Back
Top