HELP!! NEw to Arrays/Functions.. LOST

This is a discussion on HELP!! NEw to Arrays/Functions.. LOST within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've been working on this assignment and i'm to the last part of it. The program was to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    HELP!! NEw to Arrays/Functions.. LOST

    Hey guys,
    I've been working on this assignment and i'm to the last part of it. The program was to make a function to check the file by a user-defined file name and open it. And once open, i am supposed to read from the integer from the first file and use that integer in the second file by showing how many rows to do for each string. Then in the second file I am supposed to multiply the int * the double and correspond the total by each name.

    I figured out how to do the ReadFirstFile function, but the ReadSecondFile function (which i know is totally wrong in my code) i am totally lost on how to make an Array using a FOR loop within a FOOR loop to do the second part.. can anyone help me out?? Any help would be appreciated!!

    The description is:
    Next, using the data in the integer array, read the appropriate number of data items from the second input
    file. Remember, there are three (3) pieces of data for each data item (line). While reading the "gadget"
    data, keep a total dollar amount of inventory for that person. Again, you must use the integer stored in the
    integer array to determine how many "gadget" items to read from the second input file.
    As you finish reading "gadget" items for one person, print a line of output that consists of the person's name
    and the total dollar amount for all of their inventory (gadget) items. For example, since Mickey Mouse has
    the integer 4 included in its data line, you must read 4 "gadget" items from the second file. This means
    Mickey Mouse has 45 hammers, 20 screwdrivers, 28 box cutters, and 14 wrenches in his inventory. Using
    the unit prices of $2.79 (hammer), $2.59 (screwdriver), $1.99 (box cutter), and $2.34 (wrench), Mickey
    Mouse will have a total dollar amount of inventory of $265.83. NOTE: We don't use the gadget name,
    although you must read it from the file to get to the gadget count and unit price.

    My first input file is:
    4 Mickey Mouse
    2 Donald Duck
    7 Wile E. Coyote
    5 Roadrunner
    1 Foghorn Leghorn

    My second input file is:
    hammer 45 2.79
    screwdriver 20 2.59
    boxCutter 28 1.99
    wrench 14 2.34
    milk 27 2.09
    butter 12 3.89
    tablet 165 0.59
    pen 89 1.19
    pencil 145 0.79
    tape 67 1.25
    staples 94 1.79
    erasers 82 0.67
    markers 23 2.99
    printer 5 78.99
    mouse 35 9.99
    monitor 5 298.95
    flashDrive 30 44.99
    CD-ROM 84 0.99
    blankets 18 34.98

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    void OpenCheck(ifstream&, string);
    int ReadFirstFile(ifstream&,int[],string[]);
    int ReadSecondFile(ifstream&,string[],int[],double[]);
    
    int main()
    {
        ifstream inFile1, inFile2;
        int num, count, count2;
        int intArray[50];
        string strArray[50];
        double dArray[50];
        string name, file1, file2;
        
        cout << "Enter a name for first input file:";
        cin >> file1;
        cout << "\nEnter a name for the second input file:";
        cin >> file2;
            
        OpenCheck(inFile1, file1);
        OpenCheck(inFile2, file2);
    
        count = ReadFirstFile(inFile1,intArray,strArray);
        count2 = ReadSecondFile(inFile2,strArray,intArray,dArray);
    
        system("Pause");
        return 0;
    
    }
    
    //functioin definition for OpenCheck
    void OpenCheck(ifstream& inFile1,string file)
    {
         inFile1.open(file.c_str());
         
         if(inFile1.fail())
         {
             cout << "Error Opening "
             << file
             << ". Program Terminated. \n";
             system("Pause");
             exit(1);
         }
    }
    
    //function definition for ReadFirstFile
    int ReadFirstFile(ifstream& inFile1,int intArray[],string strArray[])
    {
        int count, num;
        string name;
    
        // priming read
        inFile1 >> num;
        getline(inFile1,name);
        for (count = 0; count < 50; count++)
        {
            // store data in the integer and string arrays
            intArray[count] = num; 
            strArray[count] = name;
            // repeat priming read
            inFile1 >> num;
            //cout << strArray[count] << " ";
            getline(inFile1,name);
        }
        return count;
    }
    
    int ReadSecondFile(ifstream& inFile2,string strArray[], int intArray[], double dArray[])
    {
        int count2, num2;
        string gadgets;
        double price;
        
        // priming read
        inFile2 >> num2;
        getline(inFile2,gadgets);
        for (count2 = 0; count2 < 50; count2++)
        {
            // store data in the integer and string arrays
            intArray[count2] = num2; 
            strArray[count2] = gadgets;
            dArray[count2] = price;
            // repeat priming read
            inFile2 >> num2;
            cout << count2;
            cout << strArray[count2] << " ";
    
            getline(inFile2,gadgets);
        }
        return count2;
    }

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Your forward declarations are wrong:
    Code:
    void OpenCheck(ifstream& inFile1, string file);
    int ReadFirstFile(ifstream& inFile1,int intArray[],string strArray[]);
    int ReadSecondFile(ifstream& inFile2,string strArray[], int intArray[], double dArray[]);
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I see no problem with function declaration
    but

    dArray[count2] = price;

    price is used without initialization...
    also you read from the second file not in the order the data are stored in the line
    the string-name goes first
    then count (int)
    then price (double)
    you should extract the data from the string in the same order
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    exit() is in <cstdlib>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Your forward declarations are wrong
    No, they are fine. You don't need to provide variable names in function prototypes.

    >> i am totally lost on how to make an Array using a FOR loop within a FOOR loop to do the second part..
    Start with a simple example. Can you make nested for loops that add each of the numbers 0-9 to the numbers 10-19? What about nested for loops that get the sum of each positive number less than each number from 0-9?

    If you have those ideas down, you can translate them to using the values you get from the intarray as part of your nested loop structure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Date dt)??? I'm lost...
    By patricio2626 in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2006, 11:49 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 05:18 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21