I used to write IRC stuff in TCL back when I was a l33t d00d.
Then I grew hair on my balls and got interested in girls.
Were you one of the 10 people besides me who actually used XiRCON?
I knew a few people who used it. All of them were terrifying people.
In fairness, it wasn't that outdated when I was most active on IRC (1998-2003ish), but the channels I hung out also had their own script that ran on top of it that made all super leet.
Here's an easier question: What is the correct way to properly do command-line interactive programs in C. I feel like the way I normally do it is incredably fragile...
Here was I was working on tonight. It's homework, where we are simulating an ALU. The test program here isn't really the main point of the project, so I don't feel bad asking about it. scanf is such a house of cards sometimes.
int main(void) {
RegisterFilePtr rf = registerFile_initialize();
RegisterPtr status = register_initialize();
Alu alu = alu_initialize(rf, status);
printf("%s", registerFile_toString(rf));
int running = TRUE;
int input = 0;
int value = 0;
int error = NO_ERROR;
while (running == TRUE) {
printf("What would you like to do?\n");
printf("\t1: Print register file.\n");
printf("\t2: Set a register.\n");
printf("\t3: Perform an ALU operation.\n");
printf("\t0: Exit.\n:");
//read input
if (scanf("%i", &input) == 1){
//determine what to do
switch (input){
//Exit
case 0: running = FALSE; break;
//Print register file
case 1: printf("%s", registerFile_toString(rf)); break;
//Set register
case 2:
//get register choice
printf("Which register to set?\n:");
if (scanf("%i", &input) != 1){printf("What?\n"); break;}
//get register value
printf("\nInput new value.\n:");
if (scanf("%i", &value) != 1){printf("What?\n"); break;}
//Perform register setting
error = registerFile_putRegValue(rf, (uchar) input,
(uchar) value);
//report results
if (error == NO_ERROR){
printf("\nRegister $%X set to %04X\n", input, value);
} else {
printf("\nError!\n");
}
break;
case 3:
//get operation choice
printf("Which ALU operation would you like to perform?\n");
printf("ADD:\t0x0\t(A+B->R)\n");
printf("SUB:\t0x1\t(A-B->R)\n");
printf("NEG:\t0x2\t(-A->R)\n");
printf("MUL:\t0x3\t(A*B->[R8,R9])\n");
printf("DIV:\t0x4\t(A/B->RA, A%B->RB)\n");
printf("AND:\t0x5\t(A&B->R)\n");
printf("OR:\t0x6\t(A|B->R)\n");
printf("XOR:\t0x7\t(A^B->R)\n");
printf("NOT:\t0x8\t(~A->R)\n");
printf("SFL:\t0x9\t(A<<1->R)\n");
printf("SFR:\t0xA\t(A>>1->R)\n");
if (scanf("%i", &input) != 1){printf("What?\n"); break;}
//get first operand
printf("Which register is first operand?\n:");
if (scanf("%i", &value) != 1){
printf("What?\n"); break;
} else {
error = alu_loadFromReg(alu, value, $RALUA);
if (error == NO_ERROR){
printf("\nLoaded ALU register A with value");
printf(" in register $R%X\n", value);
} else {
printf("\nError(%X)\n", error);
}
}
//get second operand
int choice;
printf("Load second operand from register (0)\n");
printf("Or load an immediate value? (1)\n:");
if (scanf("%i", &choice) != 1){printf("What?"); break;}
//Load B from register
if (choice == 0){
printf("Which register is second operand?\n:");
if (scanf("%i", &value) != 1){
printf("What?\n"); break;
} else {
error = alu_loadFromReg(alu, (uchar) value, $RALUB);
if (error == NO_ERROR){
printf("\nLoaded ALU register B with value");
printf(" in register $R%X\n\n", value);
} else {
printf("\nError(%X)\n", error);
}
}
} else {//Load B from immediate
printf("What value for second operand?\n:");
if (scanf("%i", &value) != 1){
printf("What?\n"); break;
} else {
error = alu_loadFromImmediate(alu, (ushort) value);
if (error == NO_ERROR){
printf("\nLoaded ALU register B with value");
printf("%04X\n\n", value);
} else {
printf("\nError(%X)\n", error);
}
}
}
alu_operation(alu, input);
printf("RegA: %04X\n", alu_getRegValue(alu, $RALUA, &error));
printf("RegB: %04X\n", alu_getRegValue(alu, $RALUB, &error));
printf("RegR: %04X\n", alu_getRegValue(alu, $RALUR, &error));
}
} else {
//clear the input buffer
while (getchar() != '\n'){};
}
}
}
scanf() tends to be a house of cards mainly due to its vulnerabilities more than anything else. Anyway, I don't really see anything wrong here (other than the lack of a default case in your switch statement). I would probably abstract some of those larger switch cases into functions though.
I gotta say, though, if you actually had to write this out what a huge waste of time. The focus should be on the ALU part. Much easier not worry about interactivity and just let you parse a file or something. Geez.
I did not, strictly speaking, have to do it interactively. There was the option to hard-code a demo of the ALU operations. It was an option to do it this way, so I did it.
Monkey Ball Warrior on
"I resent the entire notion of a body as an ante and then raise you a generalized dissatisfaction with physicality itself" -- Tycho
If I have a class, and I want to create many objects and store them in a dynamic array, how do I set it up? I read in a list of files in a directory on initialisation, so I know that for example I may have 50 files, and what I want to do is read each file into a class, which creates an object. I then want to be able to store each object that I've created in an array of objects, so I can recall them all later. What I can't seem to do is declare a dynamic array of objects based on the number of files I've read in to start with...
If I have a class, and I want to create many objects and store them in a dynamic array, how do I set it up? I read in a list of files in a directory on initialisation, so I know that for example I may have 50 files, and what I want to do is read each file into a class, which creates an object. I then want to be able to store each object that I've created in an array of objects, so I can recall them all later. What I can't seem to do is declare a dynamic array of objects based on the number of files I've read in to start with...
Do you mean something like....
#include <vector>
using std::vector;
class tFileInfo; // Contains file information that you wish to store
typedef std::vector< tFileInfo > tFileInfoVector;
void initFileList( tFileInfoVector& rFileInfoVector )
{
while( stillFiles )
{
rFileInfoVector.push_back( tFileInfo( /* constructor */ ) );
}
}
int main( int argc, char * argv[] )
{
tFileInfoVector currentFiles;
initFileLIst( currentFiles );
for( tFileInfoVector::iterator fileIter = currentFiles.begin(); fileIter != currentFiles.end(); ++fileIter )
{
tFileInfo& rFile = *fileIter;
// Do something with rFile
}
// or can access like an array
for( unsigned fileIndex = 0; fileIndex < currentFiles.size(); ++fileIndex )
{
tFileInfo& rFIle = currentFiles[fileIndex];
// Do something with rFile
}
}
That'll be a generic C++ way of doing it.
The more C oriented way would be to dynamically allocate an array:
Ya Ecco that vector approach looks like the kind of thing I need to do. It's not actually files that I am storing, it's a whole bunch of filepaths. The info I need to break out is the different parts of the filename (which all have a meaning in various other places).
I'll give it a shot but I think what you've desribed will suit me perfectly Thanks
Well, trying to do something simple/quickly is giving me problems again.
I've got characters walking down an alley, and I've set up a trapezoid they can walk in. To the characters it's a 1024 x 1024 field, I use this algorithm to scale that into actual screen coordinates.
The problem with this is that lines curve like crazy. What I remember about perspective in art class is that straight lines are straight in any perspective, so what am I doing wrong in my approach?
No, they definitely shouldn't curve, barring weird projections (fisheye, etc). How are you drawing them? The easiest way would be to translate the start/end coordinates to whatever your view space is and then just draw a line between those two points
Yeah, if I just draw a line between the 2 points it works fine. But if I break it up into steps it curves, which makes tracing that line to do hit detection or moving characters hard.
I need advice on choosing a programming language to study. I have rudimentary knowledge of Assembly and a fair grasp of Java, along with design principles and patterns. I have little difficulty with most programs that can be interacted with from a command line, but I seriously struggle with GUIs and graphics (to the point where I have no idea how most of Java's graphical classes/functions actually work).
I wish to write a fairly simple grid-based, 2D, turn-based tactical RPG, in the style of X-COM or Final Fantasy Tactics. Which language should I learn and use for that purpose?
Grey Paladin on
"All men dream, but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity; but the dreamers of the day are dangerous men, for they may act their dream with open eyes to make it possible." - T.E. Lawrence
I need advice on choosing a programming language to study. I have rudimentary knowledge of Assembly and a fair grasp of Java, along with design principles and patterns. I have little difficulty with most programs that can be interacted with from a command line, but I seriously struggle with GUIs and graphics (to the point where I have no idea how most of Java's graphical classes/functions actually work).
I wish to write a fairly simple grid-based, 2D, turn-based tactical RPG, in the style of X-COM or Final Fantasy Tactics. Which language should I learn and use for that purpose?
Really, for that sort of stuff, it's what libraries you use that matter more than the language. Java isn't the greatest language for writing games in, but millions of Minecraft players don't seem to care. It'd probably work fine.
This being said, I'm not a huge fan of Java. I'd probably look at LÖVE (Lua) or maybe PyGame or Cocos2D (Python). XNA or Unity3D are good if you want to write some C#.
hey programming thread, programming on windows is pretty sucky, and my current laptop is super old and falling apart, so I'm looking to buy a new laptop to run linux on
do you guys have any recommendations for a really cheap laptop/netbook that will work fine with linux
That is amusing!
The music is kinda repetive, but that's probably easy to add/change/whatever
I like the cheevos.
Yah we didn't have a sound person on the team so they just threw in some free stuff they found. Which is why I programmed in the mute button. I totally called them cheevos in the code too. Thank you for tryin' it!
That is amusing!
The music is kinda repetive, but that's probably easy to add/change/whatever
I like the cheevos.
Yah we didn't have a sound person on the team so they just threw in some free stuff they found. Which is why I programmed in the mute button. I totally called them cheevos in the code too. Thank you for tryin' it!
Just out of curiosity, what did you settle on for path finding?
My thesis is on traversing implicitly defined graphs, so I'm always curious to see what people in the real world use, even if yours was an explicit graph.
I need advice on choosing a programming language to study. I have rudimentary knowledge of Assembly and a fair grasp of Java, along with design principles and patterns. I have little difficulty with most programs that can be interacted with from a command line, but I seriously struggle with GUIs and graphics (to the point where I have no idea how most of Java's graphical classes/functions actually work).
I wish to write a fairly simple grid-based, 2D, turn-based tactical RPG, in the style of X-COM or Final Fantasy Tactics. Which language should I learn and use for that purpose?
I'm biased towards Java, so you could def. do that in Java using a Canvas object for the grid and sprite placement and writing up all the behvaior with that.
I am also SUPER biased towards GWT, so if you want to get fancy you can write it in GWT (which uses Java) as a web page. I personally learned most of my Java skills through GWT because it's so easy to get visual feed back based on what code you write. It's not the super best for games, but it can definitely be done. Small Disclaimer though, I recommend GWT for anyone who asks what language they should use.
Oh shit. It looks like they're filing Chapter 11, but they're already planning on closing 200 stores. I should find out if any one of the ones they're closing are near me.
End on
I wish that someway, somehow, that I could save every one of us
Oh shit. It looks like they're filing Chapter 11, but they're already planning on closing 200 stores. I should find out if any one of the ones they're closing are near me.
Let me know if you find a list, eh? I'm looking around for one, but haven't found it yet.
Yeah mine isn't there, but they're definitely going out of business. I think mine's closing in 2 months which isn't included in that list. IMO, call them and ask if they're one of the stores scheduled for closing soon.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
That is amusing!
The music is kinda repetive, but that's probably easy to add/change/whatever
I like the cheevos.
Yah we didn't have a sound person on the team so they just threw in some free stuff they found. Which is why I programmed in the mute button. I totally called them cheevos in the code too. Thank you for tryin' it!
Just out of curiosity, what did you settle on for path finding?
My thesis is on traversing implicitly defined graphs, so I'm always curious to see what people in the real world use, even if yours was an explicit graph.
The method wasn't too complicated. I started with a node, then sent out subsequent expanding radial nodes until the target was found, then found the shortest path and used that. Sounds like I used an explicit graph, I think?
scanf() tends to be a house of cards mainly due to its vulnerabilities more than anything else. Anyway, I don't really see anything wrong here (other than the lack of a default case in your switch statement). I would probably abstract some of those larger switch cases into functions though.
I gotta say, though, if you actually had to write this out what a huge waste of time. The focus should be on the ALU part. Much easier not worry about interactivity and just let you parse a file or something. Geez.
I guess I'm a bit late here, but:
You can write code which uses scanf() securely, you just have to pay attention to the size of your buffers. However, you should never use scanf() by itself. Instead, use fgets() followed by sscanf(). This is because if scanf() fails to parse, the input stream will end up a partially read line and it's difficult to recover (using "cin >> a" has the same problem, btw). Also, if you have several alternatives, you can test against each of them by trying to parse the same line with sscanf() and either checking how many fields have been matched, or checking if the entire line has been parsed with %n.
In my experience, fgets() + sscanf() is the easiest way of processing input in C or C++.
Yeah, if I just draw a line between the 2 points it works fine. But if I break it up into steps it curves, which makes tracing that line to do hit detection or moving characters hard.
Then you have some weird projection issues, are you absolutely sure that the coordinates of the line you're showing is right?
If I've understood what you're doing right, the Points are the locations of the trapezoid in space, with 0,2 being the smaller part at the top and 3,1 being the larger base, this determines the width of the trapezoid slice at the y coordinate (top width + change in width * distance from top). I think your problem might be that minX and maxX have weird values at the extremes (if ratioY is 0 then minX is maximum, maxX is zero, if it's 1 then minX is 0 and maxX is maximum), which could cause curving
#include <stdio.h>
int main()
{
char input[75];
printf("Enter your first name and age:\n");
fgets(input,75,stdin);
char name[50];
unsigned int age;
//sscanf should return a nonzero number, indicating a match
//a good programmer would make sure that the number matches
//the input we're looking for here
if(!sscanf(input,"%s %u",name,&age)) {
printf("Well nice to meet you, %s. I don't know very many %u year olds.",name,age);
} else {
printf("Sorry I didn't understand that.");
}
return 0;
}
Feel free to pick apart my code. :P
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
That is amusing!
The music is kinda repetive, but that's probably easy to add/change/whatever
I like the cheevos.
Yah we didn't have a sound person on the team so they just threw in some free stuff they found. Which is why I programmed in the mute button. I totally called them cheevos in the code too. Thank you for tryin' it!
Just out of curiosity, what did you settle on for path finding?
My thesis is on traversing implicitly defined graphs, so I'm always curious to see what people in the real world use, even if yours was an explicit graph.
The method wasn't too complicated. I started with a node, then sent out subsequent expanding radial nodes until the target was found, then found the shortest path and used that. Sounds like I used an explicit graph, I think?
Well, you get to smash the stack due to name being placed after input :P
The order of the variables isn't the main problem. The main problem is that he's writing name (which could be up to 75 characters long) into an array which is only 50 characters long. Either increase the size of name, or use sscanf(input, "%50s %u", name, &age).
You should be checking if the return from sscanf() is equal to the number of fields parsed. I.e. sscanf("%50s %u", name, &age) == 2.
I also like to add an extra space around my format string so it will gobble leading and trailing whitespace:
sscanf(input, " %50s %u ", name, &age) == 2.
Finally, this will still match inputs with extra garbage at the end. You can fail if there's extra garbage by comparing the length of the matched string with the length of the entire string: sscanf(input, " %50s %u %n", name, &age, &len) == 2 && len == strlen(input)
Posts
In fairness, it wasn't that outdated when I was most active on IRC (1998-2003ish), but the channels I hung out also had their own script that ran on top of it that made all super leet.
Ah, high school.
SE++ Forum Battle Archive
I did not, strictly speaking, have to do it interactively. There was the option to hard-code a demo of the ALU operations. It was an option to do it this way, so I did it.
I think the web service is already provided to me (they sent a WSDL and a service URL), so I'll just have to develop the app side of it.
If I have a class, and I want to create many objects and store them in a dynamic array, how do I set it up? I read in a list of files in a directory on initialisation, so I know that for example I may have 50 files, and what I want to do is read each file into a class, which creates an object. I then want to be able to store each object that I've created in an array of objects, so I can recall them all later. What I can't seem to do is declare a dynamic array of objects based on the number of files I've read in to start with...
Do you mean something like....
#include <vector> using std::vector; class tFileInfo; // Contains file information that you wish to store typedef std::vector< tFileInfo > tFileInfoVector; void initFileList( tFileInfoVector& rFileInfoVector ) { while( stillFiles ) { rFileInfoVector.push_back( tFileInfo( /* constructor */ ) ); } } int main( int argc, char * argv[] ) { tFileInfoVector currentFiles; initFileLIst( currentFiles ); for( tFileInfoVector::iterator fileIter = currentFiles.begin(); fileIter != currentFiles.end(); ++fileIter ) { tFileInfo& rFile = *fileIter; // Do something with rFile } // or can access like an array for( unsigned fileIndex = 0; fileIndex < currentFiles.size(); ++fileIndex ) { tFileInfo& rFIle = currentFiles[fileIndex]; // Do something with rFile } }That'll be a generic C++ way of doing it.
The more C oriented way would be to dynamically allocate an array:
tFileInfo *pFileArray = new tFileInfo[ NUM_OF_FILES ]; for( unsigned fileIndex = 0; fileIndex < NUM_OF_FILES; ++fileIndex ) { // operate on stuff } delete[] pFileArray; // Free allocated memoryAn MFC specific way involves using CArray.
Each has its own advantages/disadvantages, depending on situation.
Mostly recommend using std::vector, though.
I'll give it a shot but I think what you've desribed will suit me perfectly
I've got characters walking down an alley, and I've set up a trapezoid they can walk in. To the characters it's a 1024 x 1024 field, I use this algorithm to scale that into actual screen coordinates.
The problem with this is that lines curve like crazy. What I remember about perspective in art class is that straight lines are straight in any perspective, so what am I doing wrong in my approach?
Now that's a name I haven't heard in years.
Worked on a script package called Kallisti.
I wish to write a fairly simple grid-based, 2D, turn-based tactical RPG, in the style of X-COM or Final Fantasy Tactics. Which language should I learn and use for that purpose?
Really, for that sort of stuff, it's what libraries you use that matter more than the language. Java isn't the greatest language for writing games in, but millions of Minecraft players don't seem to care. It'd probably work fine.
This being said, I'm not a huge fan of Java. I'd probably look at LÖVE (Lua) or maybe PyGame or Cocos2D (Python). XNA or Unity3D are good if you want to write some C#.
do you guys have any recommendations for a really cheap laptop/netbook that will work fine with linux
Unity game link is here: http://www.kongregate.com/games/celticconundrum/dr-dna
Special thanks again to:
bowen
EvilMonkey
GnomeTank
JHunz
Kakodaimonos
The music is kinda repetive, but that's probably easy to add/change/whatever
I like the cheevos.
Yah we didn't have a sound person on the team so they just threw in some free stuff they found. Which is why I programmed in the mute button. I totally called them cheevos in the code too. Thank you for tryin' it!
Just out of curiosity, what did you settle on for path finding?
I'm biased towards Java, so you could def. do that in Java using a Canvas object for the grid and sprite placement and writing up all the behvaior with that.
I am also SUPER biased towards GWT, so if you want to get fancy you can write it in GWT (which uses Java) as a web page. I personally learned most of my Java skills through GWT because it's so easy to get visual feed back based on what code you write. It's not the super best for games, but it can definitely be done. Small Disclaimer though, I recommend GWT for anyone who asks what language they should use.
The C Programming Language
Thanks for going out of business Borders!
Oh shit. It looks like they're filing Chapter 11, but they're already planning on closing 200 stores. I should find out if any one of the ones they're closing are near me.
Let me know if you find a list, eh? I'm looking around for one, but haven't found it yet.
Sadly, my state isn't even in their list at all.
The method wasn't too complicated. I started with a node, then sent out subsequent expanding radial nodes until the target was found, then found the shortest path and used that. Sounds like I used an explicit graph, I think?
I guess I'm a bit late here, but:
You can write code which uses scanf() securely, you just have to pay attention to the size of your buffers. However, you should never use scanf() by itself. Instead, use fgets() followed by sscanf(). This is because if scanf() fails to parse, the input stream will end up a partially read line and it's difficult to recover (using "cin >> a" has the same problem, btw). Also, if you have several alternatives, you can test against each of them by trying to parse the same line with sscanf() and either checking how many fields have been matched, or checking if the entire line has been parsed with %n.
In my experience, fgets() + sscanf() is the easiest way of processing input in C or C++.
Then you have some weird projection issues, are you absolutely sure that the coordinates of the line you're showing is right?
Try this maybe?
*displayX = *ratioX * ((Points[0].x - Points[2].x) + (Points[3].x - Points[0].x) * (1-*ratioY));
If I've understood what you're doing right, the Points are the locations of the trapezoid in space, with 0,2 being the smaller part at the top and 3,1 being the larger base, this determines the width of the trapezoid slice at the y coordinate (top width + change in width * distance from top). I think your problem might be that minX and maxX have weird values at the extremes (if ratioY is 0 then minX is maximum, maxX is zero, if it's 1 then minX is 0 and maxX is maximum), which could cause curving
#include <stdio.h> int main() { char input[75]; printf("Enter your first name and age:\n"); fgets(input,75,stdin); char name[50]; unsigned int age; //sscanf should return a nonzero number, indicating a match //a good programmer would make sure that the number matches //the input we're looking for here if(!sscanf(input,"%s %u",name,&age)) { printf("Well nice to meet you, %s. I don't know very many %u year olds.",name,age); } else { printf("Sorry I didn't understand that."); } return 0; }Feel free to pick apart my code. :P
That's a breadth first search.
The order of the variables isn't the main problem. The main problem is that he's writing name (which could be up to 75 characters long) into an array which is only 50 characters long. Either increase the size of name, or use sscanf(input, "%50s %u", name, &age).
You should be checking if the return from sscanf() is equal to the number of fields parsed. I.e. sscanf("%50s %u", name, &age) == 2.
I also like to add an extra space around my format string so it will gobble leading and trailing whitespace:
sscanf(input, " %50s %u ", name, &age) == 2.
Finally, this will still match inputs with extra garbage at the end. You can fail if there's extra garbage by comparing the length of the matched string with the length of the entire string: sscanf(input, " %50s %u %n", name, &age, &len) == 2 && len == strlen(input)