Click to See Complete Forum and Search --> : Create Easy Windows Table from C++
vinniez_ok
November 29th, 2006, 11:42 AM
Hi All,
Please reference this thread :: Vector Sort Multidimensional Array
I'd like to take this output and push it to a GUI window from this code once compiled.
The output should look something like this ::
Name Age Wage Ranking
===== ==== === =======
Ben 9 20 1
Matt 10 28 2
Steve 12 33 1
Cliff 13 33 2
Brandon 15 14 1
Vini 17 28 3
Tom 21 30 3
Mark 22 37 4
Tim 34 28 4
Thanks so much!
vinniez_ok
November 29th, 2006, 07:39 PM
Here's the code from the other posting. In fact, Paul's use of structures works better than the multidimensional array, so I'm going with that ::
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
struct PersonInfo
{
std::string name;
int age;
int wage;
PersonInfo(const std::string& n, int a, int w) : name(n), age(a), wage(w) { }
};
bool SortByAge(const PersonInfo& first, const PersonInfo& second)
{
if ( first.age < second.age )
return true;
return false;
}
bool SortByWage(const PersonInfo& first, const PersonInfo& second)
{
if ( first.age == second.age )
{
if ( first.wage < second.wage )
return true;
return false;
}
return first.age < second.age;
}
// commented out since names/data will be provided
// std::string NameGenerator()
// {
// static int i = 0;
// ++i;
// std::string sName = "Joe";
// return sName += (char)(i + '0');
// }
void SortByAgeWage(std::vector<PersonInfo>& info)
{
// first sort by age
std::sort(info.begin(), info.end(), SortByAge);
// sort by wage given that the array has been sorted by age
std::sort(info.begin(), info.end(), SortByWage);
}
int main()
{
std::vector<PersonInfo> theInfo;
theInfo.push_back(Tim, 12, 33);
theInfo.push_back(Mark, 34, 28);
theInfo.push_back(Tom, 21, 30);
theInfo.push_back(Cliff, 13, 33);
theInfo.push_back(Vini, 17, 28);
theInfo.push_back(Matt, 10, 28);
theInfo.push_back(Ben, 9, 29);
theInfo.push_back(Brandon, 15, 14);
// removed this since I supplied the data to be used -- theInfo.push_back(PersonInfo(NameGenerator(), 7, 1));
//...
int i;
std::cout << "Sorted by Age + Wage\n\n";
std::cout << "Name Age Wage Ranking\n";
std::cout << "===== ==== ==== =======\n";
SortByAgeWage(theInfo);
for ( i = 0; i < theInfo.size(); ++i )
std::cout << theInfo[i].name << " "
<< theInfo[i].age << " " << theInfo[i].wage << "\n";
// Somehow here the cout makes it to a GUI App in Windows ??
}
dave2k
November 30th, 2006, 03:17 AM
Why not provide operators inside PersonInfo? This would in my opinion be neater.
dave2k
November 30th, 2006, 03:56 AM
Stick the following inside the struct: bool operator<(const PersonInfo& rhs) const
{
if( age == rhs.age )
return wage < rhs.wage;
return ( age < rhs.age );
}and then you only require one sort and you can get rid of thoose two functions:void SortByAgeWage(std::vector<PersonInfo>& info)
{
std::sort(info.begin(), info.end());
}
vinniez_ok
November 30th, 2006, 10:57 AM
Thanks Dave! Reviewing code now.
What really has me stymied is the output to a Window. . . there're so many postings that perform much more than I need to. I am not really sure what I can jettison so that (as you present above) I would be left with the cleanest, easiest way to create a table. I didn't wanna use a sledgehammer to slap a table onto a GUI.
Appreciated!
Vin
vinniez_ok
November 30th, 2006, 11:31 AM
:confused: What I have to do is a very small piece of code that sends a table from compiled C++ code to a Windows GUI.
I've been searching for days for something extremely simple to send 4 columns/10 rows of data out and can't find anything without a lot of clutter.
I realize it's not proper to ask for code but I don't anticipate having to do this again with my C++ code --Do you know of a posting or some tutorial that can pull off this very easy event?
Paul McKenzie
November 30th, 2006, 11:38 AM
:confused: What I have to do is a very small piece of code that sends a table from compiled C++ code to a Windows GUI.In Charles Petzold's programming Windows books, the first chapter shows output to a basic window using "Hello World".
Maybe you need to show us exactly what you want to do in terms of what type of control you're expecting to show output in. There are so many GUI controls that can display text.
Regards,
Paul McKenzie
Paul McKenzie
November 30th, 2006, 11:43 AM
Stick the following inside the struct: bool operator<(const PersonInfo& rhs) const
{
if( age == rhs.age )
return wage < rhs.wage;
return ( age < rhs.age );
}and then you only require one sort and you can get rid of thoose two functions:void SortByAgeWage(std::vector<PersonInfo>& info)
{
std::sort(info.begin(), info.end());
}I agree that this would have been shorter than my example, but I've never liked overloading operator < in this context.
If the class were a number type, or a type where < is well-defined in theory, then that's where overloading < would come into play. I just don't see it here in this case, since the sorting of age and then wage is more specialized than a simple, theoretical ordering.
Regards,
Paul McKenzie
vinniez_ok
November 30th, 2006, 12:42 PM
Hi Again Paul
I thought thatz where Dave was going with "<"; it's been over a year since I've worked with C++ but thought I recognized overloading.
I reviewed Petzold's sample chapter and~par for the course in my researching~see lots about fonts & graphics . . . but all I need is the data from the table to print out on a plain pop-up window. Perhaps something like this is all I need?
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static CHOOSEFONT cf ;
static DOCINFO di = { sizeof (DOCINFO), TEXT ("Justify2: Printing") } ;
static int iAlign = IDM_ALIGN_LEFT ;
static LOGFONT lf ;
static PRINTDLG pd ;
static TCHAR szText[] = {
TEXT ("Call me Ishmael. Some years ago -- never ")
TEXT ("mind how long precisely -- having little ")
TEXT ("or no money in my purse, and nothing
vinniez_ok
November 30th, 2006, 04:26 PM
someone suggested a listview.
that sounds like it may be simple enuf for me.
is that an option?
dave2k
December 1st, 2006, 03:20 AM
There is a lot to learn before ouputting stuff to a window. i would create an mfc dialog - based application and then practice drawing on the dialog. to learn about mfc get 'programming windows with mfc'
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.