• What do you need help with? Version 1
    5,001 replies, posted
Your entity class should never need to know about the classes that are derived from it. vec[i] gives you the object you stored, if you had a vector of Entity*'s you'd want your function to take an entity pointer.
C++0x random is messing with me: [cpp]#include <random> #include <iostream> int main() { RandomNumberGenerator randomGenerator(std::mt19937(), std::uniform_real<double>(0, 1)); std::uniform_real<double> distributor(0, 1); std::mt19937 generator; std::cout << randomGenerator() << '\n' //okay, random number in [0..1] << randomGenerator.distribution()(randomGenerator.engine()) << '\n' //okay, random number in [0..1] << distributor(generator) << '\n' //5.81869e+08 << distributor(randomGenerator.generator()) << '\n' //okay, random number in [0..1] << randomGenerator.distribution()(generator) << std::endl; //3.49921e+09 }[/cpp] What is wrong with my generator to fuck up my distribution?! If I do auto generator(randomGenerator.engine()); the output is normal.
I have inherited from sf::Shape and added mass and speed to the class. What constructor should i use? .Circle()? or MyBall() ? At the moment i have [cpp]MyBall ball = MyBall::MyBall(24.0f, 0.0f); ball.Circle(50.0f, 50.0f ,20.0f, sf::Color(240, 240, 0));[/cpp] But it isn't drawing to th render window. ( and yes i do have App.Draw(ball) ) BTW if i use MyBall ball = MyBall::Circle(...); i get main.cpp|16|error: conversion from &#8216;sf::Shape&#8217; to non-scalar type &#8216;MyBall&#8217; requested| [editline]11th November 2010[/editline] [QUOTE=ZeekyHBomb;25989725]C++0x random is messing with me: [cpp]<< randomGenerator.distribution()(generator) '\n' //3.49921e+09 [/cpp] What is wrong with my generator to fuck up my distribution?! If I do auto generator(randomGenerator.engine()); the output is normal.[/QUOTE] Arent you missing a ; at the end of that?
Whoops yeah :P Got that in my file, dunno how to didn't get here o_O Anyways, that's not the problem. Looking on replacing libstdc++ with libc++. Then I can also use clang instead of gcc for C++0x :D
Ughh this is giving me a headache. What would be the best way to do this? [QUOTE=shill le 2nd;25964740]I don't think you should be creating an entity that inherits from a shape. Just make the shape a member.[/QUOTE]
I'm not entirely sure why you're getting that result, but I do see a few errors that it could be attributed to! Mainly, you should use single quotes when setting char values, plus several equality tests are missing an equals sign (eg. should be placeholder[i][x]==1). Also, your loop looks like x will go out of bounds - using two layers of for loops would make it easier! Here's the code with those changes: [cpp]int main() { char field[6][5]; int placeholder[6][5]; for(x=0;x<5;x++) { for(int i=0;i<6;i++) { if(placeholder[i][x]==1) { field[i][x] = 'X'; } if(placeholder[i][x]==2) { field[i][x] = '0'; } if(placeholder[i][x]==0) { field[i][x] = '-'; } } } cout << "The field of play: " << endl; cout << "||1|2|3|4|5|6|7||" << endl; cout << "/////////////////" << endl; cout << "||" << field[6][5] << "|" << field[5][5] << "|" << field[4][5] << "|" << field[3][5] << "|" << field[2][5] << "|" << field[1][5]<< "|" << field[0][5] << "||" << endl; cout << "||" << field[6][4] << "|" << field[5][4] << "|" << field[4][4] << "|" << field[3][4] << "|" << field[2][4] << "|" << field[1][4]<< "|" << field[0][4] << "||" << endl; cout << "||" << field[6][3] << "|" << field[5][3] << "|" << field[4][3] << "|" << field[3][3] << "|" << field[2][3] << "|" << field[1][3]<< "|" << field[0][3] << "||" << endl; cout << "||" << field[6][2] << "|" << field[5][2] << "|" << field[4][2] << "|" << field[3][2] << "|" << field[2][2] << "|" << field[1][2]<< "|" << field[0][2] << "||" << endl; cout << "||" << field[6][1] << "|" << field[5][1] << "|" << field[4][1] << "|" << field[3][1] << "|" << field[2][1] << "|" << field[1][1]<< "|" << field[0][1] << "||" << endl; cout << "||" << field[6][0] << "|" << field[5][0] << "|" << field[4][0] << "|" << field[3][0] << "|" << field[2][0] << "|" << field[1][0]<< "|" << field[0][0] << "||" << endl; cout << "/////////////////" << endl;[/cpp]
[QUOTE=bootv2;26012668]allright thanks almost works now. the only problem I'm encountering right now is in field[6] [*] (* can be every possible value) and in field[5][5] theres some strange character being showed. I think it is the "¬" character. also the field[y][x] in the "for(x=0;x<5;x++)" loop is only set once. not every time the main gameloop(which isn't showed in the posted code) passes it. and yes, x is ste to 0 everytime before code reaches the "for(x=0;x<5;x++)" loop.[/QUOTE] That's because you are going out of bounds of the array, the array you have declared is 6*5 elements, yet you are outputting 7*6 elements.
Hey guys can any of you work out why my program crashes when I try to assign the return value of the function rand() to r_number. I am trying to make a C++ console program which asks the user a maths question. I'm pretty knew to coding though so its probably something obvious, Thanks [code]#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int random(int max, int min); void question(int x,int y,char z); int main() { srand((unsigned)time(NULL));//Seed Random bool userWantsToPlay = true; while (userWantsToPlay) { int x = random(1,100); int y = random(1,100); char z; int r_number = random(0,3); if (r_number == 0) { z = '+'; } else if (r_number == 1) { z = '-'; } else if (r_number == 2) { z = '*'; } else { z = '/'; } question( x, y, z); } return 0; } int random(int max, int min) { int r_number = 0 ; if ((max == 0) && (min == 0)) { r_number = 0; } else { r_number = ((rand() % max) + min); //TODO:Random number here between min and max. Crashes when I do ((rand() % max) + min); instead of 0;... } return r_number; } void question(int x,int y,char z) { cout << "What does " << x << z << y << " equal?" << endl; int a; cin >> a; if (z == '+') { if (a == (x + y)) { cout << a << " is correct!" << endl; } } else if (z == '-') { if (a == (x - y)) { cout << a << " is correct!" << endl; } } else if (z == '*') { if (a == (x * y)) { cout << a << " is correct!" << endl; } } else if (z == '/') { if (a == (x / y)) { cout << a << " is correct!" << endl; } } else { cout << a << " is incorrect!" << endl; } cout << endl; }[/Code]
What's the error message it crashes with? I don't see why it should.
[QUOTE=Sartek;26013351]Hey guys can any of you work out why my program crashes when I try to assign the return value of the function rand() to r_number. I am trying to make a C++ console program which asks the user a maths question. I'm pretty knew to coding though so its probably something obvious, Thanks <Snip>[/QUOTE] You're trying to divide by zero. (Well, Modulo by zero.) Assumption: You're not changing the parameters of the Random() call and are instead commenting out the first part of the if statement in Random. Either way, your random function is wrong - It should be: [code]rand() % ( Maximum - Minimum ) + Minimum[/code] (With appropriate sanitisation.) Or change the Maximum parameter to the name Range.
[QUOTE=esalaka;26013383]What's the error message it crashes with? I don't see why it should.[/QUOTE] There is absoulutely no error message in my ide. I compile it fine then run the exe it pauses for half a second then comes up with a this program has stopped responding message. Could it have anything to do with the never ending while loop? I doubt it though because it only crashes when I do "r_number = ((rand() % max) + min);" Edit1:Oh fuck Thanks :D Well I might go to bed now not thinking properly and too tired to code. Edit2:Just realised I was taking the min and max inputs in reverse. Edit3:What exactly does "fflush( stdin );" do?
[QUOTE=Sartek;26013458]There is absoulutely no error message in my ide. I compile it fine then run the exe it pauses for half a second then comes up with a this program has stopped responding message. Could it have anything to do with the never ending while loop? I doubt it though because it only crashes when I do "r_number = ((rand() % max) + min);" Oh fuck Thanks :D Well I might go to bed now not thinking properly and too tired to code.[/QUOTE] This is the point where I would say add the following above the cin line: [code]fflush( stdin );[/code] This is also the point where a load of pedantic idiots would jump in and despite the code working fine would point out that it's wrong, broken, stupid and undefined. You should then decide if you want the program to actually do what you intended, or to be "correct" but not do its job. The reason it is failing is because you have a return character in the input stream after the first value is input. As a result the cin line doesn't block after its first use.
[QUOTE=bootv2;26013653]no, I'm declaring a [6][5] array which means when counting normally this would mean 7*6. since the compiler starts counting from 0. so I'm not accessing beyond the boundries. I'm accessing [6] of [6] or in normal counting number six of numer six.[/QUOTE] I'm kind of confused with the way you are wording that, especially when you specify the 7 * 6. If I'm miss-reading what you just said then sorry, but I think you have things mixed up. If you have a 5 element array: [code]int Array[5];[/code] You can access the elements 0 -> 4 inclusive: (A total of 5 elements.) [code]Array[0] Array[1] Array[2] Array[3] Array[4][/code]
[QUOTE=yngndrw;26013545]pedantic idiots[/QUOTE] Well ... thanks :| Also, doesn't work on my system. [editline]12th November 2010[/editline] [QUOTE=bootv2;26013951]I can access field[*][5] without problems while it's an "char field[6][5]"[/QUOTE] Happened by chance. You see, the behavior of this is undefined - as such it could do what you intended, but just as well it could do something else.
The reason why that works is because the array is actually a single dimensional array in memory. For example if you had an array: [code]int Array[2][2];[/code] It would be stored like this in memory: [code]Array[0][0] Array[0][1] Array[1][0] Array[1][1][/code] When you try to access [*][5], or in the example I've given [*][2] it will overflow into other rows. E.g. Array[0][2] would actually access the memory at Array[1][0]. When you try and access [6][*], you are trying to access memory outside of the block you have defined which could be anything. [editline]12th November 2010[/editline] [QUOTE=ZeekyHBomb;26014017]Well ... thanks :| Also, doesn't work on my system.[/QUOTE] I'm happy for people to point out that it's undefined / wrong if they also post both: 1) What situations the code does not work in. 2) Code which fixes said problem. The problem comes when people come jumping in shouting about how it makes you the worst person alive and how you're a maggot, yet don't actually provide any real information or solutions - That's what really annoys me. I love you really though - You at least half-answered part 1 of the above.
1) I dunno. It doesn't work on my system (Linux 2.6.something). 2) Do std::sin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after you get some input. It basically will discard any character in the buffer (since it cannot hold more than the maximum value of std::streamsize) up to and including a new line-character.
I'm looking to rotate something depending on the X movement of the mouse, but not quite sure how I would do it. Would I have it get the amount moved, and set it back to it's default position every frame?
Yup, that's the general idea. FPS games do it like that.
I'm try to learn to use SFML.NET, but I can't even use the first example program. I have all three .dlls referenced and all that, but when I try to compile it, it complains about not being able to find some other dll. This dll comes with it, but there is nothing in the code sample about it. I tried adding it as a reference to see if that helped, but it crashed vcs. The error traced back to the RenderWindow function, but that's doesn't sound like it could be causing it. Is there something obvious I'm missing?
[QUOTE=ZenX2;26026308]I'm try to learn to use SFML.NET, but I can't even use the first example program. I have all three .dlls referenced and all that, but when I try to compile it, it complains about not being able to find some other dll. This dll comes with it, but there is nothing in the code sample about it. I tried adding it as a reference to see if that helped, but it crashed vcs. The error traced back to the RenderWindow function, but that's doesn't sound like it could be causing it. Is there something obvious I'm missing?[/QUOTE] I think that you also need to copy some dlls next to your exe for it to work. They are in the SFML package I just don't remember which ones exactly.
You have to reference the sfmlnet-xxxxxx DLLs, and you must also have the csfml-xxxxxxx DLLs next to your executable
[CODE] #include <cstdlib> #include <iostream> using namespace std; int main() { int a; int b; int x; cout<<"wellcome to the blahblah\n"; for ( int x= 0; x < 3; x++ ) { cout<<"Please enter a number\n"; cin>> a; cin.ignore(); cout<<"You entered: "<< a <<"\n"; cout<<"Is this correct?\n"; cin>> b; if ( b == 1 ) { if ( a < 10 ) { cout<<"good job\n"; } else { cout"Try agian\n"; cout<< x <<endl; } } else { cout<< x <<endl; } cout<<"Too many attempts, Exiting program.\n" cout<<"YOU LOSE.\n" return EXIT_SUCCESS; } [/CODE] getting a expected ";" before string constant on line 24. Any ideas?
Some cout's are missing ; at the end of them. [editline]13th November 2010[/editline] And don't use cstdlib when you already include iostream
[QUOTE=sim642;26033178]And don't use cstdlib when you already include iostream[/QUOTE] What's wrong with that?
[QUOTE=Phreebird;26033040] -code snip- getting a expected ";" before string constant. Any ideas?[/QUOTE] You should post the full error when asking for help. You should also consider using [noparse][cpp][/cpp][/noparse] tags for C++ code (and other C-likes) to make it easier to read (= more people solving the problem for you). Here's an annotated version of your program including the solution. [cpp] #include <cstdlib> #include <iostream> int main() { using namespace std; // Try not to put this at global scope // The unused 'x' variable was declared here, but is redeclared in the loop // Shadowing local variables is bad practice cout << "wellcome to the blahblah\n"; for ( int x = 0; x < 3; x++ ) { cout << "Please enter a number\n"; int a; // Declare variables as late as possible cin >> a; cin.ignore(); cout << "You entered: " << a << "\n"; cout << "Is this correct?\n"; int b; cin >> b; if ( b == 1 ) { if ( a < 10 ) { cout << "good job\n"; } else { cout << "Try again\n"; // Missing '<<' here caused the error (Also, it's spelled "again") cout << x << endl; } } else { cout << x << endl; } cout << "Too many attempts, Exiting program.\n"; // Missing semicolon cout << "YOU LOSE.\n"; // Missing semicolon // At the end of main, return 0 is implicit // Feel free to use the explicit version for stylistic reasons, of course }[/cpp] I also made spacing between operators and bracket placement more uniform. Consistency is important when writing code to make it easier to read. This might not mean a lot to you right now, but it also makes it easier to read for those who try to help you, which is a big plus for everyone.
[QUOTE=jA_cOp;26033359]You should post the full error when asking for help. You should also consider using [noparse][cpp][/cpp][/noparse] tags for C++ code (and other C-likes) to make it easier to read (= more people solving the problem for you). Here's an annotated version of your program including the solution. I also made spacing between operators and bracket placement more uniform. Consistency is important when writing code to make it easier to read. This might not mean a lot to you right now, but it also makes it easier to read for those who try to help you, which is a big plus for everyone.[/QUOTE] Thank you so much, and i will use the cpp tags in the future. Thanks again.
[QUOTE=Chris220;26027267]You have to reference the sfmlnet-xxxxxx DLLs, and you must also have the csfml-xxxxxxx DLLs next to your executable[/QUOTE] I figured it out, I had the csfml ones near the project, not the exe
In .NET, when I write a UInt16 to a stream, is the most significant byte written first or second?
[QUOTE=Robert64;26038095]In .NET, when I write a UInt16 to a stream, is the most significant byte written first or second?[/QUOTE] Little-endian, so LSB first and MSB last. [url]http://msdn.microsoft.com/en-us/library/091b1s67.aspx[/url] You can use BinaryWriter if you need to specify the encoding and endianness. You can get the platform's endianness using BitConverter.IsLittleEndian, however I'm not sure if it affects the stream read / write methods - The MSDN page just specifies little-endian. [b]Edit[/b]: Sorry got my pages messed up - It only specifies the default for BinaryWriter, however there is no Stream.Write method which takes a UInt16 so it depends upon which order you're using if you're writing it by hand.
[QUOTE=yngndrw;26038422]Little-endian, so LSB first and MSB last.[/QUOTE] No, exactly the other way around.
Sorry, you need to Log In to post a reply to this thread.