• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Sid_TheBeginner

Please simplify my code.

43 posts in this topic

I've been interested in game programming recently. So I've started learning C++
I'm a total beginner. Please review my code and also help me to improve it.
I want a slot machine with: 1000$ to start with. It's seems to be working. Please help me to make it more simpler.


[CODE]
// A Slotting Machine implementation
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int Random(int low, int high);
int main()
{
int low = 2; // Random number range.
int high = 7; // Random number range.
int r0, r1, r2;
int money = 1000; // Initial money to start with.
int reply = 0;
char exit;
int bet;
char restart;
char cont;
srand( time (0) ); // Default Random Number function call.
cout << "Players chips: $1000"<< endl;
while(true)
{
cout << "1) Play slot. 2) Exit. ";
cin >> reply;
if(reply == 1)
{
while(1)
{
while(true)
{
cout << "Enter your bet: ";
cin >> bet;
if(bet > 0 && bet <= money)
break;
else cout << "Please enter a valid bet.\n";
}
r0 = Random(low, high); // Function call
cout << r0 <<" ";
r1 = Random(low, high); // Function call
cout << r1 << " ";
r2 = Random(low, high); // Function call
cout << r2 << endl;
if (r0 == r1 == r2 == 7) // Machine's number sequence.
{
money = (10 * bet);
cout << "Lucky Bucky!\n";
cout << "Players chips: $" << money << endl;
}
if((r0 == r1 == r2) && (r0 != 7 && r1 != 7 && r2 != 7)) // Machine's number sequence.
{
money = (5 * bet);
cout << "Not Bad!\n";
cout << "Players chips: $" << money << endl;
}
if(r0 == r1 || r1 == r2 || r0 == r2) // Machine's number sequence.
{
money = (3 * bet);
cout << "Go Happy!\n";
cout << "Players chips: $" << money << endl;
}
else
{ cout << "You Lose!\n";
money -= bet; // Get Previous amount back.
cout << "Your Money: " << money << endl;
if(money == 0)
{
cout << "You just got flushed!\n";
cout << "Exiting...\n";
break;
}
}
cout << "1) Play slot. 2) Exit. ";
cin >> cont;
if(cont == '1')
continue;
else cout << "Exiting\n";
break;
}
}
else if (reply == 2)
cout << "Exiting\n";
break;
}
}
int Random(int low, int high)
{
int r0 = low + rand() % ((high + 1) - low);
return r0;
int r1 = low + rand() % ((high + 1) - low);
return r1;
int r2 = low + rand() % ((high + 1) - low);
return r2;
}
[/CODE] Edited by Sid_TheBeginner
0

Share this post


Link to post
Share on other sites
EDIT: OK, fine, no one liked this comment. *grump* [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] I obviously must've stated this the wrong way. Edited by antiHUMANDesigns
-6

Share this post


Link to post
Share on other sites
Why do you return three times in Random()? the r1 and r2 lines dont do anything, return r0 ends the function. Also, shouldnt all these lines "money = (3 * bet);" be "money += 3*bet;" ?
1

Share this post


Link to post
Share on other sites
@ zacaj. Thanks for your reply. I'm calling it 3 times to generate 3 different random nos. Is it possible to generate 3 different nos. in 1 call? If so, please let me know.
If all 3 nos. are the same, i get 10times the money. if 2 nos. are same, i get 3 times the money. If all 3 nos. are different, I lose the money i put in. And yeah, it should be money +=.. Thanks a lot.
0

Share this post


Link to post
Share on other sites
[CODE]
int Random(int low, int high)
{
int r0 = low + rand() % ((high + 1) - low);
return r0; //the function quits here, all code below this never runs
int r1 = low + rand() % ((high + 1) - low);
return r1;
int r2 = low + rand() % ((high + 1) - low);
return r2;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Just to help you with desplaying your code on this site there is a red code button {....} that will maintain all of your code indentation when you use it to desplay your code.it will help us read it a little better. Some bits of advice if you are trying to learn C/C++ with your game is try using some containers to organize your data such as arrays, enums, and vectors. I promise you if you try to learn these concepts more deeply at the beggining it will help you Immensely in the long run. Also you should add way more comments that reffer to what your functions and expression are doing. this is good for both yourself when reviewing your code as well as for others trying to fix or understand your code. Edited by greenzone
1

Share this post


Link to post
Share on other sites
Now my function looks like this. The code still runs. What coresponding changes should I make in the main function? Sorry for such a dumb question but I seem to be confused.

[CODE]
int Random(int low, int high)
{
int r0 = low + rand() % ((high + 1) - low);
return r0;

}
[/CODE]
0

Share this post


Link to post
Share on other sites
@ greenzone. Hey thanks for your reply. Yeah, I've just began learning. I'll get into all those topics soon. I've already learnt a bit of arrays
0

Share this post


Link to post
Share on other sites
First I recommend you dont use r0 since there aren't multiple numbers going on, it's a bit less confusing
[CODE]
int Random(int low, int high)
{
int randomNumber = low + rand() % ((high + 1) - low);
return randomNumber ;
}
[/CODE]

You don't need to make any changes to main() because of the changes in Random(). Each time you call random it's running it separate from the other times. You can call it three times, and each time it will make a new random number and return it.
0

Share this post


Link to post
Share on other sites
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!
0

Share this post


Link to post
Share on other sites
Hey Thank You It works perfectly! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
[quote name='riverreal' timestamp='1340508456' post='4952209']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!
[/quote]
Yes I think for something like this objects are just going to seem needlessly complicated really. Any time you're only making one instance of an object you're really just using it for categorization
0

Share this post


Link to post
Share on other sites
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

Sorry I've no clue what your talking about. Seems its advanced stuff.
0

Share this post


Link to post
Share on other sites
[quote name='Sid_TheBeginner' timestamp='1340508797' post='4952212']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

Sorry I've no clue what your talking about. Seems its advanced stuff.
[/quote]

He thinks you should be structuring your program the same way you would structure a much more advanced program, so that you get into the proper habits. Probably something you should do soon, but right now it looks like you're still getting a grasp of basic programming to me.

@antiHUMANDesigns
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.
[/quote]
This isn't the C way either. This is the way you write stuff that fits in a single function, on not much more than a single page of code
2

Share this post


Link to post
Share on other sites
OK, got downvoted because I'm trying to help? I dunno what's up with people, but OK, he wanted the code fixed for him, so here's a quick (!) example of how it could look, in C++.


EDIT:

OK, so it can only show 26 lines of code? That seems weird.

Can't get the code tag working, so I jus attached a txt file of it instead.

[attachment=9624:slotmachine example.txt] Edited by antiHUMANDesigns
-2

Share this post


Link to post
Share on other sites
[quote name='antiHUMANDesigns' timestamp='1340510370' post='4952219']
OK, got downvoted because I'm trying to help? I dunno what's up with people, but OK, he wanted the code fixed for him, so here's a quick (!) example of how it could look, in C++.


EDIT:

OK, so it can only show 26 lines of code? That seems weird.

Can't get the code tag working, so I jus attached a txt file of it instead.

[attachment=9624:slotmachine example.txt]
[/quote]

The people downvoted you because the code above is written in C++...
He wrote a C++ code but without implementing the object oriented programming paradigm style. But is C++
The OOP is only a paradigm not the complete language
2

Share this post


Link to post
Share on other sites
[quote name='Sid_TheBeginner' timestamp='1340508797' post='4952212']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

Sorry I've no clue what your talking about. Seems its advanced stuff.
[/quote]

I don't know about "advanced", it's what C++ was created for. It's pretty much the whole point of it, as an improvement upon C. If you want to learn C++, you need to start right away. If you want to use C instead, then that's a choice you need to make, but you need to be aware of the difference. I don't know if all these other guys thinks I'm just messing with you or something, but I think that once you look at the code example I attached to my other post, you'll understand that C++ is slightly different from C. Hopefully you other guys would care to listen to me and see the truth in what I'm saying instead of jsut downvoting me as if I was trolling. I, for one, am trying to teach a newbie the proper way to code, and good practices. I don't know what the rest of you are trying to do.
0

Share this post


Link to post
Share on other sites
[quote name='riverreal' timestamp='1340508456' post='4952209']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!
[/quote]

You're going to start being snide? Can we not do that, please?

Ask mr Stroustroup (spelling?) if this is how C++ was designed to look. Yes, he may use one of C++'s objects, but if he still codes his programs as if it was C, then he gains no benefits of C++'s improvements. That is not something I should have to argue about.
-2

Share this post


Link to post
Share on other sites
[quote name='riverreal' timestamp='1340511078' post='4952222']
[quote name='antiHUMANDesigns' timestamp='1340510370' post='4952219']
OK, got downvoted because I'm trying to help? I dunno what's up with people, but OK, he wanted the code fixed for him, so here's a quick (!) example of how it could look, in C++.


EDIT:

OK, so it can only show 26 lines of code? That seems weird.

Can't get the code tag working, so I jus attached a txt file of it instead.

[attachment=9624:slotmachine example.txt]
[/quote]

The people downvoted you because the code above is written in C++...
He wrote a C++ code but without implementing the object oriented programming paradigm style. But is C++
The OOP is only a paradigm not the complete language
[/quote]

I dunno, really... you mean it's C++ because the file is called .cpp, and because he uses std::cout? I would say the lack of OOP is a stronger argument than that, since that was the whole point of C++. The creator himself explains that very clearly. He wanted to merge OOP with the performance of C.
-3

Share this post


Link to post
Share on other sites
[quote name='antiHUMANDesigns' timestamp='1340511151' post='4952223']
[quote name='Sid_TheBeginner' timestamp='1340508797' post='4952212']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

Sorry I've no clue what your talking about. Seems its advanced stuff.
[/quote]

I don't know about "advanced", it's what C++ was created for. It's pretty much the whole point of it, as an improvement upon C. If you want to learn C++, you need to start right away. If you want to use C instead, then that's a choice you need to make, but you need to be aware of the difference. I don't know if all these other guys thinks I'm just messing with you or something, but I think that once you look at the code example I attached to my other post, you'll understand that C++ is slightly different from C. Hopefully you other guys would care to listen to me and see the truth in what I'm saying instead of jsut downvoting me as if I was trolling. I, for one, am trying to teach a newbie the proper way to code, and good practices. I don't know what the rest of you are trying to do.
[/quote]

Your code works amazingly well [img]http://public.gamedev.net//public/style_emoticons/default/ohmy.png[/img] Actually I'm learning C++ as my first programming language. So I called your stuff advanced. I'm an altogether beginner programmer. I'm saving your code buddy. When I jump into these topics, I'll understand your code and would be able to admire it much more.
2

Share this post


Link to post
Share on other sites
[quote name='zacaj' timestamp='1340508581' post='4952211']
[quote name='riverreal' timestamp='1340508456' post='4952209']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!
[/quote]
Yes I think for something like this objects are just going to seem needlessly complicated really. Any time you're only making one instance of an object you're really just using it for categorization
[/quote]

Ye, that is absolutely true, from the viewpoint of scale. But if a beginner never uses classes, he won't learn the proper ways, and to think in OOP takes experience which you should try to garnish from the start.
I mean, most C++ programmers are still not taking full advantage of C++, but still mixes in C. That goes up to professional level, even. And that's an issue.
0

Share this post


Link to post
Share on other sites
[quote name='antiHUMANDesigns' timestamp='1340511462' post='4952224']
[quote name='riverreal' timestamp='1340508456' post='4952209']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!
[/quote]

You're going to start being snide? Can we not do that, please?

Ask mr Stroustroup (spelling?) if this is how C++ was designed to look. Yes, he may use one of C++'s objects, but if he still codes his programs as if it was C, then he gains no benefits of C++'s improvements. That is not something I should have to argue about.
[/quote]

You are wrong again...
The OOP is just one of the paradigms. C++ is multi-paradigm.
Look here: [url="http://en.wikipedia.org/wiki/Multi-paradigm_programming_language#Multi-paradigm_programming_language"]http://en.wikipedia.org/wiki/Multi-paradigm_programming_language#Multi-paradigm_programming_language[/url]
So, C++ can be structured like C.
OOP is not the only improvement.

You said the code ISN'T C++ and IS C...
So go and make a main.c file, copy the code, and compile it...
1

Share this post


Link to post
Share on other sites
[quote name='Sid_TheBeginner' timestamp='1340511738' post='4952226']
[quote name='antiHUMANDesigns' timestamp='1340511151' post='4952223']
[quote name='Sid_TheBeginner' timestamp='1340508797' post='4952212']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

Sorry I've no clue what your talking about. Seems its advanced stuff.
[/quote]

I don't know about "advanced", it's what C++ was created for. It's pretty much the whole point of it, as an improvement upon C. If you want to learn C++, you need to start right away. If you want to use C instead, then that's a choice you need to make, but you need to be aware of the difference. I don't know if all these other guys thinks I'm just messing with you or something, but I think that once you look at the code example I attached to my other post, you'll understand that C++ is slightly different from C. Hopefully you other guys would care to listen to me and see the truth in what I'm saying instead of jsut downvoting me as if I was trolling. I, for one, am trying to teach a newbie the proper way to code, and good practices. I don't know what the rest of you are trying to do.
[/quote]

Your code works amazingly well [img]http://public.gamedev.net//public/style_emoticons/default/ohmy.png[/img] Actually I'm learning C++ as my first programming language. So I called your stuff advanced. I'm an altogether beginner programmer. I'm saving your code buddy. When I jump into these topics, I'll understand your code and would be able to admire it much more.
[/quote]

Hey, thanks a bunch, that warms the old heart. :)

See, C++ is an inherently "advanced" language, so you've got a lot on your plate. But I don't think it's right to try to dumb it down. I've walked this road myself, and I'd have loved someone to steer me in the right direction and save me some time trying to re-adjust later.

Take my code and improve it! It still lacks some stuff that would make the program more complete. And anything you don't understand about it, don't just skip it, ask and/or learn it instead.
0

Share this post


Link to post
Share on other sites
[quote name='riverreal' timestamp='1340511991' post='4952228']
[quote name='antiHUMANDesigns' timestamp='1340511462' post='4952224']
[quote name='riverreal' timestamp='1340508456' post='4952209']
[quote name='antiHUMANDesigns' timestamp='1340506692' post='4952199']
OK, let me start by saying this, very clearly: This is not C++. This is C. You should learn C++ instead. What book or reference are you using, which claims that this is C++? Throw it out the window, whether literal or virtual, and get a reference of C++ instead.

C++ is an object-oriented programming language. This means that you should look at things as real-world object, created from blueprints called classes (or structs). If you have a game with a slot-machine, then that slot machine is an object, I think you'd agree. (In fact, the game itself is an object, isn't it?) So what you need to do is to write a class called SlotMachine, and create an object from that class. And pulling the lever on a slot machine should be represented as a member function (or a "method", as it's called in some languages) of the SlotMachine class. So when you want to pull the lever, you write slotMachine.pullLever().

You have pretty much crammed everything into the main() function. That is not the C++ way, but the C way.

I'd love to re-write it all in C++ for you, but... well, I think you should do it, frankly. Otherwise you'd not be learning, I think.

EDIT:

Oh, don't get me wrong, I'll help you if you need it, and I'm sure others here will aswell. Just try some on your own, with a decent C++ reference in your back, and if you run into problems, we're here.
Just... start over, and do it in C++.
[/quote]

I think you are confused...
He uses an object of class osteam called "cout" instead of "printf".

Ok, let me say this clearly this is not C. This is C++
You can create a new file, and call it "main.c". Then copy the code above, and compile it... What do you get? A lot of errors!!
C can not use objects, and iostream isn't a C library!
[/quote]

You're going to start being snide? Can we not do that, please?

Ask mr Stroustroup (spelling?) if this is how C++ was designed to look. Yes, he may use one of C++'s objects, but if he still codes his programs as if it was C, then he gains no benefits of C++'s improvements. That is not something I should have to argue about.
[/quote]

You are wrong again...
The OOP is just one of the paradigms. C++ is multi-paradigm.
Look here: [url="http://en.wikipedia.org/wiki/Multi-paradigm_programming_language#Multi-paradigm_programming_language"]http://en.wikipedia....amming_language[/url]
So, C++ can be structured like C.
OOP is not the only improvement.

You said the code ISN'T C++ and IS C...
So go and make a main.c file, copy the code, and compile it...
[/quote]

If you want to be picky, OK, you win. Technically, what I said was wrong, because it won't compile as C.

Now, listen to this: [url="https://www.youtube.com/watch?v=JBjjnqG0BP8"]https://www.youtube.com/watch?v=JBjjnqG0BP8[/url] and see my point.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0