So I need to find the highest and lowest for some numbers I am storing into struct

``````#include <iostream>
#include <string>

using namespace std;

struct WeatherStation {
string StationDesignation;
double Temperature;
} CA, FL, NY;

// EXAMPLE, I WILL ADD A LOOP FOR FL AND NY

int main()
{
string Command;
WeatherStation CA;
cout<<"Enter reported temperatures..."<<endl;
cout<<endl;
cout<<"Weather Station CA: ";
cin>>CA.Temperature;``````

I need to know how to find the lowest and highest from these 3

What field structure is determined?

I don't understand what you mean

Anyone know how to do this??? I NEED HELP!!

If you were using an array of structs, I would simply just tell you to sort the array based on the desired struct attribute, and simply pick off the struct at the begging and at the end of the array.

But since this is not the case, the best technique I can think of will be to make the high/low determination as soon as you accept user input (based on previous user input).

To do this, I am going to add a couple of flags to your struct. These flags will be initially set to FALSE, but will be set to TRUE anytime the struct qualifies for highest or lowest, and will be set back to FALSE whenever it no longer qualifies as the highest or lowest.

For this example, I will be using the 'temperature' attribute of the struct:

``````struct WeatherStation {

bool highest;
bool lowest;
string StationDesignation;
double Temperature;

//Inline Constructor
WeatherStation(){highest = FALSE;  lowest = FALSE;}
}CA, FL, NY;

string input;
int high=-200, low=200;

//Caleeforneeaah
cout << "\nEnter California station designation:  ";
cin >> CA.StationDesignation;
cout >> "\nEnter California temperature:  ";
cin >> CA.Temperature;

if(CA.Temperature > high)
{
high = CA.Temperature;
CA.highest = TRUE;
}
else if(CA.Temperature <low)
{
low = CA.Temperature;
CA.lowest = TRUE;
}

//Pablo honey, come to Florida...
cout << "\nEnter Florida station designation:  ";
cin >> FL.StationDesignation;
cout >> "\nEnter Florida temperature:  ";
cin >> FL.Temperature;

if(FL.Temperature > high)
{
high = FL.Temperature;
FL.highest = TRUE;
CA.highest = FALSE:
}
else if(FL.Temperature < low)
{
low = FL.Temperature;
FL.lowest = TRUE;
CA.lowest = FALSE;
}

//New York.
cout << "\nEnter New York station designation:  ";
cin >> NY.StationDesignation;
cout >> "\nEnter New York temperature:  ";
cin >> NY.Temperature;

if(NY.Temperature > high)
{
high = NY.Temperature;
NY.highest = TRUE;
CA.highest = FALSE:
FL.highest = FALSE;
}
else if(NY.Temperature < low)
{
low = NY.Temperature;
NY.lowest = TRUE;
CA.lowest = FALSE;
FL.lowest = FALSE;
}``````

As you can see, the code can get pretty long and drawn out since we lose the ability to loop through an array of structs. All high/low determination is made at the point of user input. You now have the ability to identify the struct with the highest temperature, whenever struct.highest == TRUE. Conversely, you can identify the struct containing the lowest tempeature whenever struct.lowest == TRUE.

This is untested code an may contain easy to fix errors. If any knows a better way to do this, please let us know.

PS
I could not understand what a member of the structure is determinative.

How would I make an array of struct? I think that would be more appropriate for my project since there will be type-in command

#include <iostream> #include <string>

using namespace std;

struct WeatherStation {

string StationDesignation;

double Temperature;

} CA, FL, NY;

// EXAMPLE, I WILL ADD A LOOP FOR FL AND NY

int main()

{ string Command; //"ENTER DATA" COMMAND (WILL ADD TYPE IN COMMAND AFTER I FIGURE OUT HOW TO ARRAY STRUCT) WeatherStation CA; cout<<"Enter reported temperatures..."<<endl; cout<<endl; cout<<"Weather Station CA: "; cin>>CA.Temperature; cout<<"Weather Station FL: "; cin>>FL.Temperature; cout<<"Weather Station NY: "; cin>>NY.Temperature;

//THEN SHOW HIGHEST AND LOWEST SOMEHOW "SHOW HIGHEST AND LOWEST" COMMAND ??? ??? ???[code c]
#include <iostream>
#include <string>

using namespace std;

struct WeatherStation {

string StationDesignation;

double Temperature;

} CA, FL, NY;

// EXAMPLE, I WILL ADD A LOOP FOR FL AND NY

int main()

{
string Command;
//"ENTER DATA" COMMAND (WILL ADD TYPE IN COMMAND AFTER I FIGURE OUT HOW TO ARRAY STRUCT)
WeatherStation CA;
cout<<"Enter reported temperatures..."<<endl;
cout<<endl;
cout<<"Weather Station CA: ";
cin>>CA.Temperature;
cout<<"Weather Station FL: ";
cin>>FL.Temperature;
cout<<"Weather Station NY: ";
cin>>NY.Temperature;

//THEN SHOW HIGHEST AND LOWEST SOMEHOW "SHOW HIGHEST AND LOWEST" COMMAND
???
???
???

This is a common topic and very necessary for your CS academic career.

First, create your object:

``````struct WeatherStation{

string StationDesignation;
double Temperature;
};``````

Now create a bunch of them. For your assignment, I think you only need 3 of them:

``````//creates array elements [0] thru [2]
WeatherStation stations[3];``````

Now populate your 'station' objects:

``````char ans;
int i=0;

do{

cout << "\nEnter Station Designation:  ";
cin >> station[i].StationDesignation;

cout << "\nEnter Station Temperature:  ";
cin >> station[i].Temperature;

cout << "\nWould you like to enter another station?  (Y/N) ";
cin >> ans;

i++;

}while(ans == 'Y' || ans == 'y');``````

Now that your 3 'station' objects are populated, you can now sort the array. I'm in a fiesty mood today, so I'll use the swap( ) function from <algorithm>:

``````for(int i=0; i<2; i++)
{
if(station[i].Temperature > station[i+1].Temperature)

swap(&station[i+1], &station[i]);
}``````

Now your 3 'station' objects are sorted by their temperature attribute to ascending order (lowest to highest), so you know that station[0] will be the struct with the lowest temperature, and station[2] will be the struct with the highest temperature.

Also, I hope you can see how much code we have saved just by using an array data structure. It allows us to do the same things over and over via the use of loops and common array operations.

This is untested code and may contain easy to fix errors. If anyone knows of a better way to do this, please let us know.

``````bool compare(WeatherStation& st1, WeatherStation& st2) {
return (st1.Temperature < st2.Temperature);
}

int main() {
std::vector<WeatherStation> WeatherStations(3);
std::vector<WeatherStation>::iterator max;
/** fill the array... */
/** ... */

/** enumerate all for determinate max */
max = std::max_element(WeatherStations.begin(), WeatherStations.end(), compare);

return 0;
}``````

Is there anyway to assign CA, FL, and NY to an array WITHOUT having to type them in? For example

Enter data for CA: (User input)
Enter data for FL: (User input)
Enter Data for NY: (User input)

Then find the highest and lowest depending on what the user Enters

Is there anyway to assign CA, FL, and NY to an array WITHOUT having to type them in?

I am not sure what you mean by this, but in my previous two examples, I prompted the user for input and then tested the input to make high/low determinations.

Edit: Ok, I think I know what you mean now.. you want to display specifcially which state you are entering instead of my generic prompts for user information.

Let's throw a little switch( ) action inside of our loop in order to customize our prompts for user input:

``````char ans;
int i=0;

do{

switch(i)
{

case 1:  cout << "\nEnter data for CA:  ";
cin >> station[i].StationDesignation;
cin >> station[i].Temperature;
break;

case 2: cout << "\nEnter data for FL:  ";
cin >> station[i].StationDesignation;
cin >> station[i].Temperature;
break;

case 3: cout << "\nEnter data for NY:  ";
cin >> station[i].StationDesignation;
cin >> station[i].Temperature;
break;

default:  cout << "\a\nInvalid Entry! ";
cin.get();
break;

cout << "\nWould you like to enter another station?  (Y/N) ";
cin >> ans;

i++;

}while(ans == 'Y' || ans == 'y');``````

In your previous example, I think you had the user input the STATES. I need it set to that the states are already in the arrays (if possible).

``````Now populate your 'station' objects:

char ans;
int i=0;

do{

cout << "\nEnter Station Designation:  ";
cin >> station[i].StationDesignation;

cout << "\nEnter Station Temperature:  ";
cin >> station[i].Temperature;

cout << "\nWould you like to enter another station?  (Y/N) ";
cin >> ans;

i++;

}while(ans == 'Y' || ans == 'y');

Now that your 3 'station' objects are populated, you can now sort the array.``````

I need the object (STATES) to be populated w/o the user input.

Ok this is my project so far,

``````#include <iostream>
#include <string>
using namespace std;

struct WeatherStation {
string StationDesignation;
double Temperature;
};

int main()

{
string Command;

WeatherStation DeAnza
WeatherStation CA
WeatherStation FL

while(true)  {

cout << "Enter Command: ";
getline(cin, Command);
if(Command == "Quit")
break;
else if(Command == "Post Temperatures"){  //user inputs the temperature
cout<<"Enter reported temperatures..."<<endl;
cout<<endl;
cout<<"Weather Station DeAnza: ";
cin>>DeAnza.Temperature;
cout<<"Weather Station CA: ";
cin>>CA.Temperature;
cout<<"Weather Station FL: ";
cin>>FL.Temperature;
}
else if(Command == "Daily Report") //shows the data the user input
cout<<"DeAnza= "<<DeAnza.Temperature<<endl;
else if(Command == "High-Low Report") //Report low and high, (I don't know how to do this yet so its just cout for now)
cout<<"done:"<<endl;
}

}

{
cout << "-----------Choices-----------" << endl;
cout << "------------------------------" << endl;
cout << "1. Post Temperatures" << endl;
cout << "2. Daily Report" << endl;
cout << "3. High-Low Report" << endl;
cout << "4. Quit" << endl;
cout << "------------------------------" << endl;
}``````

I need to have it something like this, so my menu's and stuff all fit together. I need the DeAnza, CA, and FL to be already in an array without the user input. or if there is anyother way to find lowest and highest w/o array.

sorry for the confusion! (im pretty confused my self :P)

I need the object (STATES) to be populated w/o the user input.

I am still not sure what you mean by this. The struct object has no attribute named 'states'.

In order to populate your structs, you will have to prompt the user for input.

I have provided a previous example of how you can provide the user a specific prompt for each state in which the related struct is to be populated.

If you would like to add additional information to your struct, such as the state, this attribute could possibly be loaded via comparison to an existing data database of information according to let's say, the station designation (for example, station id N70FX3 is located in california, and we could assign 'California' to a <string> attribute of your struct).

Please be very specific with your requests.

I need the DeAnza, CA, and FL to be already in an array without the user input.

Ok, I think we are narrowing this down here...

Just try something like this:

``````station[0].StationDesignation = "DeAnza, CA";
station[1].StationDesignation = "Naples, FL";
station[2].StationDesignation = "Buffalo, NY";``````

Ok, I think we are narrowing this down here...

Just try something like this:

``````station[0].StationDesignation = "DeAnza, CA";
station[1].StationDesignation = "Naples, FL";
station[2].StationDesignation = "Buffalo, NY";``````

this seems to work, I'll try it and let you know the results, but now its Halloween!! Thanks for the help

this seems to work, I'll try it and let you know the results, but now its Halloween!!

Trick-or-Code (I think we all know the obvious answer to this question...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.