Powered by Invision Power Board

 
  Pages: (10) [1] 2 3 ... Last » ( Go to first unread post ) Reply to this topicStart new topicStart Poll

> Giygas learns the art of C++
United States
Giygas
Posted: Sep 24 2009, 01:09 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


After using GM for about 5 years, it's time to move onto to something more professional and open...
And alot more complicated :c

I've been working with it for about a day now, and I have some of the basic idea's down currently. I could write a program that asks for your name and returns it to you, or a simple calculator.

Though I understand it just get's harder from here... I'll post updates as I make some simple programs or expand upon older ones, ect.

Some programs I made from scratch ( I do these occassionally to make sure I understand some aspects. It's nice when they actually work, I feel like I accomplished something :3 )

QUOTE

#include<iostream>
#include<string>
using namespace std;

int main ()
{
    int mynumber;
    int othernumber;
    cout << "Give me a number: \n";
    cin >> mynumber;
    cout << "You gave me " << mynumber << " , now give me a number to multiply it by: \n";
    cin >> othernumber;
    cout << "You gave me " << mynumber << " and " << othernumber << " , let me think...\n";
    cout << "So " << mynumber << " X " << othernumber << " = " << mynumber*othernumber;
    system("pause");
    return 0;
}

This one just multiplies two numbers you give it.

Anyways, post with idea's to help with my coding, or some suggestions for me to try and make. :3
PMEmail Poster
Top
United States
Sploder
Posted: Sep 24 2009, 01:39 PM
Quote Post


Standard Member
[*][*][*][*]

Group Icon
Group: Members
Posts: 781
Member No.: 2317
Joined: 12-July 06

Status: (0d) [--]


It's good that you've decided to move onto a more powerful, better controlled language.

It's not too hard to move onto C++ from GML, provided you understand all the advanced GML issues.

Anyway, good luck on your "journey!"
PMEmail Poster
Top
United States
Giygas
Posted: Sep 24 2009, 01:44 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Sploder @ Sep 24 2009, 01:39 PM)
It's good that you've decided to move onto a more powerful, better controlled language.

It's not too hard to move onto C++ from GML, provided you understand all the advanced GML issues.

Anyway, good luck on your "journey!"

Thanks- hopefully i'll be able to break all the GML habits i've developed in place of the C++ ones.

Also, functions are fun =D
QUOTE

#include<iostream>
using namespace std;

int divide(int a, int b)
{
    int u;
    u=a/b;
}

int main ()
{
    int u;
    divide(10,2);
    cout << u;
    system("pause");
    return 0;
}

So many possibilities :3
PMEmail Poster
Top
Unspecified
ChaosEmerl
Posted: Sep 24 2009, 01:52 PM
Quote Post


You are our master!!
[*][*]

Group Icon
Group: Members
Posts: 1419
Member No.: 2765
Joined: 5-October 06

Status: (0d) [--]


That's technically not a function.

Or maybe C++ is just weird, I don't know. But in most languages, a function returns a value without you having to declare a variable for it.


--------------------
PMEmail Poster
Top
Canada
Elixer
Posted: Sep 24 2009, 01:56 PM
Quote Post


Fully Restores Health and Mana
[*]

Group Icon
Group: Members
Posts: 673
Member No.: 6178
Joined: 15-April 09

Status: (0d) [--]


Just remember; if C can shoot you in the foot, C++ can blow your leg off.


--------------------
I am not interested in ordinary people
If there are any aliens, time travelers, espers or sliders, please come with me
That is all

user posted image user posted image user posted image
Blazblue: Main user posted image Subs user posted image user posted image
user posted image
PMEmail PosterUsers WebsiteAOL
Top
United States
Giygas
Posted: Sep 24 2009, 01:57 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (ChaosEmerl @ Sep 24 2009, 01:52 PM)
That's technically not a function.

Or maybe C++ is just weird, I don't know. But in most languages, a function returns a value without you having to declare a variable for it.

Unless i'm mistaken, C++ functions refer to unset variables and outputs that can be called at any time, in this case, "divide" can be called with the variabes 10 and 5. Since divide is set to know that it should divide a by b, it divides 5 by 10 and outputs the answer on the screen.

I think?
PMEmail Poster
Top
United States
Xgoff
Posted: Sep 24 2009, 01:58 PM
Quote Post


<):|
[*][*][*][*][*]
[*][*]

Group Icon
Group: Members
Posts: 52341
Member No.: 24
Joined: 13-October 03

Status: (0d) [--]


QUOTE (ChaosEmerl @ Sep 24 2009, 12:52 PM)
That's technically not a function.

Or maybe C++ is just weird, I don't know.  But in most languages, a function returns a value without you having to declare a variable for it.

p sure that declaration is redundant anyway

EDIT: nvm i see what he did

that's kind of a weird way to write most functions

generally what you'd do is something like:

#include using namespace std; int divide(int a, int b) { return a/b; } int main () { int u = divide(10,2); cout << u; system("pause"); return 0; }

This post has been edited by Xgoff on Sep 24 2009, 02:00 PM


--------------------

This post may contain original research or unverified claims.
Please disregard the above information and contact an administrator.

DISCLAIMER: by sending me (xgoff) a private message, you agree to the directives and their terms specified henceforth:
DIRECTIVE 1 (APPLE): i may or may not reply promptly or at all; and there are no guarantees to the usefulness of the reply. i may not acknowledge whether i have even received your private message
DIRECTIVE 2 (CHILE CON CARNE): as per my view, "private" applies only to the initial transaction, and the material of your message may or may not be made public at my discretion; as this will more than likely be a post in the CCC or IRC, you may not be able to view it
DIRECTIVE 3 (FEATHER DUSTER): you must address me (xgoff) as "Sir Master Xgofficus his Highest and Most Awesome the Third"; failure to comply with this term may invoke one or both of the above directives, and i will leave a burning bag of **** on your doorstep
DIRECTIVE 4 (BOOTSTRAP): if you have read this disclaimer, please private message me promptly, in compliance with the above terms, so i can ensure you are capable of following directions you idiot
this concludes the test of the emergency disclaimer system, your scheduled programming will now continue. satisfaction guaranteed, and 100% cash back available under certain circumstances; restrictions may or may not apply within your place of residence
NOTICE: these directives and their terms may change at any time, without notice; as a private message transaction to myself assumes an understanding and full compliance of the above, you should ensure you are fully aware of the above terms at any point before sending a private message; any message received is assumed to have been sent in compliance with the above

QUOTE
(5:25:58 PM) Mikau: xgoff
(5:26:00 PM) Mikau: guess what
(5:26:04 PM) Xgoff: chicken butt
(5:26:09 PM) Mikau: **** you
PMEmail PosterUsers WebsiteAOLMSN
Top
United States
Giygas
Posted: Sep 24 2009, 02:01 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Xgoff @ Sep 24 2009, 01:58 PM)
p sure that declaration is redundant anyway

EDIT: nvm i see what he did

that's kind of a weird way to write most functions

generally what you'd do is something like:

#include using namespace std; int divide(int a, int b) { return a/b; } int main () { int u = divide(10,2); cout << u; system("pause"); return 0; }

Oh hey that's alot easier to call up a function that way
;D
PMEmail Poster
Top
United States
Giygas
Posted: Sep 24 2009, 02:32 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Alright, so I wrote my first "game" program from scratch.
It asks you to input a number, you have 10 turns to try and guess the number it's thinking.
It will tell you if the number is higher than what you typed in, or lower.

However, right now, it generates a random number, from 1 to 37,150. That's no good! Does anybody have an idea of how I can set a range of numbers from 1-100?

Here is the current code:
QUOTE
#include<iostream>
#include<string>
using namespace std;

int main ()
{
    int myguess;
    int n=rand();
    int t=10;
    while (myguess != n)
    {
          cout << "Try again! ";
          if (myguess > n)
          cout << "Go lower! ";
          else if (myguess < n)
          cout << "Go higher! ";
          cin >> myguess;
          t--;
          if (t==0)
          if (myguess != n)
          {
          cout << "  Game over!  ";
          system("pause");
          return 0;
          }
}
if (myguess=n){
cout << "Correct!";
system("pause");
return 0;
}
}
PMEmail Poster
Top
United States
RetroXYZ
Posted: Sep 24 2009, 02:36 PM
Quote Post


Standard Member
[*][*]

Group Icon
Group: Members
Posts: 2714
Member No.: 4325
Joined: 27-August 07

Status: (0d) [--]


I don't get why people think C++ is hard. It's an extremely easy language (or at least, in my opinion) to learn, however, the sheer simplicity is what makes it difficult to apply to making programs.

With your program, you multiply the answer by x/RAND_MAX, x being the max number. In which case it would be 1 to x.

This post has been edited by RetroXYZ on Sep 24 2009, 02:36 PM
PMEmail PosterMSN
Top
United States
Giygas
Posted: Sep 24 2009, 02:42 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (RetroXYZ @ Sep 24 2009, 02:36 PM)
I don't get why people think C++ is hard. It's an extremely easy language (or at least, in my opinion) to learn, however, the sheer simplicity is what makes it difficult to apply to making programs.

With your program, you multiply the answer by x/RAND_MAX, x being the max number. In which case it would be 1 to x.

Ah, awesome- that worked.
After messing around, though, a quicker way is to do
(rand() % x)+1
With x being max number, and the +1 making it so it can't be 0.

So the final code is:
QUOTE
[size=0.1]
#include<iostream>
#include<string>
using namespace std;

int main ()
{
    int myguess=242;
    int n = (rand() % 10) + 1;
    int t=3;
    while (myguess != n)
    {
          cout << "Try again! ";
          if (myguess > n)
          cout << "Go lower! ";
          else if (myguess < n)
          cout << "Go higher! ";
          cin >> myguess;
          t--;
          if (t==0)
          if (myguess != n)
          {
          cout << "  Game over!  ";
          system("pause");
          return 0;
          }
}
if (myguess=n){
cout << "Correct!";
system("pause");
return 0;
}
}

3 triesto guess the number between 1 and 10. Yay :3
PMEmail Poster
Top
United States
RetroXYZ
Posted: Sep 24 2009, 04:08 PM
Quote Post


Standard Member
[*][*]

Group Icon
Group: Members
Posts: 2714
Member No.: 4325
Joined: 27-August 07

Status: (0d) [--]


Using modulo is faster, but it's not quite as random. Of course, C++ random algorithms are hardly random in the first place. :P
PMEmail PosterMSN
Top
United States
Giygas
Posted: Sep 24 2009, 04:25 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (RetroXYZ @ Sep 24 2009, 04:08 PM)
Using modulo is faster, but it's not quite as random. Of course, C++ random algorithms are hardly random in the first place. :P

Well, right now, i'm just getting a list of programs done before moving on.
Things like Hi-Lo, a simple Slot Machine, and something grid based are the main things i'm focusing on. When I can do these, i'll move on to...
Well... Actually, I don't know what to do after i'm finished with the "DOS" stage as I call it- do I continue practicing? Do I go onto the application stage? The 2D Stage?
PMEmail Poster
Top
Unspecified
Lightning
Posted: Sep 24 2009, 04:40 PM
Quote Post


Ignorance isn't stupidity but choosing to remain ignorant is
[*][*]

Group Icon
Group: IRC Operators
Posts: 6381
Member No.: 583
Joined: 31-August 04

Status: (0d) [--]


you'd do well to stay at the console for now--learn how to deal with arrays, STL vectors and strings, and classes first, before you move on to graphics.


--------------------
click here to change my avatar. / gosh why are you even here lightning
hacker, n.an individual who enjoys learning computer system details and how to capitalize on his or her capabilities...not a criminal.
(from webster's new world hacker dictionary)
fedora linux 10
Fedora 10 Final!

Download today!
quality web comics (stories):
  1. girl genius: adventure! romance! mad science!
  2. punch an' pie: try a slice of life, then swallow.
  3. dresden codak: most interesting comic ever
quality web comics (one-shots):
  1. a softer world: truth and beauty bombs
  2. smbc: saturday morning breakfast cereal
  3. buttersafe: pictures and probably some words
"Religion is comparable to a childhood neurosis." - Sigmund Freud
“It is not by delusion, however exalted, that mankind can prosper, but only by unswerving courage in the pursuit of truth.” - Bertrand Russell
“To kill an error is as good a service as, and sometimes better than, the establishing of a new truth or fact.” - Charles Darwin
PMUsers WebsiteMSN
Top
United States
Giygas
Posted: Sep 24 2009, 04:43 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Lightning @ Sep 24 2009, 04:40 PM)
you'd do well to stay at the console for now--learn how to deal with arrays, STL vectors and strings, and classes first, before you move on to graphics.

Have not heard of STL vectors yet, but i'm working on Classes right now- I think I have a good knowledge of Arrays currently, and I might need to review on strings before going onto the more advanced stuff.
PMEmail Poster
Top
United States
RetroXYZ
Posted: Sep 24 2009, 05:06 PM
Quote Post


Standard Member
[*][*]

Group Icon
Group: Members
Posts: 2714
Member No.: 4325
Joined: 27-August 07

Status: (0d) [--]


QUOTE (Lightning @ Sep 24 2009, 05:40 PM)
you'd do well to stay at the console for now--learn how to deal with arrays, STL vectors and strings, and classes first, before you move on to graphics.

actually, for the most part, if you understand classes, SFML is extremely easy to use
PMEmail PosterMSN
Top
United States
Giygas
Posted: Sep 24 2009, 05:07 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


I've been having a really hard time with classes, though.
I can't really get the concept down... I think all of the variables and functions to define intimidates me.
PMEmail Poster
Top
United States
Giygas
Posted: Sep 24 2009, 05:32 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Alright, so I think I understand classes- I just honestly can't type out a working program using one without some kind of reference, it is very confusing! Other than that, I find I can use almost any of the other console prompts i've dealt with so far. In time, I suppose i'll have to get used to classes. Until then, i'll be writing a few programs to expand upon them a bit, and eventually move onto some more advanced things.
PMEmail Poster
Top
United States
OniLink10
Posted: Sep 24 2009, 05:59 PM
Quote Post


C++ Programmer, Unofficial Physicist, and Unofficial Chemist
[*][*]

Group Icon
Group: Members
Posts: 3920
Member No.: 4907
Joined: 19-February 08

Status: (0d) [--]


Once you have figured out all of the Console Commands and basics, I would highly recommend getting SFML and using that for Graphics, Windowing, Input, Multithreading(le gasp! It's complicated, but if you can figure it out, you can use it for turning one program into two and taking full use of multi-core Processors), Audio, Networking, etc. It's a great library, and uses OpenGL for High-speed Rendering and Pixel Shaders! Of course, you should finish learning the basics first, and stick with that until you get used to everything.


--------------------
QUOTE (Xgoff @ Sep 10 2009 @ 06:11 PM)
did you try hello's engine

make sure to not ****ing change anything before using it!
PMEmail PosterUsers WebsiteYahoo
Top
United States
Bacteriophage
Posted: Sep 24 2009, 06:04 PM
Quote Post


injustice anywhere is a threat to justice everywhere
[*][*][*][*][*]
[*][*][*][*][*]
[*][*][KFC]

Group Icon
Group: Members
Posts: 10716
Member No.: 2411
Joined: 31-July 06

Status: (0d) [--]


Grats. I'm doing the same thing.

Though I think you're taking this far too fast. You won't learn arrays, pointers, or classes to the extent that you can use them in just four hours.

Take it slow.

Also, don't be an idiot about it. Don't think that just because you understand how to use classes you're a master of programming. You have a lot of work to do with memory management before you're ready to make anything good.

This post has been edited by Bacteriophage on Sep 24 2009, 06:05 PM


--------------------
PMEmail PosterUsers WebsiteAOLMSN
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

  Topic Options Topic Options Pages: (10) [1] 2 3 ... Last » Reply to this topicStart new topicStart Poll

 




[ Script Execution time: 0.0990 ]   [ 13 queries used ]   [ GZIP Enabled ]   [ Server Load: 1.44 ]