Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> #include <iomanip> #include <string> usin

ID: 3627508 • Letter: #

Question

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

// Global
const int ARRAY_SIZE = 15;

// Struct

struct ProductInfo
{
int number; // Product number
string description; // Product Description
double price; // Product Price
};

// Function Prototype
void showMenu();
void clearScreen();
void pauseScreen();
void openProductFile(ifstream &productFile);
void readProductFile(ifstream& productFile, char a[], string & productCode, string & productDesc, string & productPrice);
void bubbleSort(ProductInfo products[], int size);
int binarySearch(ProductInfo products[], int size, int prodNum);



/******************************************************************
Function: Main
******************************************************************/
int main()
{
// Set Variables
int choice;
ifstream prodFile;
string product_code, product_desc, product_price;
char prd[50];
ProductInfo products;

// numeric formatting
cout << fixed << showpoint << setprecision(2);

do
{
// Display Function showMenu
showMenu();
cin >> choice;

// Validate the Menu Choice
// If user enters a number other than 1 or 2, display an error message
while (choice < 1 || choice > 2)
{
clearScreen();

cout << " ERROR: Your have entered an invalid choice. ";
cout << " Please Choose again: ";
cout << " 1. Search for a Product Number ";
cout << " 2. Quit ";
cout << " Enter your choice again: ";
cin >> choice;
}


//*************************************************
// If the user enter 1, continue with questions
//*************************************************
if (choice == 1)
{
// variables


// Clear Screen
clearScreen();

cout << " You are in... You chose 1 ";

//User enters information
//getUserData(rooms, price_per_gallon, total_sf, total);

// Calling Function openProductFile
openProductFile(prodFile);

// Calling Function readProductFile
readProductFile(prodFile, prd, product_code, product_desc, product_price);

//
//products = bubbleSort(ProductInfo, size);

//
//binarySearch(ProductInfo products, size, product_code);

// Pause Screen
pauseScreen();

}

}

// Quit the program
while (choice != 2);
{
return 0;
}
}




//******************************************************
// Function: clearScreen
// Description: it clears the screen
//******************************************************
void clearScreen()
{
system ("cls");
}

//*******************************************************************************
// Function pauseScreen: Pause the screen *
//*******************************************************************************

void pauseScreen()
{
cout << " ";
system ("pause");
}


//*****************************************************************
// Function showMenu: Display Menu
//*****************************************************************

void showMenu()
{
// Clear Screen
clearScreen();

// Menu

cout << " WELCOME! Product Number Lookup Screen ";
cout << " Please choose from the menu below. ";
cout << " PRODUCT NUMBER LOOKUP ";
cout << " ****************************** ";
cout << " 1. Search for a Product Number ";
cout << " 2. Quit ";
cout << " Enter your choice: ";
}



//******************************************************
// Function: openProductFile
// Description: Asks the user for the path of the Product
// File
//******************************************************
void openProductFile(ifstream &productFile)
{
string productInfile;

cout << " Please write the path where the Product File is located: ";
cin >> productInfile;

// Checking if the Student Answers File has opened. If not, an error message will show.
productFile.open(productInfile);
while (productFile.fail())
{
// Calling funtion Clear Screen
clearScreen();

cout << " *******ERROR******* ";
cout << " The path for the Product File you have entered ";
cout << " " << productInfile << " ";
cout << " is incorrect, the file did not open. Please try again! ";
cin >> productInfile;
cin.ignore();

productFile.open(productInfile);
cout << " ";

}
}

//******************************************************
// Function: readProductFile
// Description: Read Product File
//******************************************************
void readProductFile(ifstream& productFile, char a[], string & productCode, string & productDesc, string & productPrice)
{
productFile.ignore(50, ' ');
productFile >> productCode;
productFile.ignore(7, ',');
//productFile >> productDesc;
getline (productFile, productDesc);
productFile.ignore(100, ',');
getline (productFile, productPrice);

for(int count=0; count < ARRAY_SIZE; count++)
{

productFile >> productCode[count];
productFile >> productDesc[count];
productFile >> productPrice[count];
}

productFile.close();

// Display
//clearScreen();

for (int count = 0; count < ARRAY_SIZE; count++)
{
cout << "RECORD NUMBER: " << count << " ";
cout << "Product Number: " << productCode[count] << " ";
cout << "Product Desc: " << productDesc[count] << " ";
cout << "Product Price: " << productPrice[count] << " ";
}
cout << endl;
}



/******************************************************************
Function: bubbleSort
This function performs an ascending order bubble sort on the
products array based on product number.
******************************************************************/

void bubbleSort(ProductInfo products[], int size)
{
bool swap;
ProductInfo temp;

do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (products[count].number > products[count + 1].number)
{
temp = products[count];
products[count] = products[count + 1];
products[count + 1] = temp;
swap = true;
}
}
} while (swap);
}


/******************************************************************
Function: binarySearch
This function performs a binary search on the products array.
It searches the products array for the element whose product number
is equal to "prodNum". If the element is found, its array subscript is
returned. Otherwise, -1 is returned indicating the array doesn't
contain an element with that product number.
*****************************************************************************/


int binarySearch(ProductInfo products[], int size, int prodNum)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of element with product number = prodNum
bool found = false; // Flag

while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (products[middle].number == prodNum) // If prodNum is found at mid
{
found = true;
position = middle;
}
else if (products[middle].number > prodNum) // If prodNum is in lower half
last = middle - 1;
else
first = middle + 1; // If prodNum is in upper half
}
return position;
}

This is the File that needs to be read:

Product Number,Description,Price
1405327,Samsung - 40 in. Class / 1080p / 60Hz / LCD HDTV,579.99
1414158,Samsung - 46 in. Class / 1080p / 120Hz / LCD HDTV,797.99
9763694,Sony - BRAVIA 46 in. Class / 1080p / 60Hz / LCD HDTV,763.99
9761174,Sony - BRAVIA 40 in. Class / 1080p / 60Hz / LCD HDTV,599.99
9742442,Sharp - AQUOS / 40 in. Class / 1080p / 120Hz / LCD HDTV,799.99
9783676,Panasonic - VIERA / 42 in. Class / 1080p / 60Hz / LCD HDTV,599.99
9933082,LG - 47 in. Class / 1080p / 60Hz / LCD HDTV - Glossy Black,999.99
9693695,Dynex - 40 in. Class / 1080p / 60Hz / LCD HDTV,499.99
1284588,LG - 42 in. Class / 1080p / 60Hz / LCD HDTV,579.99
9791077,LG - 42 in. Class / 1080p / 120Hz / LCD HDTV,899.99
9605116,Dynex - 46 in. Class / 1080p / 60Hz / LCD HDTV,599.99
9783524,Samsung - 40 in. Class / 1080p / 120Hz / LCD HDTV,899.99
9835115,Toshiba - 46 in. Class / 1080p / 120Hz / LCD HDTV,799.99
9784784,Insignia - 550 Series / 46 in. Class / 1080p / 120Hz / LCD HDTV,787.99
1390554,Philips - 46 in. Class / 1080p / 240Hz / LCD HDTV,799.99

Please help! Thanks!

Explanation / Answer

#include <iostream>

#include <fstream>

#include <iomanip>

#include <string>

 

using namespace std;

 

// Global

const int ARRAY_SIZE = 15;

 

// Struct

struct ProductInfo

{

long number; // Product number

string description; // Product Description

double price; // Product Price

};

 

// Function Prototype

void showMenu();

void clearScreen();

void pauseScreen();

void openProductFile(ifstream &productFile);

void readProductFile(ifstream& productFile, char a[], string & productCode, string & productDesc, double & productPrice,ProductInfo products[]);

void bubbleSort(ProductInfo products[], int size);

int binarySearch(ProductInfo products[], int size, long prodNum);

 

int main()

{

// Set Variables

int choice;

ifstream prodFile;

string product_code, product_desc;

double product_price;

char prd[50];

 

ProductInfo products[15];

 

// numeric formatting

cout << fixed << showpoint << setprecision(2);

 

do

{

// Display Function showMenu

showMenu();

cin >> choice;

 

// Validate the Menu Choice

// If user enters a number other than 1 or 2, display an error message

while (choice < 1 || choice > 2)

{

clearScreen();

 

cout << " ERROR: Your have entered an invalid choice. ";

cout << " Please Choose again: ";

cout << " 1. Search for a Product Number ";

cout << " 2. Quit ";

cout << " Enter your choice again: ";

cin >> choice;

}

 

if (choice == 1)

{

 

// Clear Screen

clearScreen();

 

cout << " You are in... You chose 1 ";

 

//User enters information

//getUserData(rooms, price_per_gallon, total_sf, total);

 

// Calling Function openProductFile

openProductFile(prodFile);

 

// Calling Function readProductFile

readProductFile(prodFile, prd, product_code, product_desc, product_price,products);

 

bubbleSort(products, ARRAY_SIZE);

 

clearScreen();

long product_ID;

cout<<"Enter product number: ";

cin>>product_ID;

int position=binarySearch(products, ARRAY_SIZE, product_ID);

if(position!=-1)

{                     

                        cout << "Product Number: " << products[position].number << " ";

                        cout << "Product Desc: " << products[position].description << " ";

                        cout << "Product Price: " << products[position].price << " ";

}

else

            cout<<"Product is not found";

 

// Pause Screen

pauseScreen();

}

}

 

// Quit the program

while (choice != 2);

{

return 0;

}

}

 

void clearScreen()

{

system ("cls");

}

 

void pauseScreen()

{

cout << " ";

system ("pause");

}

 

void showMenu()

{

// Clear Screen

clearScreen();

 

// Menu

 

cout << " WELCOME! Product Number Lookup Screen ";

cout << " Please choose from the menu below. ";

cout << " PRODUCT NUMBER LOOKUP ";

cout << " ****************************** ";

cout << " 1. Search for a Product Number ";

cout << " 2. Quit ";

cout << " Enter your choice: ";

}

 

void openProductFile(ifstream &productFile)

{

string productInfile;

 

cout << " Please write the path where the Product File is located: ";

cin >> productInfile;

 

// Checking if the Student Answers File has opened. If not, an error message will show.

productFile.open(productInfile);

while (productFile.fail())

{

// Calling funtion Clear Screen

clearScreen();

 

cout << " *******ERROR******* ";

cout << " The path for the Product File you have entered ";

cout << " " << productInfile << " ";

cout << " is incorrect, the file did not open. Please try again! ";

cin >> productInfile;

cin.ignore();

 

productFile.open(productInfile);

cout << " ";

 

}

}

 

void readProductFile(ifstream& productFile, char a[], string & productCode, string & productDesc, double & productPrice,ProductInfo products[])

{

            int index=0;

            while(!productFile.eof())

            {                     

                        getline (productFile, productCode,',');

                        getline (productFile, productDesc,',');

                        productFile >> productPrice;

                        products[index].number=atol(productCode.c_str());//converts string to long

                        products[index].description=productDesc;

                        products[index].price=productPrice;

                        index++;

            }

           

            productFile.close();

           

            //Display

            clearScreen();

           

            for (int count = 0; count < ARRAY_SIZE; count++)

            {

                        cout << "RECORD NUMBER: " << count+1 << " ";

                        cout << "Product Number: " << products[count].number << " ";

                        cout << "Product Desc: " << products[count].description << " ";

                        cout << "Product Price: " << products[count].price << " ";

            }

            cout << endl;

}

 

void bubbleSort(ProductInfo products[], int size)

{

bool swap;

ProductInfo temp;

 

do

{

swap = false;

for (int count = 0; count < (size - 1); count++)

{

if (products[count].number > products[count + 1].number)

{

temp = products[count];

products[count] = products[count + 1];

products[count + 1] = temp;

swap = true;

}

}

} while (swap);

}

 

int binarySearch(ProductInfo products[], int size, long prodNum)

{

int first = 0, // First array element

last = size - 1, // Last array element

middle, // Mid point of search

position = -1; // Position of element with product number = prodNum

bool found = false; // Flag

 

while (!found && first <= last)

{

middle = (first + last) / 2; // Calculate mid point

if (products[middle].number == prodNum) // If prodNum is found at mid

{

found = true;

position = middle;

}

else if (products[middle].number > prodNum) // If prodNum is in lower half

last = middle - 1;

else

first = middle + 1; // If prodNum is in upper half

}

return position;

}