Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: std::cout showing random bits and pieces of other strings?  (Read 4410 times)

0 Members and 1 Guest are viewing this topic.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
std::cout showing random bits and pieces of other strings?
« on: November 18, 2012, 01:52:36 am »
So I was just messing around to day as I usually do, when my console started looking a bit... strange.

For some reason my cout in this code:

Shortened it up, Frex's answer worked.


I have attached a example of what the error looks like below. You can see what I mean hopefully. The strings that its showing at random are in my main class, and they are never even called, so thats why I'm thinking it may be memory related.

I really look forward to some guidance on this issue. ....I'm lost.

EDIT

Also, it will print "Printing the DevHud Data" Every time it goes through the loop. That was intentionally done so I could keep track and see how much was being printed out per loop.

EDIT

Also those strings its showing are declared here:

void Genesis::ValidateFlags()
{
        if((Genesis::WindowX > 0 && Genesis::WindowY <= 0) || (Genesis::WindowY > 0 && Genesis::WindowX <= 0)) //Checks to make sure both X and Y are correct.
        {
                ConsoleLog.error("\"-X\" and \"-Y\" must be used at the same time, have a value greater than 0, and be proper integers!");
                ConsoleLog.error("\"-X\" and \"-Y\" will be ignored.");
                if(Windowed)
                {
                        ConsoleLog.error("\"-WM\" Was dected but due to argument errors will be disabled.");
                        Windowed = false;
                }
        }
        if((Genesis::WindowX>0)&&(Genesis::WindowY>0)&&(Genesis::Windowed==false)) //Makes sure that windowed mode is enabled when X and Y are correct.
        {
                ConsoleLog.error("\"-X\" and \"-Y\" were declared but windowed mode was never set! Did you forget a \"-WM\" ?");
                ConsoleLog.error("\"-X\" and \"-Y\" will be ignored.");
        }
        if((Genesis::WindowX<=0)&&(Genesis::WindowY<=0)&&(Genesis::Windowed==true)) //Make sure that -WM is not missing a X/Y variable!
        {
                ConsoleLog.error("Windowed mode was declared but X and Y are invalid or missing!");
                ConsoleLog.error("-WM will be ignored.");
                Genesis::Windowed=false;
        }
}
 
[/spoiler]
But it can pretty much show anything I think, but it usually shows that stuff.

[attachment deleted by admin]
« Last Edit: November 18, 2012, 02:36:51 am by Flash619 »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::cout showing random bits and pieces of other strings?
« Reply #1 on: November 18, 2012, 02:15:38 am »
Didn't read whole, anyway, stuff like:
"blablabla"+some_number; is WRONG.
Add this function to your program and see if wrapping every number in it(as in, don't do +number, do +EEVarToStr(number) instead everywhere) will stop the problem.
template <typename T> std::string  EEVarToStr(T targ)
{
    std::stringstream ss;
    ss<<targ;
    return ss.str();
}
Back to C++ gamedev with SFML in May 2023

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: std::cout showing random bits and pieces of other strings?
« Reply #2 on: November 18, 2012, 02:19:56 am »
Didn't read whole, anyway, stuff like:
"blablabla"+some_number; is WRONG.
Add this function to your program and see if wrapping every number in it(as in, don't do +number, do +EEVarToStr(number) instead everywhere) will stop the problem.

Worked perfectly!

Haha, I actually was wondering about the whole "+" thing. I used to do that in java so maybe I just forgot I was in C++ for a moment? ;)

So what exactly does that function you wrote up do? It looks like it is casting a number to a stringstream then returning the string value of that stringstream. Correct?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::cout showing random bits and pieces of other strings?
« Reply #3 on: November 18, 2012, 02:28:26 am »
"Text between quotation marks like that" is a char * that points to c style string, adding numbers to pointers increment them and your compiler decided to put all constant strings near each other so you drove your pointer somewhere into other string in memory. That is bad.
Quote
So what exactly does that function you wrote up do? It looks like it is casting a number to a stringstream then returning the string value of that stringstream. Correct?
Not 'casting'
Read this answer:
http://www.stroustrup.com/bs_faq2.html#int-to-string
Back to C++ gamedev with SFML in May 2023

ZakMan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: std::cout showing random bits and pieces of other strings?
« Reply #4 on: December 02, 2012, 03:03:50 pm »
OK, I have a similar problem right now, trying to display the mouse's coordinates in a RenderWindow. This means I have to put them into a std::string and then set this string to a sf::Text. But trying to use your function results in these error messages:

D:\Projects\DoesItWork\main.cpp||In function 'std::string EEVarToStr(T)':|
D:\Projects\DoesItWork\main.cpp|9|error: invalid use of incomplete type 'std::stringstream {aka struct std::basic_stringstream<char>}'|
d:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\iosfwd|107|error: declaration of 'std::stringstream {aka struct std::basic_stringstream<char>}'|
D:\Projects\DoesItWork\main.cpp||In function 'std::string EEVarToStr(T) [with T = int, std::string = std::basic_string<char>]':|
D:\Projects\DoesItWork\main.cpp:55|49|instantiated from here|
D:\Projects\DoesItWork\main.cpp|7|error: 'ss' has incomplete type|
D:\Projects\DoesItWork\main.cpp|10|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 3 errors, 1 warnings ===|

Any tips?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::cout showing random bits and pieces of other strings?
« Reply #5 on: December 02, 2012, 03:28:40 pm »
included <sstream>?
Back to C++ gamedev with SFML in May 2023

ZakMan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: std::cout showing random bits and pieces of other strings?
« Reply #6 on: December 02, 2012, 04:15:36 pm »
Oh... I'm an idiot :X Now it worked! Thanks!