Alphabetically Sort Structure Array - c++

so I have a structure array with various variables.
struct Data
{
char name[11];
int ID;
int life;
int date;
float avgWindSpeed;
float avgRainFall;
int tornadoes;
int stormCategory;
};
I am looking to sort all the information based on char name[11]. The data that is stored in struct Data comes from a file named storms.txt. Currently, I have everything to make up perfectly but still struggling with sorting everything alphabetically. Also, I named my ofstream outfile and counter contains the total number of storms inside the file.
My current code is:
//--------------------------------------------------------------------------------------------------------------------------------------------------
//Start of Hurricane Level 1
int totalLevel1 = 0; //Will hold the number of storms that are level 1
//This is just setting the top part of the chart
outfile << setw(70) << "Hurricane Level 1" << endl << endl;
outfile << "Name" << setw(10) << "ID" << setw(20) << " Life " << setw(20) << " Average " << setw(20) << " Average " << setw(20) << "Tornadoes" << setw(19) << " Date " << endl;
outfile << " " << setw(10) << " " << setw(20) << "in days" << setw(20) << "wind speed" << setw(20) << "rain fall" << setw(20) << " spawned " << setw(20) << " " << endl;
outfile << endl << endl;
float avgLifeSpan, avgRainFall, avgTornadoes, avgWindSpeed, life = 0, rain= 0, tornado= 0, wind= 0;
//Starting to process the information and printing it in its proper location
for(int i = 0; i < counter; i++)
if(hurricanes[i].stormCategory == 1)
{
totalLevel1++;
life = life + hurricanes[i].life;
rain = rain + hurricanes[i].avgRainFall;
tornado = tornado + hurricanes[i].tornadoes;
wind = wind + hurricanes[i].avgWindSpeed;
outfile << hurricanes[i].name << setw(5) << hurricanes[i].ID << setw(15) << hurricanes[i].life << setw(21) << hurricanes[i].avgWindSpeed
<< setw(20) << hurricanes[i].avgRainFall << setw(19) << hurricanes[i].tornadoes << setw(21) << hurricanes[i].date << endl;
}
//Printing the extra information for HURRICANE LEVEL 1
outfile << endl << endl << "Total number of Level 1 hurricanes is " << totalLevel1 << "." << endl;
outfile << "Average Life span in days of Level 1 hurricanes is " << life / float(totalLevel1) << "." << endl;
outfile << "Average rain fall for Level 1 hurricanes is " << rain / float(totalLevel1) << "." << endl;
outfile << "Average tornadoes spawned for Level 1 hurricanes is " << tornado / float(totalLevel1) << "." << endl;
outfile << "Average wind speed for Level 1 hurricanes is " << wind / float(totalLevel1) << "." << endl;
outfile << endl << endl;
//End of the Hurricane Level 1
//--------------------------------------------------------------------------------------------------------------------------------------------------
How to do I make so that everything that gets printed out on the output file is sorted alphabetically with its correct information? Could someone give me some suggestions?

It depends on how you've defined hurricanes. If it is a C array, then you'll be wanting something like this:
std::sort(hurricanes, hurricanes + counter,
[](const Data& a, const Data& b) { return std::strcmp(a.name, b.name) < 0; });
If however it is a std::vector or std::array, then...
std::sort(hurricanes.begin(), hurricanes.end(),
[](const Data& a, const Data& b) { return std::strcmp(a.name, b.name) < 0; });

I don't see what type of container you use ( I suggest to use std::vector), but you may do couple of nice things to make you life easier in this code.
First overload operators in Data structure like:
ostream& operator<<(ostream& os, const Data& a_data) for easily sending your struct to ostream (without need of encapsulating all it's members).
bool T::operator <(const T2 &b) const; , bool T::operator >(const T2 &b) const; : for comparison in standard algorithms.
Take look at reference : link You will write it only once but could use it in every part of program.
Second suggestion is to use std::sort algorithm to sort your data structure.
std::sort(hurricanes.begin(), hurricanes.end(), greater<Data>());
Syntax of your code will be much easier to understand and read for you wen you come back to project in 6 (or so) months.

Related

How do I find the sum using a loop function?

I was able to loop a file that gave me the miles driven, gallons used, and gasoline cost at a certain day successfully. Now I'm trying to figure out how to get the sum of miles driven, gallons used, and gasoline cost by using loops
int main()
{
ifstream inputFile;
int x = 1;
int milesDriven = 0;
double gallonsUsed = 0,
gasolineCost = 0;
int truckNumber,
numberOfTrips,
sumMilesDriven = 0;
double sumGallonsUsed = 0,
sumGasolineCost = 0;
int avgMilesDriven;
double avgGallonsUsed,
avgGasolineCost;
/* Display Truck Information
Get Number of Trips
Get Truck Information
Process Each Trip
Display Averages
*/
inputFile.open("100.txt");
//Display Truck Information
cout << " " << setw(35) << "Red-Rig Trucking" << endl << endl;
cout << " " << setw(40) << "Summary of Truck Operations" << endl << endl;
inputFile >> truckNumber;
cout << "Truck: " << truckNumber << endl << endl;
inputFile.close( );
inputFile.open("truck.txt");
//Get Number of Trips
inputFile >> numberOfTrips;
//Get Truck Information
cout << "Day" << " " <<setw(16) << "Miles" << " " << setw(16) << "Gallons"
<< " " << setw(16) << "Gasoline" << endl << setw(20) << "Driven" << " "
<< setw(16) << "Used" << " " << setw(16) << "Cost" << endl << endl;
while(!inputFile.eof()){
inputFile >> milesDriven >> gallonsUsed >> gasolineCost;
cout << x << " " << setw(17) << milesDriven << " " << setw(17)
<< fixed << setprecision(2) << gallonsUsed << " " << setw(12) << fixed
<< setprecision << gasolineCost << endl ;
x++;
}
//Process Each Trip
/*while(inputFile)
{ sumMilesDriven = sumMilesDriven + milesDriven;
inputFile >> milesDriven;
}*/
for (; milesDriven--;)
sumMilesDriven += milesDriven;
cout << endl << "Sum" << " " << setw(15) << sumMilesDriven ;
for (;gallonsUsed;)
sumGallonsUsed += gallonsUsed;
cout << " " << setw(17) << sumGallonsUsed;
for (;gasolineCost--;)
sumGasolineCost += gasolineCost;
inputFile.close( );
return 0;
}
I've gotten this far and I can't figure out what's wrong. I've taken out the milesDriven >=10 from the for loop parenthesis. When the code runs I get an incorrect sum amount. The sum is either too big or too small.
Your code:
for (sumGasolineCost += gasolineCost; gasolineCost >= 1; gasolineCost--);{
cout << " " << setw(17) << sumGasolineCost;
}
My first advice is, do not write confusing code. Whenever you're programming, you have better things to do than tie your shoelaces together. I think you know the above is equivalent to:
for (sumGasolineCost += gasolineCost; gasolineCost >= 1; gasolineCost--);
cout << " " << setw(17) << sumGasolineCost;
That lets us discuss the loop itself. Go back and consult your textbook. The for loop has three components:
for( init ; test ; incr )
init is executed once, usually to initialize what will be tested
test is executed on each iteration, including the first, to determine if the loop body will be executed
incr is executed after the loop body, usually to update the tested value
In your case, sumGasolineCost += gasolineCost is in init. It is executed once. It should be in the loop body. There are other errors, too. I can't be much more specific because you don't indicate where the array or input is that you're looping over.
Once you get your loop doing what it should, you might find the standard std::accumulate function interesting to compare to.

Why won't my print function won't display the elements in either sub node of my linked list?

I have been at this for days now and I just can't seem to find out why my last two things won't print. The code is quite long so I won't post it all , but if you need it I am willing to provide the entire source.
Basically I call the print function after adding 1 element to each of the listed elements. It will print them all EXCEPT the last two Spouse and Child. Those two are the most complicated as they are their own lists as well. When I tested the for loop for child it showed that no matter how many children I add to the Vector it reads the size as 0. Why is this?
void AddressNode::PrintFull()
{
cout << setfill(' ') << endl;
cout << setw(15) << "UID " << "ID" << setfill('0') << setw(3) << id_ << setfill(' ')<< endl;
cout << setw(15) << "NAME:" << firstName_ << " " << lastName_ << endl;
cout << setw(15) << "Address1:" << address_ << endl;
cout << setw(15) << "City:" << city_<< " " << endl;
cout << setw(15) << "State:" << state_<< " " << endl;
cout << setw(15) << "Zip:" << zip_<< " " << endl;
cout << setw(15) << "Date_Birth:" << dob_<< " " << endl;
cout << setw(15) << "Date_Death:" << dod_<< " " << endl;
cout << setw(15) << "Date_Wedding:" << dow_<< " " << endl;
cout << setw(15) << "Spouse:" << (spouse_ ? spouse_->GetFirstName() : "") << " " << (spouse_ ? spouse_-> GetLastName() : "") << endl;
for(unsigned int i = 0; i < children_.size(); i++)
{
cout << setw(15) << "Child: " << i << ": " << children_[i]->GetFirstName()<< " " << children_[i]->GetLastName()<< endl;
}
}
private:
std::string firstName_;
std::string lastName_;
std::string city_ ;
std::string state_ ;
std::string zip_ ;
std::string dob_ ;
std::string dow_;
std::string dod_;
std::string address_;
std::string spouseTempString;
std::vector<AddressNode*> children_;
AddressNode* spouse_;
unsigned int id_;
void AddressNode::AddChild(AddressNode& child)
{
vector<AddressNode*>::iterator iter;
if((iter = find(children_.begin(), children_.end(), &child)) != children_.end())
return;
children_.push_back(&child);
if (spouse_)
spouse_->AddChild(child);
}
public:
AddressNode(const std::string& firstName, const std::string& lastName, int id)
: children_(), id_(id)
{
firstName_= "";
firstName_+= firstName;
lastName_="";
lastName_+= lastName;
}
There's not enough code here to tell, but passing an object by reference and then storing its address is always fishy.
If a stack object gets passed to that function you'll get all kind of weird results.
Since your error only occur on pointer objects, I'm even more inclined to think you've got memory management issues somewhere.
If you really want to store a pointer, pass the pointer in the first place, or pass a const reference and store a copy?

C++ setup columns using cout

So i'm just starting to learn c++ and i'm curious if its a way to formate your output with cout so it will look nicely and structured in columns
for example.
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << "Name" << setw(30) << "Height[m]" << endl;
cout << fname + " " + lname << right << setw(20) << setprecision(2) << fixed << height << endl;
cout << fname2 + " " + lname2 << right << setw(20) << setprecision(2) << fixed << height2 << endl
The output looks like this:
Name Height[m]
testname 123 1.60
short 123 1.80
I want it to look like this:
Name Height[m]
testname 123 1.60
short 123 1.80
The problem i'm trying to solve is that i want to place height at a specific position from name but depending what length of name i take the value of height either gets far away to the right or will be very close to the left. Is there a way to fix this?
First of all, with an output stream like std::cout, you cannot travel back in time and modify output which was already performed. That makes sense -- just imagine std::cout wrote into a file because you launched your program with program.exe > test.txt, and test.txt was on a USB drive which has been disconnected in the meanwhile...
So you have to get it right immediately.
Basically, there are two ways to do so.
You can assume that no entry in the first column will ever be wider than a certain number of characters, which is what you have attempted. The problem is that your setw is at the wrong position and that right should be left. A stream manipulator must be placed before the elements which should be affected. And since you want left-aligned columns, you need left:
cout << left << setw(20) << "Name" << "Height[m]" << endl;
cout << left << setw(20) << fname + " " + lname << setprecision(2) << fixed << height << endl;
cout << left << setw(20) << fname2 + " " + lname2 << setprecision(2) << fixed << height2 << endl;
But this solution is not very general. What if you'll have a name with 21 characters? Or with 30 characters? Or 100 characters? What you really want is a solution in which the column is automatically set only as wide as necessary.
The only way to do this is to collect all entries before printing them, finding the longest one, setting the column width accordingly and only then print everything.
Here is one possible implementation of this idea:
std::vector<std::string> const first_column_entries
{
"Name",
fname + " " + lname,
fname2 + " " + lname2
};
auto const width_of_longest_entry = std::max_element(std::begin(first_column_entries), std::end(first_column_entries),
[](std::string const& lhs, std::string const& rhs)
{
return lhs.size() < rhs.size();
}
)->size();
// some margin:
auto const column_width = width_of_longest_entry + 3;
std::cout << std::left << std::setw(column_width) << "Name" << "Height[m]" << "\n";
std::cout << std::left << std::setw(column_width) << fname + " " + lname << std::setprecision(2) << std::fixed << height << "\n";
std::cout << std::left << std::setw(column_width) << fname2 + " " + lname2 << std::setprecision(2) << std::fixed << height2 << "\n";
The next step of evolution would be generalising the std::vector into a self-written class called Table and iterating that Table's rows in a loop in order to print the entries.
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << left << setw(30) << "Name" << left << "Height[m]" << endl;
cout << left << setw(30) << fname + " " + lname << right << setw(6) << setprecision(2) << fixed << height << endl;
cout << left << setw(30) << fname2 + " " + lname2 << right << setw(6) << setprecision(2) << fixed << height2 << endl;

Dynamic alignment issue

When I try to list details of all items, each on a different line
with line numbering, there is alignment issue on it. I want instantly put the close bracket after the line numbering. Thanks.
cout << left
<< setw(20) << " Item Code"
<< setw(50) << "Description"
<< setw(20) << "Quantity on hand"
<< setw(20) << "Cost price"
<< setw(20) << "Selling price(RM)"
<< setw(20) << "Status"
<< setw(20) << "Discount(%)" << endl;
for (int i = 0; i <= 100; i++)//counter - 1; i++)
{
cout << left
<< setw(2) << i + 1 << ")"
<< setw(20) << item[i].getItemCode()
<< setw(50) << item[i].getDescription()
<< setw(20) << item[i].getQuantity()
<< setw(20) << item[i].getCostPrice()
<< setw(20) << item[i].getSellPrice()
<< setw(20) << item[i].getStatus()
<< setw(20) << item[i].getDiscount() << endl;
}
The only way of doing this, as far as I can see, is to walk through the list and find out "how long does this get" for each of the columns, and track what the largest is for each of the columns. Then use those values in the column width.
Strings are easy to find the length of, since they have a length as such. Numbers are harder - basically, you have to either take the approach of dividing it by ten down until it's zero (this means the integer part of floating point numbers - presumably for something like this, you have a fixed number of decimals or use "integeer to represent prices in cents" or some such). You may be able to use the std::tostring to produce as string that has a length too. Or you can use stringstream to output to a string - either individual items, or the whole lot and then count the number of characters between some separator character [that doesn't occur in the normal output, or things go wrong pretty easily!]
Example, using a simple struct:
struct Data
{
int x;
string y;
float z;
}
...
Data d[10];
int maxLen[3] = { 0 };
... // code fills in data with stuff.
for(int i = 0; i < 10; i++)
{
stringstream ss;
ss << left << d[i].x << " " << d[i].y << " " << fixed << setprecision(2) << d[i].z;
// Number of elements = 3.
for(int j = 0; j < 3; j++)
{
string s;
ss >> s;
if (s.length() > maxLen[j])
maxLen[j] = s.legnth;
}
}
...
for(int i = 0; i < 10; i++)
{
cout << left << setw(3) i << ": "
<< setw(maxLen[0]+1) << d[i].x
<< setw(maxLen[1]+1) << d[i].y
<< setw(maxLen[2]+1) << fixed << setprecision(2) << d[i].z << endl;
}

Program won't Print a string array C++

I have been working on this program for awhile, but it refuses to cooperate on this last little stretch. The point of the program is to sift a data file into three arrays, sort the arrays, then print them out into a table. The problem I'm having appears to be with the table. The program is divided into four functions, and when I attempt to debug, it won't show the productName array in the function.
The malfunctioning segment of code looks like this:
void printReport (string productName[], int numberinStock[], float price[], int number_of_products)
{
float totalPrice;
cout << setw(18) << " " << "Friendly Grocer Store Inventory" << setw(17) << " " << endl;
cout << setw(18) << "Inventory Item" << setw(16) << "Number in Stock" << setw(16) << "Unit Price" << setw(16) << "Total Sales" << endl;
for (int count=0; count <number_of_products-1; count++)
{
cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << setw(16) << std::setprecision(2) << price[count] << setw(16) << std::fixed << std::setprecision(2) << price[count]*numberinStock[count] << endl;
}
cout << "Total Price: " << totalPrice;
}
It will print everything else, but not the productName.
Some debugging statements outside of the for loop like cout << productName[1] will print out the proper productName but it's completely blank on the actual report.
After some debugging it seems like after printing the productName in the for loop every item after that overwrites the product name.
For example just leaving cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << endl;
will produce
" 3s"
" 10h"
" 2a"
The product names there are Mangoes, Sandwich, and pizza.
I'm at a loss. Where did I mess up?
You might have screwed up passing the data into the function. If you set up test arrays in the function it should be printing correctly.
To pass arrays in C++ use the arrayname.
eg
int main ()
{
string productName[] = {"mango"};
...
printReport(productName, numofProduct);
return 0;
}