/*
*******************************************************************************
*Title: *
* *
*Name: *
* *
*Assignment: *
* *
*File: *
* *
*Description: *
* This program accepts information from a file, and accepts user input *
* to process and display the information taken from that file. *
*Source: *
* *
*******************************************************************************
*/
#include <iostream> //Required for stream i/o
#include <iomanip> //Required for stream formatting
#include <string> //Required for string functions
#include <fstream> //required for file streams
usingnamespace std; //Required CS161 namespace
/*
+-----------------------------------------------------------------------------+
|Name: |
| main |
|Parameters: |
| int argc: Count of the command line arguments passed by |
| the operating system. |
| char **argv: Array of strings. argv[0] is the program name, |
| argv[1] is the first command line argument. |
|Return: |
| int s: Integer indicating exit status. 0 = normal exit.|
|Description: |
| The "main" function is the entry point for C++ programs. |
| Every C++ program must have a "main" function, and this is |
| where execution always starts. |
+-----------------------------------------------------------------------------+
*/
int main(constint argc, constchar **argv)
{
char userHoldCharacter; //Character to hold user input
char menuOption = ' '; //user main menu entry
int counter = 0; //used for array processing
int idx = 0; //used for array processing
int index = 0; //used for array processing
int listSize = 0; //used for array processing
int searchNumber = 0; //user input for search query
float markup; //used for calculating
//product markup percent
struct productInformation //structure variable for
//storing product information
{
int IDNumber;
float wholeSale;
float retail;
float markup;
};
productInformation myArray[100]; //product info storage array
for(idx = 0; idx < 101; idx++) //initializing array
{
myArray[idx].IDNumber = 0;
myArray[idx].wholeSale = 0.0;
myArray[idx].retail = 0.0;
myArray[idx].markup = 0.0;
}
ifstream inFile; //initialize input file stream
inFile.open(argv[1]); //open file:
//file name passed as parameter
if(inFile == false) //error for incorrect file name
{
cout << "Unable to Open File" << endl;
}
if(inFile != false)
{
for(counter = 0; counter < 100; counter++)
{
inFile >> myArray[counter].IDNumber
>> myArray[counter].wholeSale
>> myArray[counter].retail;
if (counter <= myArray[counter].IDNumber)
{
listSize = counter;
}
}
cout << "Please enter one of the following: \n"
<< "L - list all products \n"
<< "F - Find a product by ID \n"
<< "Q - Quit the program" << endl;
cout << "Enter Option: ";
cin >> menuOption;
if(menuOption == 'L' || menuOption == 'l')
{
for(index = 0; index <= listSize; index++)
{
cout << setprecision(2)
<< "ID: " << setw(1) << myArray[index].IDNumber
<< setw(10) << "Cost: " << "$" << (myArray[index].wholeSale)
<< setw(10) << "Price: " << "$"
<< myArray[index].retail << " ";
markup = ((myArray[index].retail / myArray[index].wholeSale) - 1.00) * 100;
cout << fixed << setprecision(2) << setw(10)
<< "Markup: " << markup << "%" << "\n";
}
}
elseif(menuOption == 'F' || menuOption == 'f')
{
cout << "Please enter the product ID number search query: ";
cin >> searchNumber;
for(index = 0; index <= listSize; index++)
{
if(myArray[index].IDNumber == searchNumber)
{
cout << setprecision(2)
<< "ID: " << setw(1) << myArray[index].IDNumber
<< setw(10) << "Cost: " << "$" << (myArray[index].wholeSale)
<< setw(10) << "Price: " << "$"
<< myArray[index].retail << " ";
markup = ((myArray[index].retail / myArray[index].wholeSale)
- 1.00) * 100;
cout << fixed << setprecision(2) << setw(10)
<< "Markup: " << markup << "%" << "\n";
break;
}
}
}
elseif(menuOption == 'Q' || menuOption == 'q')
{
return 0;
}
else
{
cout << "Incorrect Menu Option" << endl;
}
}
inFile.clear();
inFile.close();
/*
The following code is needed in windowed environments to prevent the
closure of the execution window before the user has a chance to see
the output display.
*/
cout << "Press any key followed by <enter> to exit the program" << endl;
cin >> userHoldCharacter;
cout << "[normal end]" << endl;
return 0;
}
I need to include another option A that lists the product information in alphabetical order. Since the program doesn't deal with very high numbers, a bubble sort seem to be the best option but I haven't found a way to implement it w/o screwing something else up in the prg.
Can you please post it correctly using [c0de] your code goes here [/c0de] tags. Replace 0 with o so it spells code. This will preserve your indentation and make it easier to read.
Try to use a function that passes as a reference the array that you read and sorts it.
Here: http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
you can find a sample of bubble shorting, just reformat it for your needs.
Also the line:
for(idx = 0; idx < 101; idx++)
you should go until 100, not 101 because you have 100 positions array.
I don't have the file to test it but I think that this code will do the shorting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//------shorting------
int i, j, flag = 1; // set flag to 1 to begin initial pass
productInformation temp; // holding variable
int arrayLength = 100;
for(i = 1; (i <= arrayLength) && flag; i++){
flag = 0;
for (j=0; j < (arrayLength -1); j++){
if (myArray[j+1].IDNumber > myArray[j].IDNumber){ // ascending order simply changes to <
temp = myArray[j]; // swap elements
myArray[j] = myArray[j+1];
myArray[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
//----end shorting----
No,no.
temp is just a temporary variable made just to change the order of myArray. When you change position of two variables you need a temporary var that will handle the value of one. myArray is still your main array with your information...