CODE: #include <iostream> #include <fstream> #include <iomanip> #include <string
ID: 3627582 • Letter: C
Question
CODE:#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, string & productCode, string & productDesc, double & productPrice, ProductInfo products[]);
void bubbleSort(ProductInfo products[], int size);
int binarySearch(ProductInfo products[], int size, long prodNum);
/******************************************************************
Function: Main
******************************************************************/
int main()
{
// Set Variables
int choice;
ifstream prodFile;
string product_code, product_desc;
double product_price;
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 the user enter 1, continue with questions
//*************************************************
if (choice == 1)
{
// variables
// Clear Screen
clearScreen();
// Calling Function openProductFile
openProductFile(prodFile);
// Calling Function readProductFile
readProductFile(prodFile, product_code, product_desc, product_price, products);
// Calling Function bubbleSort
bubbleSort(products, ARRAY_SIZE);
// Clear Screen
clearScreen();
long product_ID;
cout<<" Enter Product Number: ";
cin>>product_ID;
int position = binarySearch(products, ARRAY_SIZE, product_ID);
if(position!=-1)
{
cout << " PRODUCT INFORMATION ";
cout << " Product Number: " << products[position].number << " ";
cout << " Product Description: " << products[position].description << " ";
cout << " Product Price: " << products[position].price << " ";
}
else
cout<<" Product is NOT found";
// Pause Screen
pauseScreen();
}
}
// Quit the program if number 2 is chosen
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, 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;
pauseScreen();
*/
}
/******************************************************************
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, 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;
}
-------------------------------------------------------------------------------------------
FILE THAT NEEDS TO BE OPENED:
-----------------------------------------------
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
------------------------------------------------------------------------------------------------
ERROR I GET:
'PRN9.exe': Loaded 'C:Documents and SettingsMaria LeonDesktopPRN9DebugPRN9.exe', Symbols loaded.
'PRN9.exe': Loaded 'C:WINDOWSsystem32 tdll.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32kernel32.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32msvcp100d.dll', Symbols loaded.
'PRN9.exe': Loaded 'C:WINDOWSsystem32msvcr100d.dll', Symbols loaded.
'PRN9.exe': Loaded 'C:Program FilesCommon FileslogishrdLVMVFMLVPrcInj.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32user32.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32gdi32.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32imm32.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32dvapi32.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32 pcrt4.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32secur32.dll', Cannot find or open the PDB file
The thread 'Win32 Thread' (0xb6c) has exited with code 8388608 (0x800000).
'PRN9.exe': Loaded 'C:WINDOWSsystem32pphelp.dll', Cannot find or open the PDB file
'PRN9.exe': Loaded 'C:WINDOWSsystem32ersion.dll', Cannot find or open the PDB file
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
First-chance exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
Unhandled exception at 0x1027cac0 (msvcr100d.dll) in PRN9.exe: 0xC0000005: Access violation writing location 0xcccccccc.
The program '[5392] PRN9.exe: Native' has exited with code 0 (0x0).
Explanation / Answer
DEAR FRIEND i solved the error the index of the array was exceeding the allocated memory and this will get he program into a memory may have some system commands which cuased the error PLEASE RATE #include #include #include #include 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, string & productCode, string & productDesc, double & productPrice, ProductInfo products[]); void bubbleSort(ProductInfo products[], int size); int binarySearch(ProductInfo products[], int size, long prodNum); /****************************************************************** Function: Main ******************************************************************/ int main() { // Set Variables int choice; ifstream prodFile; string product_code, product_desc; double product_price; ProductInfo products[15]; // numeric formatting coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.