writing an array

This is a discussion on writing an array within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having trouble writing code that will read the index of a value located in another file, that ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    writing an array

    Hi,

    I am having trouble writing code that will read the index of a
    value located in another file, that the user is searching for. I have an external file that contains part numbers, along with data about each part. What I want to do is to be able to enter a part number, search the file for that part number and return the index (where that part number is in the file) so that later I can read whatever data I want from that line.
    My biggest obstacle at the moment is the syntax to be used when I want to read in data from the file to the instream so that I can do something with it.

    Code:
    
    #include <iostream>       // cout, cin, <<, >>
    #include <fstream>        // ifstream
    #include <string>         // string  (future)
    #include <vector>         // vector<T>  (future)
    #include <algorithm>      // max, min_element() (future)
    // #include "myVector.h"  // read(), mean()  (future)
    #include <cassert>        // assert
    using namespace std;
    
    const int CAPACITY = 3;
    const int DOLLARS = 9999;
    int number[CAPACITY] = {0};
    int price[DOLLARS] = {0};
    int key = 0;
    
    int main()
    {
    
    ifstream inFile("product.txt");
    assert(inFile.is_open()); 
    inFile >> number >> price;
    
    cout << "Enter a product number: ";
    cin >> key;
    
    for (int i = 0; i <= CAPACITY; i++)
    {
        if (key == number[i]) //find the file index where value of key is stored
    	{
    		return i;
    	}
    }
    return -1;
    
    cout << "The index is: " << number[i]; // display the index, so that it can be used later.
    
    }
    The command
    Code:
     inFile >> number >> price
    generates a "binary >>" error, saying that there is no operator which takes a right hand operand of type int[3].

    I believe the compiler has a point, but I don't know what to do to make it happy. Any thoughts or ideas?
    I am using MSVS.net 2003

    Thanks.
    Semper Fi!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >const int CAPACITY = 3;
    >const int DOLLARS = 9999;
    >int number[CAPACITY] = {0};
    >int price[DOLLARS] = {0};

    For each part number, you will have a price for that part, so why are you declaring these arrays of different sizes. You have one with 3 elements, and one with 9999 elements.

    >inFile >> number >> price;
    If number and price are arrays, you can read the whole list of part numbers and prices into these arrays. Use a loop for this:
    Code:
    int n = 0;
    while (inFile >> number[n] >> price[n])
       n++;
    That's one idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  3. Writing an array to a file.
    By Queatrix in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2005, 02:41 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. writing contents of array to file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-26-2002, 05:06 PM

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