C++ fstream and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • justin_ator
    🥓<strong><span style="col
    • Mar 2007
    • 7648

    #1

    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
    Caelondia Represent
    • Jul 2004
    • 1984

    #2
    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.
    Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

    Comment

    • justin_ator
      🥓<strong><span style="col
      • Mar 2007
      • 7648

      #3
      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

      Comment

      • Kibblre
        Caelondia Represent
        • Jul 2004
        • 1984

        #4
        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, 12:41 PM.
        Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

        Comment

        • FissionMailed1
          FFR Player
          • Feb 2012
          • 1267

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

          Comment

          • justin_ator
            🥓<strong><span style="col
            • Mar 2007
            • 7648

            #6
            Re: C++ fstream and arrays

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

            input.get(); ?

            Comment

            • FissionMailed1
              FFR Player
              • Feb 2012
              • 1267

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

              Comment

              • justin_ator
                🥓<strong><span style="col
                • Mar 2007
                • 7648

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

                Comment

                • Kibblre
                  Caelondia Represent
                  • Jul 2004
                  • 1984

                  #9
                  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);
                  }
                  Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                  Comment

                  • FissionMailed1
                    FFR Player
                    • Feb 2012
                    • 1267

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

                    Comment

                    • Kibblre
                      Caelondia Represent
                      • Jul 2004
                      • 1984

                      #11
                      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.
                      Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                      Comment

                      • justin_ator
                        🥓<strong><span style="col
                        • Mar 2007
                        • 7648

                        #12
                        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, 02:06 PM.

                        Comment

                        • qqwref
                          stepmania archaeologist
                          FFR Simfile Author
                          • Aug 2005
                          • 4092

                          #13
                          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)

                          Comment

                          • MarioNintendo
                            Expect delays.
                            FFR Simfile Author
                            FFR Music Producer
                            • Mar 2008
                            • 4177

                            #14
                            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

                            Comment

                            • emerald000
                              the Mathemagician~
                              • Nov 2005
                              • 1320

                              #15
                              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, 05:14 PM.

                              Comment

                              Working...