473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding min and max value in a struct

Hi, I'm pretty new to C++ and I want to find the min and max value in a collection of values. The pairs are in a text file and contain a descriptor and a value. For example: Bob 120 Jack 7 Larry -90 Pete 500 Ron -8 Here is the code I've worked out so far. I want to set min at first to be equal to the first value in the collection (120) but I have no clue how to do that. Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. struct collection
  6. {
  7. char descriptor[20];
  8. float value;
  9. };
  10.  
  11. bool trytoread(ifstream *ps,collection *ppt)
  12. {
  13. *ps>>(*ppt).descriptor;
  14. if (!*ps)
  15.     return false;
  16. *ps>>(*ppt).value;
  17. if (!*ps)
  18.     return false;
  19. return true;
  20. }
  21.  
  22. int main()
  23. {
  24. char fileName[80];
  25. cout<<"what input file? ";
  26. cin>>fileName;
  27. ifstream infile;
  28. infile.open(fileName);
  29. collection c;
  30. if (!infile)
  31.     {
  32.     cout<<"BAD OPEN";
  33.     exit(1);
  34.     }
  35. float min;
  36. while(trytoread(&infile,&c))
  37.     {
  38.         min=c.value;  //Want to set min to be the first value in the collection (120)
  39.     if (min>c.value)
  40.         min=c.value;
  41.     }
  42. cout<<"the minimum value is: "<<min<<endl;
  43. return 0;
  44. }
  45.  
Nov 17 '08 #1
2 6610
Banfa
9,065 Expert Mod 8TB
use a boolean variable (that many people tend to call something like firstTime). Set it to true outside the loop.

Inside the loop if it is true then set min to c.value and firstTime to false othewise perform your min>c.value comparison.

This is just a simple way to mark the first iteration of a loop. Note that for for loops it would be unnecessary as you can just use the loop variable plus the initial condition of that variable to detect the first iteration.
Nov 17 '08 #2
thank you for the help! that worked great.
Nov 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Adam H. Peterson | last post by:
Is there a standard way to find the minimum value for a data type? I'm thinking along the lines of std::numeric_limits<T>::min(). But that doesn't work for floating point types. I can't use...
5
by: Petar#M | last post by:
Dear all, Is there any way to derive the offset of a subclass without instantiating the class (in other words none of the *_cast operators can be used). So, e.g.: class A {}; class B {};...
20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
19
by: Rahul | last post by:
Hi, Is there a way to find the offset of a class member at compile time. e.g. class A{ int i; int j; char c; }; Here the offset of c = 8 bytes from the start of an object of A (assuming 4...
4
by: bbg9507 | last post by:
Hi, question about using ArrayList from C# newbie, I managed to make my array list which holds a number of structs: struct myStruct { public int field1; public int field2; }
16
by: Hendrik Schober | last post by:
Hi, suppose we have template< typename T > struct X; and some specializations: template<>
17
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.