std::cout << "Error type: " << get_errortype();
Also, "%s" is only meant for null-terminated strings.
cout<< "Error type:" << get_errortype();
:ninja:
[QUOTE=Jallen;23897509]cout<< "Error type:" << get_errortype();
:ninja:[/QUOTE]
Why make his code worse?
[QUOTE=gparent;23897648]Why make his code worse?[/QUOTE]
How is this worse?
[QUOTE=Jallen;23899079]How is this worse?[/QUOTE]
It's got seemingly erratic whitespace, no explicit namespace prefix for the cout symbol, and isn't in code tags :v:
(also doesn't provide the same output as the printf version)
[QUOTE=jA_cOp;23897232]It depends what you mean by constant value. What programming language? Which situations are you referring to?[/QUOTE]
C++
const int SOMETHING
vs.
int something
any situation, when is it actually better to use a constant, since all you'd have to do is not touch the variable.
[QUOTE=The DooD;23902415]C++
const int SOMETHING
vs.
int something
any situation, when is it actually better to use a constant, since all you'd have to do is not touch the variable.[/QUOTE]
When you don't need to touch it, you don't make it a variable. Variables require memory, whereas constants can be optimized away at compile time.
[QUOTE=Jallen;23899079]How is this worse?[/QUOTE]
Well you took out std:: (??) and removed a space.
[QUOTE=esalaka;23903376]When you don't need to touch it, you don't make it a variable. Variables require memory, whereas constants can be optimized away at compile time.[/QUOTE]
It's even more useful in object-oriented programming, when you want to pass an entire object to a function by reference instead of by value (i.e. by copying), and make sure its state is not changed at all.
I tried messing around with an if statement and making a mock login screen, but when I try to input the username, it only takes the first letter and sometimes it breezes through the rest of the code once you hit enter. If you tap it really quickly, you can sometimes slow it down enough. Anyone know how to stop that?
[code]
int main()
{
int passchk, pass;
char username; //I really wasn't too sure if char was the right choice, so would anyone mind telling me if there's a better choice?
pass = 5235; //Sets passcode(oh fuck oh fuck im gonna be poor)
cout << "Please state username.\n";
cin >> username;
cout << "Hello "<< username <<".\n";
cin.get(); //Tried adding this in an attempt to slow it down when it speeds through the program.
cout <<"Please enter your passcode to gain access.\n";
cin >> passchk;
if ( passchk == pass ) { //Checks to see if passchk matches pass.
cout << "Welcome to the system, "<< username <<". Have a pleasant day."; //Friendly welcome. :D
}
else {
cout << "Passcode denied.";
}
cin.get();
return 0;
}
[/code]
Murkat, I would suggest learning data types [i]before[/i] you start messing around with if statements and cin/cout.
char only holds a single character, which is the root of your problem. You want to use either an array of char (char[]), or an std::string.
The reason that your program "breezes through" is that since your first cin is only extracting a single character from the input buffer, your next cin doesn't take any more input from the user, since there is still some input in the buffer. Your fix worked because cin.get() is extracting all the rest of the input and throwing it away.
Something else I should mention about cin: it stops extracting when it encounters a space. So even if you fix your program to extract strings instead of single characters, if you enter a username like "bob hope", the first cin will only take "bob" from that, and "hope" will still be left in the input buffer. cin.getline is the best way to get an entire line of input into a string.
Accepting integers from cin is problematic too I believe, it causes the int to be filled with the ASCII value of the character, not the character itself, eg entering 1 will result in the integer filled with 49.
[QUOTE=arienh4;23904234]Accepting integers from cin is problematic too I believe, it causes the int to be filled with the ASCII value of the character, not the character itself, eg entering 1 will result in the integer filled with 49.[/QUOTE]
No, the cin extraction operator correctly translates numerals.
[QUOTE=shill le 2nd;23903812]It's even more useful in object-oriented programming, when you want to pass an entire object to a function by reference instead of by value (i.e. by copying), and make sure its state is not changed at all.[/QUOTE]
That's immutability, which is similar, but different to using a constant.
[QUOTE=shill le 2nd;23904051]Murkat, I would suggest learning data types [i]before[/i] you start messing around with if statements and cin/cout.
char only holds a single character, which is the root of your problem. You want to use either an array of char (char[]), or an std::string.
The reason that your program "breezes through" is that since your first cin is only extracting a single character from the input buffer, your next cin doesn't take any more input from the user, since there is still some input in the buffer. Your fix worked because cin.get() is extracting all the rest of the input and throwing it away.
Something else I should mention about cin: it stops extracting when it encounters a space. So even if you fix your program to extract strings instead of single characters, if you enter a username like "bob hope", the first cin will only take "bob" from that, and "hope" will still be left in the input buffer. cin.getline is the best way to get an entire line of input into a string.[/QUOTE]
I recommend the std::string over the char array.
std::cin.get() only gets a single character. std::cin.ignore(std::numeric_limits<std::streamsize>::max()) will dispose of all characters in the buffer.
[QUOTE=turb_;23909947]That's immutability, which is similar, but different to using a constant.[/QUOTE]
For C++ that is immutability, since the variable may not constant, just the reference to it.
I need some tips on optimizing tile rendering.
I have a 1000x1000x1 map, currently the only optimization I have in place is limiting any tiles in view being drawn, so say I have an 800x800 window I can draw 51^2 tiles, so I only draw those 51^2 tiles based on the offset of the top-down camera. The other implementation is based on the Z-Value, my system finds the top most opaque tile and makes sure nothing below it is drawn. So with these optimizations I get roughly 400 - 500 on a map with only 1 value of depth, but for every z level I add on top of that I'm basically cutting the framerate in half.
So any optimization techniques you guys have would be awesome. I'm using SFML and a multi_array from boost for my Tile Array.
[QUOTE=FerrisWheel;23868029]I'm attempting to make a simple drawing program, but whatever library I am using seems to be too slow to draw a circle at every point when the mouse moves, so I'm thinking I will need to get the last mouse point since last render, and the current mouse point this render, and draw a circle at every point in between (whole integer). Anyone able to show me how to apply this in C++?[/QUOTE]
Alright, I've used Bresenham's line algorithm and it's all dandy and working, but I want to draw a circle at each point along the line (rather than just a 1 pixel width line). I've got some code that theoretically works, but is EXTREMELY laggy and inefficient. Is somebody able to show me a more efficient way of doing this?
[cpp]struct DrawInfo { //struct for parameter of function
int x;
int y;
int radius;
sf::Color col;
};
sf::Color pixels[800][600]; //array of sf::Color's to be used as the pixels for the sprite, screen size 800*600
void DrawCircle(DrawInfo Draw)
{
int dx, dy;
for(int w=0; w<800; w++)
{
for(int h=0; h<600; h++)
{
dx = w - Draw.x;
dy = h - Draw.y;
if(dx*dx + dy*dy <= Draw.radius*Draw.radius)
{
pixels[w][h] = Draw.col;
}
}
}
}[/cpp]
I managed to track down a struct for a very obscure file type. How would I parse in a file of that format as an instance of the struct? I've never dealt with files this way, only parsing into a byte array and stripping off a header.
And would this be possible with C#? The code I found is written in C++ and contains nothing to do any actual parsing (from what I understand of it)
[cpp]#define XBG_MAGIC (((DWORD)'X'<<0)|(((DWORD)'B'<<8))|(((DWORD)'G'<<16))|(2<<24))
struct XBG_HEADER
{
DWORD dwMagic;
DWORD dwNumMeshFrames;
DWORD dwSysMemSize;
DWORD dwVidMemSize;
};
struct XBMESH_FRAME
{
D3DXMATRIX m_matTransform;
XBMESH_DATA m_MeshData;
CHAR m_strName[64];
XBMESH_FRAME* m_pChild;
XBMESH_FRAME* m_pNext;
struct XBMESH_DATA
{
D3DVertexBuffer m_VB;
DWORD m_dwNumVertices;
D3DIndexBuffer m_IB;
DWORD m_dwNumIndices;
DWORD m_dwFVF;
DWORD m_dwVertexSize;
D3DPRIMITIVETYPE m_dwPrimType;
DWORD m_dwNumSubsets;
XBMESH_SUBSET* m_pSubsets;
};
};
struct XBMESH_SUBSET
{
D3DMATERIAL8 mtrl;
LPDIRECT3DTEXTURE8 pTexture;
CHAR strTexture[64];
DWORD dwVertexStart;
DWORD dwVertexCount;
DWORD dwIndexStart;
DWORD dwIndexCount;
};[/cpp]
It's an xbox-based .x file type, which somehow shows up in the PC version of Far Cry 2.
[QUOTE=robmaister12;23913099]The code I found is written in C++ and contains nothing to do any actual parsing (from what I understand of it)[/QUOTE]
Generally when you find a struct like that, it means you'll just have to read everything in the file into the struct simply from top to bottom. Usually they are accompanied by any possible extra information on parsing.
[editline]12:22PM[/editline]
Also, isn't .x a DirectX model?
[QUOTE=esalaka;23913146]Generally when you find a struct like that, it means you'll just have to read everything in the file into the struct simply from top to bottom. Usually they are accompanied by any possible extra information on parsing.
[editline]12:22PM[/editline]
Also, isn't .x a DirectX model?[/QUOTE]
yeah, .xbg is supposed to be a slightly modified xbox version of .x, but after looking through one of the extracted .xbg's from Far Cry 2 in a hex editor, it looks like ubisoft modified it quite a bit, and decided not to use a standard header and whatnot.
It also seems like they flipped words, like HSEM* at the beginning, EDON, SDOL, etc.
I don't have the strongest knowledge in model filetypes, but I guess I'm going to have to learn eventually..
[QUOTE=robmaister12;23913201]It also seems like they flipped words, like HSEM* at the beginning, EDON, SDOL, etc.
[/QUOTE]
Everything probably appears flipped because the xbox uses a PowerPC based processor, which is big endian. Whereas an intel x86/x86_64 based processor is little endian. The reason it's flipped is most likely due to your hex editor loading the file and displaying it as though it were little endian. If you were to reverse the entire file's byte order, you might be able to extract a bit more info.
(Incidentally, Ubisoft most likely has a function or two to automatically reverse the bytes as they are reading while the game is running. It isn't anything complex, and there are many many examples out there. It is also a *very* common practice for AAA studios to do something like this, where data is stored in one format, and optimized for the more popular platform, in this case the Xbox 360 and PS3, which is also big endian)
I'm having trouble with a simple function. The compiler is complaining that the function "getWords" hasn't been defined:
[cpp]istream& getWords(istream& in, vector<string>& v)
{
string x;
while (in >> x)
v.push_back(x);
if (v.size() == 0)
throw domain_error("empty vector of words");
return in;
}
int countWords(vector<string>& v)
{
return v.size();
}
int main()
{
cout << "Please enter a list of words followed by end-of-file:" << endl;
//count a list of words from the in-stream
vector<string> words;
getWords(cin, words);
cout << "\nThere are " << countWords(words) << " words in the vector!"
<< endl;
return 0;
}[/cpp]
But I don't understand why.
[QUOTE=nos217;23914010]I'm having trouble with a simple function. The compiler is complaining that the function "getWords" hasn't been defined:
But I don't understand why.[/QUOTE]
What is the exact error?
'getWords' was not declared in this scope.
And you have included <string> and <iostream> and are using namespace std, right?
Yeah.
[cpp]#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using std::cout; using std::vector;
using std::cin; using std::string;
using std::endl;
struct wordAppearance {
string word;
int timesAppeared;
};
// function to get a list of words from the input stream
// and store them in a vector
istream& getWords(istream& in, vector<string>& v)
{
string x;
while (in >> x)
v.push_back(x);
if (v.size() == 0)
throw domain_error("empty vector of words");
return in;
}
int countWords(vector<string>& v)
{
return v.size();
}
int main()
{
cout << "Please enter a list of words followed by end-of-file:" << endl;
//count a list of words from the in-stream
vector<string> words;
getWords(cin, words);
cout << "\nThere are " << countWords(words) << " words in the vector!"
<< endl;
return 0;
}[/cpp]
How about using std::istream; :downs:
Oh of course! Thanks.
[QUOTE=nos217;23914424]Oh of course! Thanks.[/QUOTE]
What kind of awful compiler are you using that didn't tell you istream didn't exist when defining getWords?
(I'm guessing it did tell you, but you missed it)
It did, but for some reason my eyes just skipped over it :S. Now I look back, I see it.
Sorry, you need to Log In to post a reply to this thread.