• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
theBegger

Copy merged data to vector?

13 posts in this topic

I'm having trouble figuring out how to copy the data from the two txt files into a vector. I'm trying to do this so I can use vectors sort function for alphabetizing. With the way I've tried to write the code, I keep getting

77 request for member `SIZE1' in `insA', which is of non-class type `int[((unsigned int)((int)SIZE1))]'

Thanks

#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string> 
using namespace std;

#define theAdopted "The Adopted.txt"            //His family
#define theOriginals "The Originals.txt"        //Her family
#define theBigPicture "The Big Picture.txt"     //Our family

//Function Prototypes

class KeepRunning {                            //Prototype needed to keep console from closing.
  public:
    ~KeepRunning() {
      cin.get();}};

//Copies text
int copyLine(ifstream&, ifstream&, ofstream&);

int main()                                                                  
{   
    KeepRunning kr;  
     
    ifstream hisFamily("The Adopted.txt");
    ifstream herFamily("The Originals.txt");
    ofstream ourFamily("The Big Picture.txt");
    
    int SIZE1 = 200, SIZE2 = 200;  
    int insA[SIZE1]; 
    int insB[SIZE2];
    int outs[SIZE1 + SIZE2];
    int lineCountA = 0;
    int lineCountB = 0;
    string lineA;
    string lineB;
    
    //Retreive his family's grades.
    hisFamily;
    if(hisFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theAdopted << ". \n";
        return EXIT_FAILURE;
    } 
    //Retreive her family's grades.
    herFamily;
    if(herFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theOriginals << ". \n";
        return EXIT_FAILURE;
    }
    //Call theBigPicture.
    ourFamily;
    if(ourFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theBigPicture << ". \n";
        return EXIT_FAILURE;
    }        
    //Copy data hisFamily to ourFamily.
    getline(hisFamily, lineA);
    getline(herFamily, lineB);
    while(lineA.length() != 0 && lineB.length() != 0)
    {
        lineCountA++;
        lineCountB++;   
        ourFamily << lineA << endl;
        ourFamily << lineB << endl;      
        getline(hisFamily,lineA); 
        getline(herFamily,lineB);
    } 
    
    cout << "Input data mergered to file 'The Big Picture.exe'." << endl;
    //Close files.
    hisFamily.close();
    herFamily.close();
    ourFamily.close();
    return 0;
}  
int copyLine
    (ifstream& hisFamily,
     ifstream& herFamily,
     ofstream& ourFamily)
     {
         const char NWLN = '\n';
         char nextCh;
         int charCount = 0;
         
         //Merge
         hisFamily.get(nextCh);
         while ((nextCh != NWLN) && !hisFamily.eof())
         {
             ourFamily.put(nextCh);
             charCount++;
             hisFamily.ignore(nextCh);
             hisFamily.get (nextCh);
         }
         if(!hisFamily.eof())
         {
             ourFamily.put(NWLN);
             charCount++;
         }
         return charCount;
     }

               
Edited by theBegger
0

Share this post


Link to post
Share on other sites
`insA.SIZE1' doesn't mean anything. That's what the compiler is saying, and I agree. Which constructor of std::vector<char> are you trying to use in that line?

The fact that your code is full of commented-out lines and seems half baked doesn't help me understand what you are trying to do, so I can't be any more helpful than the compiler.
2

Share this post


Link to post
Share on other sites

Sorry, the commented out lines are my failed attempt at vector. I just didn't want to get rid of them yet...hanging onto the string of faith thinking I'll figure this out :/.

I'm really not sure which constructor I even should use...I've never dealt with vectors before, and we haven't studied them yet.

0

Share this post


Link to post
Share on other sites

The code works well, I'm just trying to use the vector class to alphabetize the data on the output .txt

 

I just edited it so that you can see the code vector-less...aside from the header.

 

This code lists the data contained in the two input.txt on the single output.txt, but it isn't alphabetized. I thought if I could copy the input data to a vector first, I could use sort, then copy the data to the output.txt.

 

Is that a bad idea?

0

Share this post


Link to post
Share on other sites

Sorry, the commented out lines are my failed attempt at vector. I just didn't want to get rid of them yet...hanging onto the string of faith thinking I'll figure this out :/.
I'm really not sure which constructor I even should use...I've never dealt with vectors before, and we haven't studied them yet.


Let me ask the question in another way: What did you want that line to do?

So you just have two text files and you want to combine their lines into an output file, but you want to use a vector to write the output sorted. Is that right? Do the files have the same number of lines? (implied by the way you wrote the loop...).
1

Share this post


Link to post
Share on other sites
I would write it something like this:
 
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>

void report_error(std::string message) {
  std::cerr << "ERROR: " << message << '\n';
  std::exit(EXIT_FAILURE);
}

void read_file_into_vector(std::string const &filename, std::vector<std::string> &lines) {
  std::ifstream ifs(filename.c_str());
  
  if (ifs.fail())
    report_error("Cannot open file \"" + filename + "\" for reading.");
  
  std::string line;
  
  while (std::getline(ifs, line))
    lines.push_back(line);
}

void write_file_from_vector(std::string const &filename, std::vector<std::string> const &lines) {
  std::ofstream ofs(filename.c_str());
  
  if (ofs.fail())
    report_error("Cannot open file \"" + filename + "\" for writing.");
  
  /* In C++11 you can just do this:
  for (auto &line : lines)
    ofs << line << '\n';
  */
  
  for (std::vector<std::string>::const_iterator it = lines.begin(), end = lines.end(); it != end; ++it)
    ofs << *it << '\n';
}

int main() {   
  std::vector<std::string> all_lines;
  
  read_file_into_vector("The Adopted.txt", all_lines);
  read_file_into_vector("The Originals.txt", all_lines);
  
  std::sort(all_lines.begin(), all_lines.end());
  
  write_file_from_vector("The Big Picture.txt", all_lines);
}
2

Share this post


Link to post
Share on other sites

 

Sorry, the commented out lines are my failed attempt at vector. I just didn't want to get rid of them yet...hanging onto the string of faith thinking I'll figure this out :/.
I'm really not sure which constructor I even should use...I've never dealt with vectors before, and we haven't studied them yet.


Let me ask the question in another way: What did you want that line to do?

So you just have two text files and you want to combine their lines into an output file, but you want to use a vector to write the output sorted. Is that right? Do the files have the same number of lines? (implied by the way you wrote the loop...).

 

Yes, exactly. They do have equal number of lines. Thanks a million for the example. I'm gonna fool around with this. I find it about a million times easier to learn this language when I'm given an example which is relevant to my problems, because then I see for myself how things work rather than reading in a reference. :)

0

Share this post


Link to post
Share on other sites

I can see your code is much more efficient. But, I have to call each line one at a time. That's why I was having such a hard time converting to vector because there are certain elements I simply have to keep.

0

Share this post


Link to post
Share on other sites

I can see your code is much more efficient. But, I have to call each line one at a time. That's why I was having such a hard time converting to vector because there are certain elements I simply have to keep.

 

Sorry, that didn't make any sense. Can you please fully describe what you are trying to do?

2

Share this post


Link to post
Share on other sites

Haha yeah. I'm sorry I have literally been awake for two days because of finals and am just trying to finish this project. My brain isn't working to well at this point.

I need to merge two files  [done],

read each line of each data file (can't use any fancypants shortcuts built into a library to have the program read the entire file)[done],

Sort,

print to output file.

 

I wanted to just do some sort of while loop which would read the data from  a file into an array within the loop, but I'm not allowed ;(.

0

Share this post


Link to post
Share on other sites

I know I can sort it, but I can't figure out how to send the data from the first two files into an array...that's my big hold up.

0

Share this post


Link to post
Share on other sites

I guess I thought that it read all the lines from inputs at once rather than one at a time.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0