Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 11-30-2012, 12:53 PM   #1
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default C++ fstream and arrays

Okay, so I'm making this thread for two reasons:
1) I'm frustrated because I can't figure out what I'm doing
2) I want to learn how to do this correctly

I was making a program for ssbmchamp's Powerball thread that would make things easier for comparing and figuring out payouts, etc.

This is a general idea of my pseudo code:
Code:
initialize input file and output files

take in lines for each user, with their guesses

guesses assigned to an array guess[i]
guess[i] is compared to numbers[i through i+4], to see if there are any matches
output and credit distribution is based on how many matches they have, etc.
My issue is as follows:
Lines are being read in as
justin_ator [#,#,#,#,#][#][#]
and I need a way to split the lines into the username, numbers, and useless info.

I'm fine with the concept of doing
Username
[#,#,#,#,#][#][#]
so that the username isn't an issue for reading, but I still don't know what I'm doing after that. I tried looking into tokens and delimiters, etc, but I just don't know how to assign the #'s to the array guess[i] without dealing with the [ and , and ]... I'm kind of confused.

This was probably confusing. If I need to clarify something more, please do ask.

I didn't post my code because I don't want you to write the program for me.. I want to learn how istringstream, tokens, iss, etc work if that's the best route to go. I tried looking into it and I was just kinda lost.
justin_ator is offline   Reply With Quote
Old 11-30-2012, 01:11 PM   #2
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default Re: C++ fstream and arrays

I'm not exactly sure what you want to do, but if you're just scanning for guesses you could just make an array of arrays, one that holds each users guesses. You can then make a loop that goes through each element of the larger array (1 for each user) and scan each guess and just have an int counter to count how many are correct. Something like:

Code:
int correctGuesses = 0;
int correctNumbers [7] = {7, 21, 32, 44, 10, 39, 80}
for(int i = 0; i < numUsers; i++)
{
     for(int j = 0; j < 6; j++)
          {
               for(int k = 0; k < 6; k++)
                    {
Here's your 2d arary: if(usersArray[i][j] == correctNumbers[k])
                            correctGuesses++;
                    }
          }
}
Then just save the correct guesses counts for each user into another array to keep track of how many each user got right. Then you can do another loop of sorts to do a payout calculation. You can also dick around with usernames or just remember what order you had them in.

Btw, I probably did a bunch wrong since I suck at C++ as well, but I think this make sense.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.
Kibblre is offline   Reply With Quote
Old 11-30-2012, 01:27 PM   #3
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default Re: C++ fstream and arrays

Well my issue isn't so much how to compare or get results, it's in the process of setting it up.

I'm reading in a text file using fstream, that would have the list like this
username [#,#,#,#,#][#][#]
username [#,#,#,#,#][#][#]
username [#,#,#,#,#][#][#]
username [#,#,#,#,#][#][#]
username [#,#,#,#,#][#][#]
username [#,#,#,#,#][#][#]
username [#,#,#,#,#][#][#]

and I'm running it in a loop for each line, so that's not the issue either. The issue is how do I save the #'s to an array while ignoring the [ , ] stuff when reading from the file?

I was using getline, but then I can't separate the #'s into each guess[] spot because they're being read in as character strings, but input.get(); is giving me weird values. I have no idea what I'm doing lmfao
justin_ator is offline   Reply With Quote
Old 11-30-2012, 01:32 PM   #4
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default Re: C++ fstream and arrays

You can set up an if/else statement for each block of text you read in. This will change depending on how you're inputting (strings, chars, ints), but it would look something like this:

Code:
for(blah blah blah)
{
     if(input == ' ' || input == '[' || input == ',' || input == ']')
          continue;
     else
          (insert into array or however you're storing the inputs)
}
The continue command if you didn't know restarts the loop basically. So say you're at the 5th iteration and the input is a space, it'll start the loop at the beginning from the 6th iteration.

EDIT: You can just read in everything as strings and input them like username [ # # # # # ][ # ][ # ] and you should be fine. Just make sure you use " marks if you're ever checking things (ie, guess = "22").

EDIT 2: Doing ^, you can also test for usernames pretty easily. Just test if(input.length() > 2). If the test succeeds, that means it can't be a 2 digit number and must be a username allowing you to place it in the correct array.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

Last edited by Kibblre; 11-30-2012 at 01:41 PM..
Kibblre is offline   Reply With Quote
Old 11-30-2012, 01:49 PM   #5
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default Re: C++ fstream and arrays

http://www.boost.org/doc/libs/1_52_0/libs/tokenizer/ This should do what you are looking for without having to write your own tokenizer.
__________________


YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

I LOVE YOU CHEETOS.
FissionMailed1 is offline   Reply With Quote
Old 11-30-2012, 01:57 PM   #6
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default Re: C++ fstream and arrays

What's the best way to read in character by character for an input file?

input.get(); ?
justin_ator is offline   Reply With Quote
Old 11-30-2012, 02:02 PM   #7
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default Re: C++ fstream and arrays

You could do it that way, sure. You would just do something like this (basic version):

int c;
for (stuff) {
c = input.get();
if (c != bad char && c != bad char && ...)
// save it
}

I'd still recommend using a tokenizer though like the one I linked above since it makes everything a lot easier than figuring out what to throw away and what to keep.
__________________


YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

I LOVE YOU CHEETOS.
FissionMailed1 is offline   Reply With Quote
Old 11-30-2012, 02:04 PM   #8
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default Re: C++ fstream and arrays

Thanks guys. I'll look into the tokenizer thing and see if I can't get at least one of these two ways working right. Will be back with results.
justin_ator is offline   Reply With Quote
Old 11-30-2012, 02:06 PM   #9
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default Re: C++ fstream and arrays

Char by char would be really messy. Like I said, inputting strings would work best if you inputted the data like I showed (spaces and all). Here's an example of reading in strings from a text file and placing them into an array:

Code:
string array [size];
ifstream file;
file.open("name.txt");

while(!file.eof())
{
     string input;
     file>>input;
     array.push_back(input);
}
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.
Kibblre is offline   Reply With Quote
Old 11-30-2012, 02:09 PM   #10
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default Re: C++ fstream and arrays

Kibblre, you don't want to read the entire string, just the important information (the numbers). If you read the entire string, you will still have to split it up anyways, so it doesn't net you anything.
__________________


YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

I LOVE YOU CHEETOS.
FissionMailed1 is offline   Reply With Quote
Old 11-30-2012, 02:13 PM   #11
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default Re: C++ fstream and arrays

It's not the most efficient way, but it's a very simple way of doing it in terms of understanding code, which is what I was shooting for.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.
Kibblre is offline   Reply With Quote
Old 11-30-2012, 02:15 PM   #12
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default Re: C++ fstream and arrays

Char by char is only particularly annoying because the numbers can be more than one digit, so a two digit number has to go as a whole into one part of the array, not as two different numbers.

Trying to work through tokenizer

okay... I'm getting WHAT tokenizer does I think, but I'm not understanding how..

Last edited by justin_ator; 11-30-2012 at 03:06 PM..
justin_ator is offline   Reply With Quote
Old 11-30-2012, 04:54 PM   #13
qqwref
stepmania archaeologist
Retired StaffFFR Simfile AuthorFFR Veteran
 
qqwref's Avatar
 
Join Date: Aug 2005
Age: 34
Posts: 4,090
Default Re: C++ fstream and arrays

You can do it character by character - it's actually not that bad since you are just reading positive integers.

Code:
int[] guesses = (array of zeroes); // this user's guesses
int guessnum = 0; // start from the first guessed number

for(character in string starting from the first '[')
{
 if (character == ',' || character == ']') { // go to next guessed number
  guessnum++;
 } else if (character >= '0' && character <= '9') {
  guesses[guessnum] = 10*guesses[guessnum] + (character - '0');
 }
}
__________________
Best AAA: Policy In The Sky [Oni] (81)
Best SDG: PANTS (86)
Best FC: Future Invasion (93)
qqwref is offline   Reply With Quote
Old 11-30-2012, 05:40 PM   #14
MarioNintendo
Expect delays.
Retired StaffFFR Simfile AuthorFFR Music ProducerFFR Veteran
 
MarioNintendo's Avatar
 
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,121
Default Re: C++ fstream and arrays

Next year I have a course called Numerical Physics at University. If I need help, could you guys help me out? :3
__________________
Click here to download my complete works!




Quote:
Originally Posted by macgravel View Post
Mario's files are top notch, so every time he submits a file I get excited in my pants.
Quote:
Originally Posted by hi19hi19 View Post
This guy, MarioNintendo?
Click the bricks


MarioNintendo is offline   Reply With Quote
Old 11-30-2012, 06:11 PM   #15
emerald000
the Mathemagician~
Retired StaffFFR Veteran
 
emerald000's Avatar
 
Join Date: Nov 2005
Location: Quebec City
Age: 33
Posts: 1,320
Send a message via Skype™ to emerald000
Default Re: C++ fstream and arrays

I would do some preprocessing on the text file before inputting it in the program. It would make it much easier. Just do a mass search and replace into a format such as:

Username,1,2,3,4,5,6,7

Then, you can use std::getline(stream, number, ',') to get each part one after the other.

MarioNintendo: I am always up to lend a hand to people that need help in maths/CS/other pure sciences. Just post somewhere here (Homework or Bits and Bytes boards) or IM me.

Last edited by emerald000; 11-30-2012 at 06:14 PM..
emerald000 is offline   Reply With Quote
Old 12-1-2012, 12:47 AM   #16
justin_ator
🥓<strong><span style="col
Resident Overseer
FFR Veteran
 
justin_ator's Avatar
 
Join Date: Mar 2007
Location: Kingsport, TN
Posts: 7,648
Default Re: C++ fstream and arrays

Quote:
Originally Posted by emerald000 View Post
I would do some preprocessing on the text file before inputting it in the program. It would make it much easier. Just do a mass search and replace into a format such as:

Username,1,2,3,4,5,6,7

Then, you can use std::getline(stream, number, ',') to get each part one after the other.
That makes sense. This is why I was saying the username part wasn't an issue because we can change the format. Didn't realize you could add those parameters to getline to make it that easy though. Thank you. I'll give it a try when I get done eating and showering.
justin_ator is offline   Reply With Quote
Old 12-2-2012, 12:07 AM   #17
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default Re: C++ fstream and arrays

I would write in a better* programming language and use regexes with capture groups to parse each line and spit out the individual elements for you

*better for non robotic beings

your regex would be something like
(\w+) \[(\d+),(\d+),(\d+),(\d+),(\d+)\]\[(\d+)\]\[(\d+)\]
(make sure to double all backslashes if your language doesn't have a way to specify to ignore them, like @" in C# and r" in python)
then you can search a line for that regex, take the match and use the contents of each match (first match will be username, second match onwards will be da numbers)
__________________
Patashu makes Chiptunes in Famitracker:
http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png
Patashu is offline   Reply With Quote
Old 12-2-2012, 12:17 AM   #18
FissionMailed1
FFR Player
 
FissionMailed1's Avatar
 
Join Date: Feb 2012
Location: Massachusetts
Age: 33
Posts: 1,267
Send a message via Skype™ to FissionMailed1
Default Re: C++ fstream and arrays

C++11 supports regexes as well if you want to go that route. http://solarianprogrammer.com/2011/1...egex-tutorial/ here is a tutorial for that.
__________________


YOUR THROBBING MULTIFARIOUS LUSTFUL DESIRES ARE COMPLETED N YOUR HYPER-ORANGE SELF, YOU MAKE ME LOVE AGAIN, YOU'VE CHANGED MY HEART, MY MELANCHOLIA DISAPPEARS WHEN YOU ARE INSIDE OF ME, MY HUMAN RAGE IS TEMPERED WHEN I AM INSIDE YOU, THE SECRET IS COMMUNICATION, LONGEVITY, STAMINA, REPETITION, FURY, SOULFUL KISSING, EARPLUGS. YOU FUCKING CORPORATE COCKS AND CUNTS.

MY ANXIETY COMPLETE, MY DESIRE REPLETE, THE TASTE OF ORANGE BLOOD AND CUM AND GREENBACKS RUNNING DOWN MY FACE. THE STREETS WILL RUN ORANGE WITH YOUR MIXTURE OF CHEETOS AND HUNDRED DOLLAR BILLS REGURGITATED AND EATEN AND SHIT OUT AGAIN AND EATEN AGAIN.

YOU ARE MY SCULPTURE, MY SCULPTRA, MY SELF-DEFINITION. MY DEFINITION OF HUMANITY, MY HARMONY. MY HEART AND MY MIND.

YOU ARE SO ORANGE. SO CRUNCHY. SO CONSUMABLE.

THE NEW ORANGE UNDERGROUND IS THE ORANGE UP MY ASS. AND YOUR ASS.

I LOVE YOU CHEETOS.
FissionMailed1 is offline   Reply With Quote
Old 12-2-2012, 01:30 AM   #19
Patashu
FFR Simfile Author
Retired StaffFFR Simfile Author
 
Patashu's Avatar
 
Join Date: Apr 2006
Location: we traced the call...it's coming from inside the house
Age: 33
Posts: 8,609
Send a message via AIM to Patashu Send a message via MSN to Patashu Send a message via Yahoo to Patashu
Default Re: C++ fstream and arrays

Quote:
Originally Posted by FissionMailed1 View Post
C++11 supports regexes as well if you want to go that route. http://solarianprogrammer.com/2011/1...egex-tutorial/ here is a tutorial for that.
C++11 is looking sexier every day :O
__________________
Patashu makes Chiptunes in Famitracker:
http://soundcloud.com/patashu/8bit-progressive-metal-fading-world
http://img.photobucket.com/albums/v216/Mechadragon/smallpackbanner.png
Best non-AAAs: ERx8 v2 (14-1-0-4), Hajnal (3-0-0-0), RunnyMorning (8-0-0-4), Xeno-Flow (1-0-0-3), Blue Rose (35-2-0-20), Ketsarku (14-0-0-0), Silence (1-0-0-0), Lolo (14-1-0-1)
http://i231.photobucket.com/albums/ee301/xiaoven/solorulzsig.png
Patashu is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution