Thread: Duplicated Coord in a stringstream?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Question Duplicated Coord in a stringstream?

    This is weird, I make a program so that you can place characters anywhere in the console, then you can save it. But, when it writes the characters coords to a file, it just writes the same coord over and over and over. (Four times) I used stringstreams, which I have had basicaly no practice with.

    Heres my save code:
    Code:
    //at confirm
    int ATCONFIRM() {
     int x;
    
     ClearAsyncArrowBuffer(); //This just checks if any arrows are down and returns nothing
    //It just clears the arrow buffer, which I had trouble with before using this to clear it.
    
     for(x=0; x <= 200; x++)
      if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
       return 1;
      else
       Sleep(10);
    
     return 0;
    } 
    
    int Save() {
     string info;
     string PATH;
     stringstream ss;
     int I = 0;
    
     if(!ATCONFIRM())
      return 0;
    
     PATH="C:\\"; //Saves to c drive.
     PATH+=Ext[0]; //Ext is a global char-variable.
     PATH+=".bsf"; //Openable as a text file in notepad.
    
     ofstream file_out(PATH.c_str());
    
     if(!file_out) {
      MessageBox(NULL, "Error[2], could not open file!", "Error[2]", MB_OK);
      return -1;
     }
    
     ss << TE[I].X;
     ss >> info;
     file_out << info;
     ss << TE[I].Y;
     ss >> info;
     file_out << info;
    
     ss << TD[I].X;
     ss >> info;
     file_out << info;
     ss << TD[I].Y;
     ss >> info;
     file_out << info;
    
     file_out.close();
     MessageBox(NULL, "File Saved Successfuly.", "Sucess!", MB_OK);
     return 1;
    }
    TE and TD each hold the character information, which is collected elsewhere. I'm pritty confident that I coded there positions correctly, however if nothing here is amiss I can post the code that gathers them... But its pritty long, and quite ugly. I have a feeling I didint use stringstreams proporely. But I've only used them once or twice before, so I'm really unsure how to use them. (Do I need to clear them before I can add new data or something?) The file itself is written without any problem though.

    Thanks for your time! .
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Do I need to clear them before I can add new data or something?
    Probably. When you empty the stringstream, it might be setting the eofbit. Try calling clear() and str("") after each use of ss, or better yet make a function that does the work so you that a new stringstream will be created each time and you won't have to worry about clearing it.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    I don't see where you are incrementing i, which appears to be a global. You also don't appear to have a need for a string stream at all.
    Code:
    fileout << TD[i].X << ", " << TD[i].Y << '\n';
    will write the cord quite nicely, to read you would want
    Code:
    char comma;
    if(filein >> x >> comma >> y && comma == ',') {
    //  Process a cord
    } else {
       std::cerr << "Parse Error" << std::endl;
    }
    Adding a bit of syntax to your file format makes it easyer to catch if the file becomes courrupted and you start reading y's for x's and so forth. Start thinking about making cords read and write themselves and wrapping up the array of cords into it's own class. This program is fine for just learning but it does make life a good bit easyer to start keeping things in thier own sections(encapsulation)

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Why bother with the coma? just

    fileout<< thing.x << " " << thing.y << '\n';

    then

    filein >> thing.x >> thing.y;

    no?

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah, I didint realize you could write integers directly to files -,-. I doesnt increment yet, this was just a test to see if it would save proporely, now that it does it makes things easier .

    Thanks!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream problem
    By black_spot1984 in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2008, 05:45 PM
  2. stringstream clear
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 01:03 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM
  5. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 12:02 PM