Flash Flash Revolution

Flash Flash Revolution (http://www.flashflashrevolution.com/vbz/index.php)
-   Technology (http://www.flashflashrevolution.com/vbz/forumdisplay.php?f=74)
-   -   C++ fstream and arrays (http://www.flashflashrevolution.com/vbz/showthread.php?t=127793)

justin_ator 11-30-2012 12:53 PM

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.

Kibblre 11-30-2012 01:11 PM

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.

justin_ator 11-30-2012 01:27 PM

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

Kibblre 11-30-2012 01:32 PM

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.

FissionMailed1 11-30-2012 01:49 PM

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.

justin_ator 11-30-2012 01:57 PM

Re: C++ fstream and arrays
 
What's the best way to read in character by character for an input file?

input.get(); ?

FissionMailed1 11-30-2012 02:02 PM

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.

justin_ator 11-30-2012 02:04 PM

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.

Kibblre 11-30-2012 02:06 PM

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);
}


FissionMailed1 11-30-2012 02:09 PM

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.

Kibblre 11-30-2012 02:13 PM

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.

justin_ator 11-30-2012 02:15 PM

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..

qqwref 11-30-2012 04:54 PM

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');
 }
}


MarioNintendo 11-30-2012 05:40 PM

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

emerald000 11-30-2012 06:11 PM

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.

justin_ator 12-1-2012 12:47 AM

Re: C++ fstream and arrays
 
Quote:

Originally Posted by emerald000 (Post 3810994)
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.

Patashu 12-2-2012 12:07 AM

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)

FissionMailed1 12-2-2012 12:17 AM

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.

Patashu 12-2-2012 01:30 AM

Re: C++ fstream and arrays
 
Quote:

Originally Posted by FissionMailed1 (Post 3811622)
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


All times are GMT -5. The time now is 04:56 AM.

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