So who knows C++?

Joined
Jul 19, 2003
Messages
8,037
Reaction score
2
I'm trying to learn C++ from scratch here. It doesn't seem too tough to start with.

I've done the basic Hello World program:
Code:
#include <iostream>

using namespace std;

int main()
{
    cout << "halflife2.net smells";
    
    cin.get();
    return 0;
}

I can do simple variables and also write it with shorter code than this:
Code:
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int AddedTogether;
    
    a = 5;
    b = 2;
    AddedTogether = a + b;
    
    cout << AddedTogether;
    
    cin.get();
    return 0;
}

I can also do loops, while and if statements.

Tomorrow I want to try cin which I think is to let users input stuff, right? Does anyone know any good tutorials?
 
I dont know C++ but why are you learning it? Making a game? :naughty:
 
Hey I'm learning too. :cheers:
"Turbo C++ Programming In 12 Easy Lessons" I'm currently on Lesson 4. :D
 
I didn't post this in the coding section since it's not really for the Source engine, but I guess it's fine in here.

Datrix said:
Hey I'm learning too. :cheers:
"Turbo C++ Programming In 12 Easy Lessons" I'm currently on Lesson 4. :D
Cool, i've just been using random sites from the internet which has been annoying. I'm gonna go to PC World in the next few days to buy a book.
 
I wrote a Tic-Tac-Toe program in C++!!
I am learning to but recently I feel like going back to PHP and finishing a site.

My Tic-Tac-Toe program would ask for names, tell who goes, would check if someone won, when you win you could play again, play again + change names, or quit.

Lots of loops and functions involved.
 
Minerel said:
My Tic-Tac-Toe program would ask for names, tell who goes, would check if someone won, when you win you could play again, play again + change names, or quit.

Lots of loops and functions involved.

How long did it take to create it? The book that I'm using right now wants me to create an accounting program... it looks like its going to take a while. :(
 
Well I created multiple versions of it, one that used inlines, one that used functions one that used neither...

Right now it would be alot easier, but alot of times I'd run into an error and have no clue what to do.

I really don't know, but within a day I had a workable version. Many improvments have been made with each.

Don't worry, but you should do a program twice or three times in a row. Each rewrite try it a diffrent way, and try and improve on something.
 
A day! A day?!?! It seems like it takes waaaay longer than that, but maybe I'm just a slow typer. Thanks for the advice BTW. :)
 
On ainsworth trainer 4, I've done up to 104/wpm average and 139 top with 0 errors(Backspace Key)... So maybe.
 
Code:
#include <iostream>

using namespace std;

int main()
{
    int i;
    cout << "Please enter an integer value: ";
    cin >> i;
    cout << "The value you entered is " << i;
    cout << " and its double is " << i*2 << ".\n";

    
    cin.get();
    return 0;
}

When I run this code it asks me to type a number, but when I hit return the window closes before I can read "The value you...". How do I stop it closing? I thought cin.get() did that...
 
Put cin.get(); after return 0;
I think that would do it....
Also cin.ignore(); I use after my cin's.(How I was taught via tutorial).
 
I know some basic C++ programming for DOS, but now I'd like to start learning do actually do something for Windows :]
 
Wouldn't putting it after the return make it so it never gets called??

-Angry Lawyer
 
int main()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";

cin.ignore();
cin.get();
return 0;
}
Ok, I've no idea what that cin.ignore() does, but if I put it there the window stays open... nice.
 
cin.ignore
- cin.ignore() is another function that reads and discards a character. Remember that when you type intput into a program, it takes the enter key too. We don't need this, so we throw it away.

See, before when you pressed enter at your Cin, it also counted for cin.get();, using ignore the enter does not count. Something like that..
 
If you compile with MSVC++ it automatically adds a press any key to continue key to the end of the program right after it's compiled, but not when you run the exe :p
 
pfft tictactoe, I was doing that back when yo was playin' with play-doh



Well, not really, I've only been programming about 6 months. So far this is just about what I know:

Variables
If statements
Looping
Functions
Switches
Templates
Pointers
References
Overloading Functions & Operators
Exception Handling
Classes
Inheritance
Polymorphism
Namespaces
File I/O

Theres probably one or two other things I'm forgetting. I've read through about 3 books, and I'm slowly starting on learning the windows API. So far I've managed to create a blank window with File -> Quit (which works xD) and tells you the filename when you click inside the window (And I'm damn proud of that). Its a bit more work than you might think o_O

I'm also learning how to create Dynamic Link Librarys, but they're easy so far =D
 
That's the problem with C++... you're learning all this stuff in the beginning, but there's nothing exciting you can do with it. Wow, I can display some numbers on the screen.

I can't wait until i'm messing around with creating windows apps. Probably years away... meh.
 
Just try and create small little fun games.
Like I created a Tic-Tac-Toe game to expand my skills. I made my computer make a beep noise by convering 1 - 600 from #'s to Ascii.

There are alot of fun things you can do, all you have to do is think of them.
Make a program to do your math for you.
 
I remember my friend coded a snake like game on his calculator. One of those £80 scientific calculators though. I wouldn't know where to start.
 
StardogChampion said:
I remember my friend coded a snake like game on his calculator. One of those £80 scientific calculators though. I wouldn't know where to start.

They don't use C++ as far as I know. Some sort of proprietary scripting language.

Not useful to learn unless you want to create calculator games.
 
I'm more concerned with how the variables, functions, and datatypes all link up with programming a game. But I guess I'll learn that in time. I'm also trying to learn C++. I'm using tutorials from the net but I'm planning to find some books on it. Also, how would you know what is what when looking through the Source code? Because they all have variables and instructions for header files that I don't know what are used for. Unless you have to somehow find out what ALL of the header files do. :dozey:
 
If you want to learn how to program C++ then you should go to a beginners' programming forum. This place is for people who know how to program and need specific help with editing the Source engine.
 
Regarding keeping the console open...

You can use system("pause") if you're programming on windows (i guess you are since you're here!
 
Heh, well, functions and var / pointers are *very* easy compared to the OOP aspect of C++ and some of the more advanced topics. I've gotten to classes and briefly skimmed through polymorphism and inheritance, seems tough but I'm taking it slowly :)

This all has to do with gaming in their own way. Just look at HL2 as a huge illusion, where everything you see and know is just a facade hiding all the mechanics behind it. Basically everything that happens in the game goes through an equation of some sort. Although, a lot of gaming programming is still a mystery to me. Practice, experience, and experimenting.

edit - http://www.cplusplus.com/doc/tutorial/ is a great place to start.
 
SLH said:
Regarding keeping the console open...

You can use system("pause") if you're programming on windows (i guess you are since you're here!

That's poor programming. Calling the system is never a good idea... and you should avoid it anywhere possible. Run the application through the command prompt and see if that works.

Also puting anything after return 0; is sort of a stupid idea. return 0; tells the program to return to windows... so putting anything after that seems a tad silly doesn't it?

I recommend you get VERY comfortable with everything up to pointers, and including pointers.. as those are the foundations for things to come.

Also, thinking of making windows applications right after you make a hello world program is not a silly thought - but it's definately not the right way to go. As it takes maybe 15 lines - tops to right a hello world app.. it takes hundreds to make a window appear on the screen. So I'd stick with the basics until you are able to find some ground to step on... meaning that you won't be doing it a few days after you multiply your first two user given variables.

Good luck to all of you - and good luck to myself... I'm teaching myself also. :naughty:
 
Shens said:
They don't use C++ as far as I know. Some sort of proprietary scripting language.

Not useful to learn unless you want to create calculator games.

its a basic python language
 
mabufo said:
That's poor programming. Calling the system is never a good idea... and you should avoid it anywhere possible. Run the application through the command prompt and see if that works.
Just out of interest, why's it bad to call the system?

I tested it using the command prompt and it did work, as i expected it would. What made you think it wouldn't??
 
To keep the console open do this:

Only place what I put inside the quotes into your code
Put "#include <conio.h>" at the very beggining of your code
The place "getch();" at the end of your code, or wherever you would like it to pause.

This will work on any OS
 
I never said it wouldn't work. I said it was poor programming.

The code will de dependant on windows if you make any sort of system call. So avoid it whenever possible.
 
mabufo said:
I never said it wouldn't work. I said it was poor programming.

The code will de dependant on windows if you make any sort of system call. So avoid it whenever possible.
The way i read your last sentence make me seem like you didn't think it'd work, guess i read it wrong.

Since he's learning C++ i made the assumption that portability wouldn't be high on his priority list, but i guess you're right, it's good to keep it as portable as possible.
 
It is true, that he shouldn't really worry about it too terribly much - as he will be the only person ever looking at those programs... but still, it's good to start on these things early.
 
SLH said:
..., but i guess you're right, it's good to keep it as portable as possible.
My thought exactely. Its like starting learning OO programming as soon as possible. The learning curve may be higher but it'll benefit you more.
 
I think you should just focus on getting comfortable with everything below, and including pointers... before you move onto data manipulation and such.
 
i wish

I had trouble creating the hello world statement but i had a pretty good tutorial.
(im learning also)
i know q-basic tho!

hehehe. its os easy. i made a game called hobo quest (its not origional but hey)
using pages, animations, variables, inputs, ect.

c++ is hard tho...
 
I know this isn't on-topic, buts its spelt Che Guevara.:p
 
Minerel said:
I wrote a Tic-Tac-Toe program in C++!!
I am learning to but recently I feel like going back to PHP and finishing a site.

My Tic-Tac-Toe program would ask for names, tell who goes, would check if someone won, when you win you could play again, play again + change names, or quit.

Lots of loops and functions involved.

lolz i wrote a TTT program too. Did yours have an AI? I used jon von neumann's Minimax, which is mathematically unbeatable, so techinically speaking you can't beat my program.
 
TheDan said:
Theres probably one or two other things I'm forgetting. I've read through about 3 books, and I'm slowly starting on learning the windows API. So far I've managed to create a blank window with File -> Quit (which works xD) and tells you the filename when you click inside the window (And I'm damn proud of that). Its a bit more work than you might think o_O

I'm also learning how to create Dynamic Link Librarys, but they're easy so far =D
I hate the WinAPI, i use gtkmm for GUI coding (www.gtkmm.org), problem is u need to have the runtime enviroment to run it on windows. The good thing is that linux and mac both support gtkmm, so you don't have to rewrite the GUI for those platforms.
 
Back
Top