The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsMe Playing with some typecasting:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int x;
cout<<"Press enter to fix your computer."<<endl;
cin.get();
while(1){
x = rand() % 254 + 1;
cout<<(char)x;
}
fflush(stdin);
cin.get();
return 0;
}
Well, this is a code snippet that uses virtual classes, inheritance, and pointers! See if you can follow along!
Also, if you know what you are doing could you double check that I'm not getting any memory leaks? I don't think I am, but I'm soak testing it at work overnight just to be sure.
///////////////////////////////////
// Virtual class
class shot
{
protected:
- BOB shotAnim;
- int dying;
- bool dead;
public:
- shot(int x, int y, BOB in)
- {
- - shotAnim = in;
- - shotAnim.x = x;
- - shotAnim.y = y;
- - dead = false;
- }
- virtual void move() = 0;
- virtual void draw() = 0;
- bool isDead();
};
///////////////////////////////////
// Inherited class
class basic : public shot
{
public:
- basic(int x, int y, BOB in) : shot(x, y, in)
- {
- - shotAnim.xv = 7;
- }
- ~basic()
- {
- - Destroy_BOB(&shotAnim);
- }
- void move();
- void draw();
};
///////////////////////////////////
// Shot node for linked list(with dummy head)
struct shotNode
{
- shot *item;
- shotNode *next;
- shotNode(shot *i, shotNode *n)
- {
- - item = i;
- - next = n;
- }
- ~shotNode()
- {
- - delete item;
- }
};
///////////////////////////////////
// Globals
shotNode *shotHead;
BOB basicShot;
shotHead = new shotNode(NULL, NULL);
Create_BOB(&basicShot, 0,0,100,100,1, BOB_ATTR_SINGLE_FRAME | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY, 0, 16);
///////////////////////////////////
// Shoot
if(keyboard_state[DIK_A])
{
- shotNode *toAdd = new shotNode(new basic(textx, texty, basicShot), shotHead->next);
- shotHead->next = toAdd;
}
///////////////////////////////////
// Move
shotNode *temp = shotHead;
while(temp->next != NULL)
{
- temp->next->item->move();
- temp = temp->next;
}
///////////////////////////////////
// Draw
temp = shotHead;
while(temp->next != NULL)
{
- temp->next->item->draw();
- temp = temp->next;
}
///////////////////////////////////
// Delete dead(offscreen) shots
temp = shotHead;
while(temp->next != NULL)
{
- if(temp->next->item->isDead())
- {
- - shotNode *toDel = temp->next;
- - temp->next = temp->next->next;
- - delete toDel;
- }
- else
- - temp = temp->next;
}
What may man within him hide, though angel on the outward side.
*claps*
OK my turn, I see your advanced iheritence code with THIS:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char* c_str();
int count(char file[255]);
int main(){
char ans = 'y';
char filep[255];
int lines;
cout<<"Welcome to SPS Line Counter!"<<endl;
while(ans == 'y'){
cout<<"Please put the full path of a c++ file and then push enter twice\n"<<endl;
cin.getline(filep, 255);
cin.ignore();
lines = count(filep);
if (lines != -1){
cout<<filep<<" has "<<lines<<" lines."<<endl;
}
cout<<"Count lines for another file?(y,n):";
cin>>ans;
cin.ignore();
}
fflush(stdin);
cin.get();
return 0;
}
int count(char file[255]){
int lines = 0;
ifstream stmfile (file);
cout<<"Counting......"<<endl;
if (!stmfile.is_open()){
cout<<"An error occured opening that file."<<endl;
cout<<"Please make sure you entered a valid file."<<endl;
lines = -1;
}
else{
while(!stmfile.eof()){
char dummy;
stmfile.get(dummy);
if (dummy == '\n'){
lines++;
}
}
}
return ++lines;
}
Line counting bitches.
This is potentially the most n00bish thing ever said in this topic, but I need more details on the whole "function" Idea... I know what they do... sorta. The book I have explains it, but I can't follow along. I know you have to make them before you call them, but I don't know how to make them or call them. help plx.
~Mike
At 7/8/05 05:45 AM, DDRManiak wrote: This is potentially the most n00bish thing ever said in this topic, but I need more details on the whole "function" Idea... I know what they do... sorta. The book I have explains it, but I can't follow along. I know you have to make them before you call them, but I don't know how to make them or call them. help plx.
~Mike
A function is pretty easy to declare:
returntype functionname(type arguement1, type arg2 [et cetera et cetera])
{
//Do a lot of stuff here
return somevariable;
}
This could be translated into, say:
int PowerOf2(int a, int b)
{
return (a << b);
}
See? It takes A, takes B, and then shifts A by B and then returns A. Functions are just reusable pieces of code that are very handy.
Give me a more precise problem and I will be able to help more. :)
omg.
Playstation Network tag: muffin-noodle
the empty set
At 7/8/05 06:40 AM, Sinnernaut wrote: Give me a more precise problem and I will be able to help more. :)
Ok, I know you can make variables inside functions, but how do you assign them a number/character/string inside the function? Or was that already explained and I missed it?
Ok.
A function is a block of code that does a certain task. There are three steps in making a C++ function:
1: Make a prototype. This basicly means you have to declare this function a long with any arguments it takes and what it will return. This step is optional, but i find it makes your code neater. A prototype looks something like this:
int add(int a, int b);
This function will return a integer value. And it takes 2 arguments, a, and b.
2: Make the function. You can put the fuction anywhere outside main(). I put it at the end of all my code, but thats personal prefrence. For our example of add(). It might look something like this:
int add(int a,int b){
return a + b;
}
This will add a and b and return the result.
The code so far looks like this:
include <iostream>
using namespace std;
int add(int a, int b);
int main(){
}
int add(int a, int b){
return a+b;
}
3: Call the function. This funcion returns a value (an int), so lets declare a variable to hold this answer:
int main(){
int answer;
}
We also need two vars to hold the values we shall add:
int numa, numb;
Then we get the values of numa and numb like this:
cin>>numa;
cin>>numb;
The actual function call looks like this:
answer = add(numa, numb);
numa will turn into a and numb will turn into b in the add function. answer will be a + b.
The whole code looks like this:
#include <iostream>
using namespace std;
int add(int a, int b);
int main(){
int answer, numa, numb;
cin>>numa;
cin>>numb;
answer = add(numa, numb);
cout<<answer;
fflush(stdin); //unclogs input buffer
cin.get(); //pauses program
return 0;
}
int add(int a, int b){
return a+b;
}
Hope you got it.
the first bit of a floating point number is the sign (+/-). 0 is positive, 1 is negative...
//absolute value of a floating point number
//ms's fabsf() is somehow slower
float FloatAbs (float f) {
int n;
n = *(int*)&f;
n &= 0x7fffffff;
return *(float*)&n;
}
//toggle sign
float FloatSignChange (float f) {
int n;
n = *(int*)&f;
//this works too
//n ^= ~0x7fffffff;
n ^= 0x80000000;
return *(float*)&n;
}
int RoundToInteger (float f) {
//floats always round down
return (int)(f + 0.5f);
}
BWAHAHAHA. This is the first slightly past beginner code I have made. It is a simple name rater I made to rate my friend, myself and my sister. just take a wild guess at my name...
#include <iostream>
#include <stdlib.h>
int main()
{
beginning:
using namespace std;
string name;
cout << "Enter your name so I can rate it." << endl;
cin >> name;
if (name == "John") {
cout << "That name sucks!" << endl;
cin.get();
goto yournameis;
}
if (name == "Michael") {
cout << "Best name ever." << endl;
goto yournameis;
cin.get();
}
if (name == "Casey") {
cout << "You are such a prep Casey!" << endl;
cin.get();
goto yournameis;
}
if (name == "casey") {
cout << "You are such a prep casey!" << endl;
cin.get();
goto yournameis;
}
else {
cout << "That name is alright, don't be so self concious." << endl;
cin.get();
}
yournameis:
cout << "Your name is " << name << "." << endl;
system ("PAUSE");
tryagain:
char yn;
cout << "Enter another name?" << endl;
cout << "y/n" << endl;
cin >> yn;
if (yn == 'y') {
goto beginning;
}
if (yn == 'n') {
goto end;
}
if (yn == 'N') {
goto beginning;
}
if (yn == 'Y') {
goto end;
}
else {
cout << "Sorry, you entered something other than a Y or N." << endl;
cout << "Please try again." << endl;
system ("PAUSE");
goto tryagain;
}
end:
return 0;
}
never EVER use goto. Use loops instead. Goto, is arguably th worst coding style mistake. Spagetti code is evil!
At 7/11/05 10:50 AM, Cinjection wrote: Spagetti code is evil!
But Tagliatelle is bliss!
At 1/12/05 12:23 AM, PelvicThrusters wrote: In The Flash Forums ActionScript Codes Here! was a smashing sucess helping people all over the world learn and become better at actionscript. And I Though We Should Have A C++ Version Seeing as Many People (Myself Included) Are Just Learning the language. We Can Share Knowledge With Each other, teach each other and hopefully some good Programmers will help us out when we are stumped. Well To Start Off Here Is A Code To Make The Words "Hello World" Appear,
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world" << endl;
cin.get();
return 0;
}
Erm, you took that DIRECTLY out of a book. FFS man dont pass this off as your own. It's in the same 'Learn c++ in 21 days' that I have.
Smash Joehanson
At 7/11/05 10:50 AM, Cinjection wrote: never EVER use goto. Use loops instead. Goto, is arguably th worst coding style mistake. Spagetti code is evil!
I am not to the point where I can use loops for that sort of thing. If I were to attempt it, I would fail horribly. The goto command was the only thing I could use. Besides, the code works flawlessly. I don't see why I would need to change anything (yet).
~Mike
PS what is spegetti code?
At 7/11/05 03:32 PM, JohnMartins wrote: Erm, you took that DIRECTLY out of a book. FFS man dont pass this off as your own. It's in the same 'Learn c++ in 21 days' that I have.
Smash Joehanson
Just shut up. That is everyone's first program. He was using it as a way to initiate the thread by putting is a generic code.
At 7/11/05 08:19 PM, DDRManiak wrote:
PS what is spegetti code?
Spagetti code is messy code that branches out and is all over the place such (hence "spagetti"). It is considered very bad style to use goto.
At 7/11/05 10:55 PM, JesusCyborg wrote: Here's a C Compiler that can compile itself. It less than 2024 bytes of code (excluding ; and { and } and whitespace)
http://fabrice.bellard.free.fr/otcc/
You ever tried Pacific C? That shit is fucking old school, it uses old ass like, C89 syntax.
Not:
int main(int argc, char *argv[])
{
...
}
You do:
int
main(argc,argv)
int argc;
char *argv[];
{
...
}
Plus it compiles fucking SMALL shit, like, Digital Mars, Open Watcom, GCC, all that shit compiles to like, 30kb, Pacific C compiles faster than Digital Mars, and the produced shit is like, 6kb here.
omg.
Playstation Network tag: muffin-noodle
the empty set
Hi, my name is Brian, and I am a C++ programmer with a fair to good amount of programming experience. However, I'm not a professional programmer, I only do it for a hobby.
If any is looking for basic C++ tutorials, you can find some on my web site:
xfactorsoftware.bravehost.com. Be warned, though, they aren't finished yet, but they should be soon. The basincs, like variables and functions are currently up, and later I should be adding lessons on pointers, classes, etc...
I specialize in making games using C++ and DirectX. Currently I am working on a 2D game engine for creating small to large scale 2D games.
Also on the site is a small game that me and a few of my friends made for a computer class. The game, and the source code is avaialbe for download. It was programmed im Microsoft Visual C++.net and uses DirectX 7 and 8 interfaces.
If you would like to contact me, e-mail me at xfactorsoftware@yahoo.com, or at fpsinsanity@yahoo.com (boy, I love free e-mail).
At 7/11/05 11:36 PM, JesusCyborg wrote: K&R Syntax is pretty old skool.
I think function declarations look cooler the old way, just without the return type on it's own line.
Now a days, the only modern compiler that produces small executables seems to be Borland Delphi. A 'Hello World' inside a MessageBox() is about 6kB or so.
I don't know how to code Delphi stuff by hand.
I know that the framework to hand code an application by hand in Delphi is the exact same as C, so I could learn how (if I figure out all the variables and differences and whatever,) but I dunno how to just remove the starting forum. I think I've done console stuff in Delphi but I can't remember how because I'm a retard.
:(
If you want to make a really really small executable in a modern format like ELF, and you're using one of the less-strict Linux kernels, you can write some pretty small stuff by hand. For example, this dude wrote a COMPILER for the brainfuck language in assembly. Once you assembly the compiler, the compiler is only 171 bytes in size. How's that for a small program?
Really good. That reminds me I need a project. Think of one for me and I will give you a dollar.
Here's some BrainFuck code that will interpret a BrainFuck program: http://esoteric.sang..ource/src-bf/Ha_BF.b
Seen those before. :D
omg.
Playstation Network tag: muffin-noodle
the empty set
At 7/12/05 03:15 PM, JesusCyborg wrote: stuff about WHAT USED TO BE RACECAR WHICH WAS A COOL NAME LIKE RHYNO SAID :(
Fun. I looked through your source code, unfortunately, I do not know how to operate flex which I can see lots of your code was generated with. Nor bison or yacc. :|
I actually had plans on writing a socket oriented language once, sorta mnemonic like because I'm a lazy fucker, but it'd be like a really retard-proof sockets class, you could do something with a lot of ease, naturally, I am a retard and haven't thought it out fully yet. But I needed an excuse to try to implement something half-ass useful with Raw Sockets.
Which don't work on old 9x kernels very well, by the way. Cryz.
omg.
Playstation Network tag: muffin-noodle
the empty set
Goody, string handling then?
My worst nemesis. Dun dun dunnnnn.
omg.
Playstation Network tag: muffin-noodle
the empty set
At 7/13/05 11:32 AM, JesusCyborg wrote: I used to hate string handling until I found out about regular expressions.
I can kick some ass with some Regexs and perl, but C is the one language that I can't do it in really. :(
omg.
Playstation Network tag: muffin-noodle
the empty set
So... Uh.. this place seems kind of dead. I have a question, how exactly would I go about displaying pictures and stuff, and when you press a certain button, the sprite moves across the screen, and the spite looks like it is running. basically, I am thinking about writing a game in C++ out of sheer boredom. anyone want to help me?
At 9/26/05 04:06 PM, -joah- wrote: ms word
Bad idea.
At 1/12/05 02:38 AM, RageOfOrder wrote: When I compile this, it generates this.
Broken links.
Okay... I created two of thems..
#1
#include <iostream.h>
#include <math.h>
float Povrsina(float a, float b, float c)
{
float s, pov, temp;
a /= 10.;
b /= 10.;
c /= 10.;
s = (a + b + c)/2.;
temp = s*(s - a)*(s - b)*(s - c);
pov = sqrt(temp);
return pov;
}
int main()
{
float a, b, c, pov;
cout << "Upisi duljinu stranice a(mm):";
cin >> a;
cout << "Upisi duljinu stranice b(mm):";
cin >> b;
cout << "Upisi duljinu stranice c(mm):";
cin >> c;
pov = Povrsina(a, b, c);
cout << "Povrsina trokuta je: " << pov << "cm2" << endl;
}
#2
#include <iostream.h>
#include <string.h>
#define MAX 100
int main()
{
int n, prosjek[MAX], pozicija;
char ime[MAX][25], temp[15];
float ocjena[MAX][9], lista[MAX][2] = {0, 0};
cout << "Koliko jebenih natjecatelja imate(max 100)?";
cin >> n;
for(int i = 0; i < n; i++)
{
cout << "Upisite postovano jebeno ime " << i + 1 << ". natjecatelja(max 25 znakova): ";
cin >> ime[i];
cin >> temp;
strncat(ime[i], " ", strlen(ime[i]));
strncat(ime[i], temp, strlen(ime[i]));
for(int j = 0; j < 8; j++)
{
cout << j + 1 << ". sudacka ocjena: ";
cin >> ocjena[i][j];
}
ocjena[i][8] = 0;
for(int j = 0; j < 8; j++)
{
ocjena[i][8] += ocjena[i][j];
}
ocjena[i][8] /= 8.;
cout << "Prosjek: " << ocjena[i][8] << endl;
}
int i = 0;
while(i < n)
{
for(int j = 0; j < n; j++)
{
if((ocjena[j][8] >= lista[i][0]) && (i == 0))
{
lista[i][0] = ocjena[j][8];
lista[i][1] = j;
}
else if((ocjena[j][8] >= lista[i][0]) && (ocjena[j][8] < lista[i - 1][0]))
{
lista[i][0] = ocjena[j][8];
lista[i][1] = j;
}
}
i++;
}
cout << "Rang lista natjecatelja:" << endl;
for(int i = 0; i < n; i++)
{
pozicija = lista[i][1];
cout << (i + 1) << ". " << ime[(int)pozicija] << ": " << ocjena[pozicija][8] << endl;
}
}
Your code = sucky.
You include iostream and string with .h extentions when in reality the C++ standard says all standard libraries use no extention.
You also don't have a return statement in main.
And you call std::cout and std::cin without first declaring what namespace they are in with the Scope Resolution Operators and you don't do using namespace std; so the compiler just has to work it out, which is bad style.
omg.
Playstation Network tag: muffin-noodle
the empty set
At 9/26/05 05:15 PM, Sinnernaut wrote: Your code = sucky.
You include iostream and string with .h extentions when in reality the C++ standard says all standard libraries use no extention.
...
I don't use/ prefer C# at all. SO I am not interested of it... I know a bit.
At 9/26/05 05:15 PM, Sinnernaut wrote: Your code = sucky.
Besides nobody understands his stupid language