ok, this may be too much to ask but I might as well. I was asked to design a program that could read a file and store it, then manipulate the data in that file accordingly: the user is given an option of Q,H,L,A, and F. Q quits (i got that), A lists all the product info in alphabetical order (I've got it to display in the order it was given, but not in alphabetical order), F finds a products info given its product ID (got that too). H will list whatever product has the highest value, which seemed simple enough but I couldn't get THAT to work either lol. And finally L lists everything in order, which I have but I can't seem to arrange it alphabetically for A.
Even though it's too late now, I'd like to know how to do this for future projects. The approach I took was using structures and arrays like so:
/*
*******************************************************************************
*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
using namespace 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(const int argc, const char **argv)
{
char userHoldCharacter;
char menuOption = ' ';
int counter = 0;
int idx = 0;
int index = 0;
int listSize = 0;
int searchNumber = 0;
float markup;
struct productInformation
{
int IDNumber;
float wholeSale;
float retail;
float markup;
/*
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;
Ok, so 'A' requires you to sort the data into alphabetical order, and 'H' requires you to search for the highest value.
Both are reasonably simple things to do.
The easy way to get the highest value is to loop through the array to find the index of the entry with the highest value, then output that entry when you have finished.
PseudoCode
Set Highest = 0;
Set Index = -1;
Loop through Array
If Current element value > Highest
Highest = Current element value
Index = Current Element Index
End Loop
Print Element (Index)
Sorting is a bit more complex, as there are a whole bunch of algorithms for sorting. Examples include
Bubble Sort - Simple but very slow for large numbers
Selection Sort, Insertion Sort - Both quicker for larger numbers of elements, but a little more complex to undestand and code.
QuickSort - Fast but more complex still to understand and code.
Try searching for any ony of the above (A Bubble sort will be fine for 100 elements) to learn more.
Yeah, a bubble sort seems to be the best option for sorting on this one.
I'm still having trouble w/ the highest value one. How exactly does the program make it's destinction between each each value? I tried it with a for loop and was unsuccessful.