Hello everyone im not very good with using pointers, maybe just need more explanation of using them/how i would change my code.
Well i have to update this program i have to read in item number cost and qusantity from a file instead of getting it from the user. the first line of the file is 3 which is the number of items that would be in the array.
next i have to convert my arrays over to pointer notation. What would be the best way to do this with my program/ how would i do it?
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//global constants
const int NUM_ITEMS = 10; //number of items in the arrays
//function prototypes
void intro();
void readData(int itemNumbers[], double itemPrices[], int itemQuantity[], const int arraySize);
void sortData(int itemNumbers[], double itemPrices[], int itemQuantity[], const int arraySize);
void printData(const int itemNumbers[], const double itemPrices[], const int itemQuantity[], const int arraySize);
int searchData(const int itemNumbers[], int searchValue, const int arraySize);
void searchLoop(const int itemNumbers[], const double itemPrices[], const int itemQuantity[]);
/*
* Main
*/
int main() {
//declare variables
int itemNumbers[NUM_ITEMS]; //holds the item numbers
double itemPrices[NUM_ITEMS]; //holds the item prices
int itemQuantity[NUM_ITEMS]; //holds the item quantity
//Welcome message to user
intro();
//get user input
//message to user
cout << "Enter an item number, quantity, and price for " << NUM_ITEMS
<< " items." << endl << endl;
//read in the data from the user
readData(itemNumbers, itemPrices, itemQuantity, NUM_ITEMS);
//sort the data
sortData(itemNumbers, itemPrices, itemQuantity, NUM_ITEMS);
//print the data in table format
printData(itemNumbers, itemPrices, itemQuantity, NUM_ITEMS);
//search for an item number and display its cost and quantity
searchLoop(itemNumbers, itemPrices, itemQuantity);
return 0;
}
/*
* Function: intro
* Description: Displays an intro message to the user
*/
void intro() {
//Program introduction
// \n and endl wanted to practice with both.
cout << "*****************************************" << endl
<< "** Welcome to Program 1 ** \n"
<< "** Created by: Brandon ** \n"
<< "** Sorting and Searching Arrays **" << endl
<< "*****************************************" << endl << endl;
}
/*
* Function: readData
* Description: Retrieves 10 item numbers, their price and quantity
* from the user and stores their answers in 3 parallel arrays.
*/
void readData(int itemNumbers[], double itemPrices[], int itemQuantity[], const int arraySize) {
//define input stream object
ifstream inputFile;
//open the file
inputFile.open("inventoryData.txt");
//close the file
inputFile.close();
}
/*
* Function: sortData
* Description: Sorts 3 arrays in parallel, in ascending order.
*/
void sortData(int itemNumbers[], double itemPrices[], int itemQuantity[], const 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 = itemQuantity[count];
pricesTemp = itemPrices[count];
itemNumbers[count] = itemNumbers[count + 1];
itemPrices[count] = itemPrices[count + 1];
itemQuantity[count] = itemQuantity[count + 1];
itemNumbers[count + 1] = numbersTemp;
itemPrices[count + 1] = pricesTemp;
itemQuantity[count + 1] = quantityTemp;
swap = true;
}
}
} while (swap);
}
/*
* Function: printData
* Description: prints the values of 3 arrays in table format
*/
void printData(const int itemNumbers[], const double itemPrices[], const int itemQuantity[], const 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) << itemQuantity[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(const int itemNumbers[], int searchValue, const int arraySize) {
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(const int itemNumbers[], const double itemPrices[], const int itemQuantity[]) {
// 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, NUM_ITEMS);
//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) << itemQuantity[result]
<< setw(11) << "|" << endl
//end of each row include a line
<< "------------------------------------------------" << endl;
}
} while (searchValue >= 0);
}
the text file i need to get data from is this
inventoryData.txt
3
39284 5.75 12
18372 4.50 23
27649 12.99 81