What's new

I want to learn some programming

tye stik

Super Nintendo Wizard
http://cplusplus.com/doc/tutorial/

Thats the one I started out with. I didn't understand some of it while I was doing it, but just skip it if you dont get it and start on the next section... comeback after learning something different. It helps me when I'm learning. It usually gives me a different perspective on a problem I'm having.

As for C++ coding... if a program dosent compile and run properly, look for basic syntax errors... theres been times where for hours I'd look for where I went wrong, only to find I missed a ; or a }. Total bummer. :p
 
OP
xneoangel

xneoangel

Persona User
I'm having some advance in C++ coding, i have already played a bit with basic variables and strings, but well its weird in the tutorial i'm doing every sample program ends up with:

return 0;

i don't know what that commad does as the program works even if i dont put it at the end, so its kinda weird what does that line do?

And also since i'm starting i'm only making console based programs which display something on-screen but when i execute the program the program shows me the stuff but it dissapears instantly, is there a line of code i can introduce to put a dealy time of about 5 seconds before it closes or to close it only when i press the X button?
 

mudlord

Banned
Essentially, that "return 0" statement, is used to return a program's status. When its status is "0", the program exists. Of course, there are other ways to clean up a program's CPU threads.

And there is code that can display a console window until the program is closed forcibly (like when using CTRL-ALT-DELETE or pressing the "X" button in Windows). When I get around to it, I'll post a code sample of how to do that :).
 
Last edited:

Toasty

Sony battery
When your main function ends, that usually marks the end of your program's execution. Whenever a program exits, it returns a number to indicate the conditions it exited under. Zero indicates a successful, 'normal' shutdown. Other integers can indicate various errors that caused the program to terminate prematurely. Some compilers don't care whether you put 'return 0' at the end of your main function and will automatically do it for you, but it's good practice to explicitly return something when the function signature (in this case, int main()) says it will.
 

Iconoclast

New member
Nope. Still didn't work.

I got this log when I tried to compile Audio.c:

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "E:\Utilities\Dev-Cpp\Projects\Audio.c" -o "E:\Utilities\Dev-Cpp\Projects\Audio.exe" -I"E:\Utilities\Dev-Cpp\include" -L"E:\Utilities\Dev-Cpp\lib"
In file included from E:\Utilities\Dev-Cpp\Projects\Audio.c:2:

E:/Utilities/Dev-Cpp/include/dsound.h:1888: error: redefinition of typedef 'LPDIRECTSOUNDFULLDUPLEX'
E:/Utilities/Dev-Cpp/include/dsound.h:175: error: previous declaration of 'LPDIRECTSOUNDFULLDUPLEX' was here

Execution terminated

And I replaced all the "include" files with those from your DirectX SDK, of course. Could this be the compiler, or did I screw up on something again?
 

Doomulation

?????????????????????????
xneoangel said:
I'm having some advance in C++ coding, i have already played a bit with basic variables and strings, but well its weird in the tutorial i'm doing every sample program ends up with:

return 0;

i don't know what that commad does as the program works even if i dont put it at the end, so its kinda weird what does that line do?

And also since i'm starting i'm only making console based programs which display something on-screen but when i execute the program the program shows me the stuff but it dissapears instantly, is there a line of code i can introduce to put a dealy time of about 5 seconds before it closes or to close it only when i press the X button?
Here's the deal...
Return is the instructions that says end this function now and return. Each function needs to either return void or another type of value or class. The main function is typically int main (though some compiler can allow a void main I think, but that is not good practice).
Return 0 means return 0 and end this function. In code, a function can typically return whatever you want and you can pick that up later. When it comes to threads, the thread's entry function (in this case, main) [Note: This isn't really relevant, but main isn't the main entry function; there is another function before that calls main, but you don't have to remember this] returns the value you want the thread to end with. Programs can later pick up what number or return value a thread ended with.

Did you get all that? What you return from main is (usually) what other programs wants to check for for success or error. Your program does not necessarily have to return 0. It can return any value you want and there are no guidelines for this (And I sure do not recommend writing console apps that returns values to indicate success or failure, because that is DOS-nature, and we're now in Windows-nature. Instead, if you want to share functionality with other apps, make a dll).
 
OP
xneoangel

xneoangel

Persona User
Ok so return (any value); basically gives the computer the value i selected to indicate the program worked ok and closes it?

Oh and do you know some code i can introduce in this console based apps so that i doesnt colse after it executes but when i close it manually?

Thanks for your help guys.
 
Last edited:

Doomulation

?????????????????????????
xneoangel said:
Ok so return (any value); basically gives the computer the value i selected to indicate the program worked ok and closes it?
No, not exactly. The compiler only generates the code and the computer only follows instructions. When you return a value from main, it is what program that started your app that decides what to do with it. When you return a value from main, however, it is assumed that it means success or failure, but it mustn't be so.

Oh and do you know some code i can introduce in this console based apps so that i doesnt colse after it executes but when i close it manually?
Ah, yes forgot that!
Do something like...
Code:
int buf;
cin >> buf;
This tells it to wait for input and store it in buf. So just type something and press enter and it will then terminate the program.
 
OP
xneoangel

xneoangel

Persona User
Ok now i got it.

Let me see if i understood your code.

int buf; //Defining a variable
cin >> buf; //Input something which will be stored in the buf variable

Am i right?
So because in the function you added an input it'll will only close after it finishes its function which is displaying the stuff i put in before that code and inputting something that will be stored in buf?(if it's a variable i can give it any name i want except reserved commands right?)
 

Doomulation

?????????????????????????
Bravo. It will execute all your code 'til the end and will then wait for you to input something. After you do that, it will close.
Yes, because you store it in a variable, you can name it whatever. However, be careful. It's not usually a good idea to input into an int, becuase you never know if the user will type a string or a number. You'll have to go through a long process of clearing the input buffer or something which I don't remember. Better to use a string (char) and later convert it to a number if that's what you're expecting.
 

mudlord

Banned
OK, here's a very simple console app. It is a sinewave synth, based on the BASS sound system. The program will exit when the spacebar is selected, and the program will only close when this happens or when it is forcibly closed.

I hope this app is a good enough practical example.
 

Iconoclast

New member
Did any of you guys even try compiling the source for the No Sound plugin using Dev-Cpp?? I don't think it works. Might be the compiler, or that I replaced all of the files with those from the zip poobah uploaded instead of just dsound.h.

Well, I'm in Chapter 2 right now, and I'm very slowly intaking that tutorial, but I am finally learning C. Thanks for the link.

And thank you, mudlord, for your example. I'll study it right now.
 
Last edited:

Poobah

New member
I think you are supposed to not replace the ones that already exist, because DX8 is pretty old and so MinGW would come with more recent headers, but I'm not sure if that is what was causing the problem.
 

Iconoclast

New member
I just reinstalled it, got all my header files back, pasted ONLY the file dsound.h in the include directory, tried to compile the No Sound plugin, and I got this:

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "E:\Utilities\Dev-Cpp\Projects\Audio.c" -o "E:\Utilities\Dev-Cpp\Projects\Audio.exe" -I"E:\Utilities\Dev-Cpp\include" -L"E:\Utilities\Dev-Cpp\lib"
In file included from E:\Utilities\Dev-Cpp\Projects\Audio.c:2:

E:/Utilities/Dev-Cpp/include/dsound.h:1888: error: redefinition of typedef 'LPDIRECTSOUNDFULLDUPLEX'
E:/Utilities/Dev-Cpp/include/dsound.h:175: error: previous declaration of 'LPDIRECTSOUNDFULLDUPLEX' was here

Execution terminated

It must be, like Doomulation said, either a) the dsound.h file I got from poobah, b) the compiler, or c) Damn salesman. I'm gettin' me mallet!
 

Doomulation

?????????????????????????
If you have a fast connection, try downloading the real DirectX SDK and see if it compiles with those headers.
 

Poobah

New member
I don't think the official one works with MinGW. Maybe I'm mistaken, but I don't think that any of Microsoft's SDKs work with MinGW. I'll try compiling it myself and see if I can get it to work.

EDIT: Just comment out the redefinition of it and it should work fine. You'll need someone to translate the MSVC inline assembly code to use GCC syntax before you can compile it, because they're completely incompatible.
 
Last edited:

Toasty

Sony battery
It's a little late to mention now, but on Windows platforms (and maybe Unix as well?) you can also pause the execution of your program by calling system("pause") as an alternative to using cin >> x. This will print a nice little "Press any key to continue..." message too. Bare in mind that the behavior (or lack thereof) of that command can vary from platform-to-platform though, so use it with care. You might also want to take a look at the C (not C++) version of "hello world". I remember that the overloaded << and >> operators in the C++ version kind of confused me at first, but the C equivalent just uses plain old functions to print and read from the console. But it's just a suggestion; proceed however you like.

@Iconoclast: Any chance you could start your own thread? It's kind of confusing to have two conversations going on in the same thread.
 

Top