Followers 0

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. ";
{
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;
}
}
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 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 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 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 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

1

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 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 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 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 on other sites
Hey Thank You It works perfectly! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

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 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]

0

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]

[/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 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 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 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]

[/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 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 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 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]

[/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 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 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...
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 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]

[/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 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...
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.

0

Voted
1

Create an account

Register a new account

Followers 0

• 19
• 14
• 23
• 11
• 28