I need to find the best way to contain baseball statistics in this format -
player's name (20 characters)
-
player's team (3 characters)
-
at bats (integer)
-
hits (integer)
-
doubles (integer)
-
triples (integer)
-
homeruns (integer)
-
So a sample file might look something like this. -
David Aardsma CHW 0 0 0 0 0
-
Bobby Abreu NYY 605 171 40 5 16
-
Jeremy Accardo TOR 0 0 0 0 0
-
Russ Adams TOR 60 14 3 0 2
-
Brian N. Anderson CHW 17 2 1 0 0
-
and I need to be able to sort it by either team, hits, batting average, and slugging percentage.
I was thinking a giant table might be simple, but when you sort a table, all the columns move with the row, right?
is there a better way than a table? I guess thats the real question I'm asking
15 2947
I need to find the best way to contain baseball statistics in this format -
player's name (20 characters)
-
player's team (3 characters)
-
at bats (integer)
-
hits (integer)
-
doubles (integer)
-
triples (integer)
-
homeruns (integer)
-
So a sample file might look something like this. -
David Aardsma CHW 0 0 0 0 0
-
Bobby Abreu NYY 605 171 40 5 16
-
Jeremy Accardo TOR 0 0 0 0 0
-
Russ Adams TOR 60 14 3 0 2
-
Brian N. Anderson CHW 17 2 1 0 0
-
and I need to be able to sort it by either team, hits, batting average, and slugging percentage.
I was thinking a giant table might be simple, but when you sort a table, all the columns move with the row, right?
is there a better way than a table? I guess thats the real question I'm asking
The columns also has to move along with the row if u are sorting the table right?
R u asking how else u can store?
Raghuram
yes, or rather, is there an easier way to go about this?
yes, or rather, is there an easier way to go about this?
You can store it in form of a structure.
this is advantageous when u want to sort it on the basis of different columns.
You have to write a comparison function for different columns and during run time yu can deceide which column to use for sorting.
Raghuram
So, here's how you might analyze this issue. And there's no one answer to it.
You have a table of entries, in which you have a fixed number of columns and arbitrary number of rows. There is no table concept in C++, so you'll have to create your table either by programming an appropriate data structure or using something that fits in C++.
A very straightforward to look at this issue would be to create a structure in C++. It's a way to group various data types into some arbitrary giant datatype. And then work with that datatype as a whole. So you could represent a row with a structure. Store these rows in a vector.
When you want to sort the vector, you can rely on the sort function provided in <algorithm>. What you do is code a function that compares two rows in a certain method (by name, or a certain statistic, or a team, etc.) and then returns a value indicating less than or greater than. Pass this function into sort so that it can properly sort the rows.
You can look up all the C++ stuff I mentioned on Google to see real code and details.
I'm having trouble reading each line of data individually, since I'd like to have my structure in this form -
struct stats
-
{
-
char[20] name;
-
char[3] team;
-
int bats, doubles, triples, homeruns;
-
real BA, SP;
-
}
-
so how can I read each line of data (what I posted originally) into the structure, and then into the vector?
Using getline doesn't seem reasonable, since it reads the entire line as a string.
I was thinking of reading each character seperated by whitespace, but how does it know when the line has ended, and thus to create another structure?
Also, since multiple structures are being created, I was thinking of naming each structure " 1, 2, 3, ..." until the last data piece is read.
does something like
read 1, or count?
C++ doesn't have a 'real' builtin datatype. You probably mean 'double'. You should probably read a tutorial about how to use structs/classes (they're basically identical in C++).
As to your I/O questions, the newline character, \n, will tell you when you're at the end of a line. I'd probably use >> myself, since everything's going to be in the same order when you read it (I hope so, it'll make your life much, much easier).
how do you check to see when the newline character is reached?
how do you check to see when the newline character is reached?
whenever u read a line using cin.getline it stops with the newline character
Raghuram
however if I use getline, it will just give me the line of data in a string, which doesn't easily enable me to separate the line into pieces, so that I can put those pieces into a struct.
I was thinking that since the data is formatted, I could use the get function to obtain the name, since it contains spaces that the >> couldn't get. I could then use >> to place the rest of the items into the assigned struct element.
However by using this method, I don't know how to get the program to recognize a new line, so the loop can restart, and a new struct item can be made.
Remember that if you cin to a string (or char*), it's whitespace-delimited. Any whitespace, be it a tab, space, or newline (or the end of the file, for that matter). Also, since you're using C++, use std::string rather than C-strings.
You're all making this way too hard. Myxamatosis, yes, getlines gets you a giant string. But you can always parse this string (tokenize, if you will) into individual strings, numbers, and so on. Stringstreams are a useful solution, although verbose. You should google stringstreams and various combinations like stringstreams convert or stringstreams tokenize and see what you get. You'll see plenty of code samples out there that show you how to deal with tokenizing a string.
Alright, the stringstream thing worked wonders for the program.
The problem now is with the structures. since I don't know the number of data pieces in the file, how do i go about naming each structure?
I had an idea of doing something like -
struct stats myStats[100] = {0};
-
so that way each struct could be named in the form
however this gives me an error saying
"ISO C++ forbids assignment of arrays"
How do i create ever expanding structs?
You can use a vector for that, rather than an array. Vectors are dynamically sized arrays that have protective code so that it's harder to do some of the nasty array/pointer screwups. If you really want to use the array, don't set it to null. Just use 'myStats stats[100];'. -
vector<myStats> stats; //stats can be as large as your memory can fit, rather than limited by your size...the syntax with [] is the same as arrays
-
here's the code I'm using to test out if assigning each structure element works. I know it doesn't though, and I think I'm getting confused by the syntax -
int main()
-
{
-
struct stats
-
{
-
string name;
-
string team;
-
int bats, hits, doubles, triples, homeruns;
-
};
-
-
vector<stats> myStats;
-
-
string stat = "Justin Verlander";
-
string t = "DET";
-
int b = 0, h = 0, d = 0, trip = 0, hr = 0;
-
-
myStats[0].name = stat;
-
myStats[0].team = t;
-
myStats[0].bats = b;
-
myStats[0].hits = h;
-
myStats[0].doubles = d;
-
myStats[0].triples = trip;
-
myStats[0].homeruns = hr;
-
}
-
so lets say I had a stats struct names "temp".
if I used "temp" instead of "myStats[0]" and then did
do i Then access the elements by this? -
cout << myStats[0].doubles << endl;
-
is that the right syntax?
That is the proper syntax. However, you cannot define a struct inside main() (or any other function). You must move it outside the function.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Doug |
last post by:
I am looking for PHP5 web hosting and many companies have not moved up
to using PHP5 yet. Is there any one who has had any experiences good or
bad with any company using PHP5?
-d
|
by: gambler |
last post by:
let's say you have:
var games = new Array();
games = new GAME(gameNum, rotNum1, rotNum2, ... );
(
so a sparsley populate array which enables me to locate a game usin
the game number...
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their...
|
by: P Adhia |
last post by:
Hello,
If the explain shows that DB2 needs to sort the result of a cursor,
does that always happen? i.e. if the resultset of the cursor is empty,
does the sort have any overhead? It appears...
|
by: Lucas Tam |
last post by:
Hi all,
I have an application which logs a considerable amount of data. Each day,
we log about 50,000 to 100,000 rows of data.
We like to report on this data... currently I'm using a stored...
|
by: Fredrik Melin |
last post by:
I have a "Inventory" Object that contains the product and all its fields.
The problem is that I am getting soooooo many functions under main Inventory
class so it becames impossible to initalize...
|
by: Mudcat |
last post by:
Hi,
I am trying to build a tool that analyzes stock data. Therefore I am
going to download and store quite a vast amount of it. Just for a
general number - assuming there are about 7000 listed...
|
by: ankitdesai |
last post by:
I would like to parse a couple of tables within an individual player's
SHTML page. For example, I would like to get the "Actual Pitching
Statistics" and the "Translated Pitching Statistics"...
|
by: gw7rib |
last post by:
I'm writing a program which has "notes" - these can appear on the
screen as windows with text in. It is possible to create an "index
note" - at present, this will contain a list of the titles (or...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |