Hey everyone, i was suppossed to convert my array notation over to pointer notation in my functions, but am i missing something? i though this would be correct. Can someone point me in the right direction

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

//function prototypes
void intro();
void readFile(int* &itemNumbers, double* &itemPrices, int* &itemQuantities, int &itemCount);
void sortData(int *itemNumbers, double *itemPrices, int *itemQuantities,int arraySize);
void printData(int *itemNumbers, double *itemPrices, int *itemQuantities, int arraySize);
int searchData(int *itemNumbers, int searchValue, const int arraySize);
void searchLoop(int *itemNumbers, double *itemPrices, int *itemQuantities, int arraySize);

/*
 * Main
 */
int main() {
    //declare variables
    int arraySize = 0;
    int* itemNumbers = NULL;  //We need an array of item numbers.
    double* itemPrices = NULL;    //We need an array of prices.
    int* itemQuantities = NULL;   //We need an array of quantities.

    //Welcome message to user
    intro();

    //read in the data from the file
    readFile(itemNumbers, itemPrices, itemQuantities, arraySize);

    //sort the data
    sortData(itemNumbers, itemPrices, itemQuantities, arraySize);

    //print the data in table format
    printData(itemNumbers, itemPrices, itemQuantities, arraySize);

    //search for an item number and display its cost and quantity
    searchLoop(itemNumbers, itemPrices, itemQuantities, arraySize);

     //Since ReadFile uses "new .. []" to allocate memory for our pointer arrays, we must delete[] it when finished..
    delete[] itemNumbers;
    delete[] itemPrices;
    delete[] itemQuantities;
    return 0;
}

/*
 * Function: intro
 * Description: Displays an intro message to the user
 */
void intro() {
    //Program introduction
    cout << "*****************************************" << endl
            << "** Welcome to my program               ** \n"
            << "** Created by: Brandon                 ** \n"
            << "** Sorting and Searching Arrays        **" << endl
            << "*****************************************" << endl << endl;
}

/*
 * Function: readData
 * Description: Retrieves 3 item numbers, their price and quantity
 * from a file and stores them in 3 parallel arrays.
 */
void readFile (int* &itemNumbers, double* &itemPrices, int* &itemQuantities, int &itemCount)
{
    ifstream inFile("inventoryData.txt"); //Setup the stream to read the file.
    if (inFile.is_open()) //If we can open the file, lets start reading it.
    {
        inFile >> itemCount;  //Read the first line of the file. Store it in "ItemCount" variable.
        itemNumbers = new int[itemCount]; //Allocate space to hold "3" integers (itemCount) 
        itemPrices = new double[itemCount]; //need space for 3 prices.
        itemQuantities = new int[itemCount]; //And 3 quantities..
        for (int i = 0; i < itemCount; i++)  //Lets read "3" lines/items.. Remember "ItemCount" is the first line of the file. Aka "3".
        {
            inFile >> itemNumbers[i]; //Read item number first.
            inFile >> itemPrices[i];      //Read prices next.
            inFile >> itemQuantities[i];  //Read quantities after.
        }
        inFile.close(); //Close it when finished.
    }else{
        cout << "********** An Error has occurred!! **********" << endl << endl;
        cout << "The data text file was not in the correct directory or does not exist." << endl <<
                "Find the file and place it in the same directory and restart the program." << endl << endl <<
                "The program will now terminate." << endl;
        exit(0);
    }
}

/*
 * Function: sortData
 * Description: Sorts 3 arrays in parallel, in ascending order.
 */
void sortData(int *itemNumbers, double *itemPrices, int *itemQuantities, int arraySize) {

    //variables
    int numbersTemp, quantityTemp;
    double pricesTemp;
    bool swap;

    do {
        swap = false;
        for (int count = 0; count < arraySize - 1; count++) {
            if (itemNumbers[count] > itemNumbers[count + 1]) {
                numbersTemp = itemNumbers[count];
                quantityTemp = itemQuantities[count];
                pricesTemp = itemPrices[count];

                itemNumbers[count] = itemNumbers[count + 1];
                itemPrices[count] = itemPrices[count + 1];
                itemQuantities[count] = itemQuantities[count + 1];

                itemNumbers[count + 1] = numbersTemp;
                itemPrices[count + 1] = pricesTemp;
                itemQuantities[count + 1] = quantityTemp;

                swap = true;
            }
        }
    } while (swap);
}

/*
 * Function: printData
 * Description: prints the values of 3 arrays in table format
 */
void printData(int *itemNumbers, double *itemPrices, int *itemQuantities, int arraySize) {

    //set the header for the table
    cout << "|" << setw(20) << "Item Numbers" << setw(5) << "|" << setw(17)
            << "Item Prices" << setw(6) << "|" << setw(20) << "Item Quantity" << setw(4) << "|" << endl
            << "=========================================================================" << endl;
    //print the data from the arrays
    for (int count = 0; count < arraySize; count++) {
        cout << "|" << setw(13) << *(itemNumbers+count)
                << setw(12) << "|" << setw(13) << fixed << setprecision(2) << *(itemPrices+count)
                << setw(10) << "|" << setw(13) << *(itemQuantities+count) << setw(11) << "|" << endl

                //end of each row include a line
                << "-------------------------------------------------------------------------" << endl;
    }
}

/*
 * Function: searchData
 * Description: Search data is a binary search that accepts and array
 * and a search value, it then searches the array with the search value
 * and returns the values position in the array  or  -1 if its not in the array
 */
int searchData(int *itemNumbers, int searchValue, const int arraySize) {
    //search data using a binary search
    int low = 0;
    int high = arraySize - 1;
    int mid;

    while (low <= high) {
        mid = (low + high) / 2;

        if (searchValue == *(itemNumbers+mid)) {
            return mid;
        } else if (searchValue > *(itemNumbers+mid)) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return -1;
}

/*
 * Function: searchLoop
 * Description: Asks the user to search for a number or exit(loop)
 * when a number is inputed it will call the searchData() function
 * and will print out the results for that search.
 */
void searchLoop(int *itemNumbers, double *itemPrices, int *itemQuantities, int arraySize) {

    // Declare variables
    int result = 0; //holds search result
    int searchValue = 0; //holds search input from user

    //search for an item number and display its cost and quantity
    do {

        //start on a new line
        cout << endl << endl;
        cout << "Enter an item number to search or negative number to exit: ";
        cin >> searchValue;

        //if searchValue is less than 0 quit loop
        if(searchValue < 0){
            break;
        }

        //search for the item number the user entered   
        result = searchData(itemNumbers, searchValue, arraySize);


        //check to see if the search returned a result.
        if (result == -1) {

            //Search did not return any results
            cout << "\n No results found..." << endl;

        } else if (result >= 0) //returned a result
        {

            cout << "Search results for item number " << searchValue << " :" << endl << endl;

            //set the header for the table
            cout << "|" << setw(17) << "Item Prices" << setw(6) << "|"
                    << setw(20) << "Item Quantity" << setw(4) << "|" << endl
                    << "================================================" << endl;

            //output the price and quantity
            cout << "|" << setw(13) << fixed << setprecision(2) << *(itemPrices+result)
                    << setw(10) << "|" << setw(13) << *(itemQuantities+result)
                    << setw(11) << "|" << endl

                    //end of each row include a line
                    << "------------------------------------------------" << endl;
        }

    } while (searchValue >= 0);
}

readfile() expect the first parameter to be a pointer to a pointer -- you only provided the pointer on line 29. Here is how the first parameter should look like. All the other parameters have the same problem.

readFile(&itemNumbers, itemPrices, itemQuantities, arraySize);

Since itemNumbers has two stars in order to allocate memory to it you need to dereference the variable, for example line 71

*itemNumbers = new int[itemCount];

This allocates memory to the variable that's declared in main().

so i need to change the lines to this here?

  //read in the data from the file
    readFile(&itemNumbers, &itemPrices, &itemQuantities, arraySize);

    //sort the data
    sortData(&itemNumbers, &itemPrices, &itemQuantities, arraySize);

    //print the data in table format
    printData(&itemNumbers, &itemPrices, &itemQuantities, arraySize);

    //search for an item number and display its cost and quantity
    searchLoop(&itemNumbers, &itemPrices, &itemQuantities, arraySize);




*itemNumbers = new int[itemCount]; //Allocate space to hold "3" integers (itemCount) 
*itemPrices = new double[itemCount]; //need space for 3 prices.
*itemQuantities = new int[itemCount];

I notice you have use parentheses in places where they aren't needed, for example line 159 the ( is in the wrong place. itemNuimbers has to be dereferenced before the value of mid can be added to it's value. Otherwise the way you have it coded the valie of mid will be added to the address of itemNumbers which is wrong, not to the value that itemNumbers points to.

if (searchValue == (*itemNumbers+mid)) {

Since you are required to convert from array notation to pointers, you might need to modify the sortData() function. For example
itemNumbers[count] = itemNumbers[count + 1];

would become

*(itemNumbers+count) = *(itemNumbers+count + 1);

In the above, the parentheses suround itemNumbers+count because the value of count is added to the address stored in itemNumbers in order to achieve the same as itemNumbers[count]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.