joshua50187

c++ learning and comprehension

78 posts in this topic

[quote name='Avatar/Oroborus' post='240422' date='Mar 10 2007, 02:15 AM']i think in this case it is ok cause i only goto 1 thing. and functions im still a lil rusty on withh the return[/quote]

it's not okay (i promise).

but since you know you are rusty with functions you should practice them.


try this:

Write a program to to work out the sum of a series from zero to some number (lets call it that number n)

The program should use a function to do this this. the number n should be supplied as an argument to the function

If n is a positive number then return the sum of all numbers up to it. eg: 1 + 2 + 3 + ... + n
otherwise return 0. Edited by rainwater_stillicide

Share this post


Link to post
Share on other sites
[quote name='livinded' post='240418' date='Mar 9 2007, 09:12 PM']gotos should never be used unless it is absolutely necessary. It creates code that is hard to read and hard to debug.[/quote]


A myth, like any construct gotos can be good or bad. Forward gotos are often the cleanest way to code something, and seen in many perfectly fine pieces of software (unix kernels and network code, for example). Paranoia about goto is largely unfounded

[code] /*
* Check for degenerate name (e.g. / or "")
* which is a way of talking about a directory,
* e.g. like "/." or ".".
*/
if (cnp->cn_nameptr[0] == '\') {
if (dp->v_type != VDIR) {
error = ENOTDIR;
goto bad;
}
if (cnp->cn_nameiop != LOOKUP) {
error = EISDIR;
goto bad;
}
...
bad:
if (!dpunlocked)
vput(dp);
VFS_UNLOCK_GIANT(vfslocked);
VFS_UNLOCK_GIANT(dvfslocked);
ndp->ni_cnd.cn_flags &= ~GIANTHELD;
ndp->ni_vp = NULL;
return (error);
}[/code]

This code is extremely easy to read, bad is used as a common error case out of the function, and the point here is it's actually more clear than the alternatives that are possible (like replicating code in each location (which is very fragile, and prone to bugs) or setting some variable (like bad=1 or something).

There are perfectly legitimate uses for goto. Edited by tiocsti

Share this post


Link to post
Share on other sites
hey my friends just sent me some source code and it's awesome i thought i would put it on here this is the type of stuff they give me and i have to figure out
[code]#include <windows.h>

int main()
{
FreeConsole();
srand(GetTickCount());
int nWidth = GetSystemMetrics(SM_CXSCREEN) - 1;
int nHeight = GetSystemMetrics(SM_CYSCREEN) - 1;
while(!GetAsyncKeyState(VK_F8)){
SetCursorPos((rand() % nWidth) + 1, (rand() % nHeight) + 1);
Sleep(600);
}
return 0;
}[/code]
[code]#include <windows.h>

int main()
{
HDC dcDesktop = GetWindowDC(NULL);
int scrX=GetSystemMetrics(SM_CXSCREEN);
int scrY=GetSystemMetrics(SM_CYSCREEN);

while(1)
SetPixel(dcDesktop,rand()%scrX,rand()%scrY,((rand()%256)*65536)+((rand()%256)*255)+rand()%256);

return 0;
}[/code]
[code]#include <windows.h>
int nRandWidth = 150, nRandHeight = 15, nSpeed = 1;
int nScreenWidth, nScreenHeight;

LRESULT WINAPI MelterProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg){
case WM_CREATE:
{
HDC hdcDesktop = GetDC(HWND_DESKTOP);
HDC hdcWindow = GetDC(hWnd);
BitBlt(hdcWindow, 0, 0, nScreenWidth, nScreenHeight, hdcDesktop, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdcWindow);
ReleaseDC(HWND_DESKTOP, hdcDesktop);
SetTimer(hWnd, 0, nSpeed, NULL);
ShowWindow(hWnd, SW_SHOW);
}
return 0;
case WM_ERASEBKGND:
return 0;
case WM_PAINT:
ValidateRect(hWnd, NULL);
return 0;
case WM_TIMER:
{
HDC hdcWindow = GetDC(hWnd);
int nXPos = (rand() % nScreenWidth) - (nRandWidth / 2),
nYPos = (rand() % nRandHeight),
nWidth = (rand() % nRandWidth);
BitBlt(hdcWindow, nXPos, nYPos, nWidth, nScreenHeight, hdcWindow, nXPos, 0, SRCCOPY);
ReleaseDC(hWnd, hdcWindow);
}
return 0;
case WM_CLOSE:
case WM_DESTROY:
{
KillTimer(hWnd, 0);
PostQuitMessage(0);
}
return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
nScreenHeight = GetSystemMetrics(SM_CYSCREEN);

WNDCLASS wndClass = { 0, MelterProc, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), 0, NULL, "Melter" };
if(!RegisterClass(&wndClass)) return MessageBox(HWND_DESKTOP, "Cannot register class!", NULL, MB_ICONERROR | MB_OK);

HWND hWnd = CreateWindow("Melter", NULL, WS_POPUP, 0, 0, nScreenWidth, nScreenHeight, HWND_DESKTOP, NULL, hInstance, NULL);
if(!hWnd) return MessageBox(HWND_DESKTOP, "Cannot create window!", NULL, MB_ICONERROR | MB_OK);

srand(GetTickCount());
MSG Msg = { 0 };
while(Msg.message != WM_QUIT){
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
DestroyWindow(hWnd);
}
return 0;
}[/code]
i just did them and all are pretty cool for some trojan or virus attributes

Share this post


Link to post
Share on other sites
[quote name='tiocsti' post='240432' date='Mar 9 2007, 09:50 PM'][code] /*
* Check for degenerate name (e.g. / or "")
* which is a way of talking about a directory,
* e.g. like "/." or ".".
*/
if (cnp->cn_nameptr[0] == '\') {
if (dp->v_type != VDIR) {
error = ENOTDIR;
goto bad;
}
if (cnp->cn_nameiop != LOOKUP) {
error = EISDIR;
goto bad;
}
...
bad:
if (!dpunlocked)
vput(dp);
VFS_UNLOCK_GIANT(vfslocked);
VFS_UNLOCK_GIANT(dvfslocked);
ndp->ni_cnd.cn_flags &= ~GIANTHELD;
ndp->ni_vp = NULL;
return (error);
}[/code]

This code is extremely easy to read, bad is used as a common error case out of the function, and the point here is it's actually more clear than the alternatives that are possible (like replicating code in each location (which is very fragile, and prone to bugs) or setting some variable (like bad=1 or something).

There are perfectly legitimate uses for goto.[/quote]
i would think in large programs with alot of functions in it it would be more useful to use a goto statement for small things then just having to write a function :blink:

Share this post


Link to post
Share on other sites
[quote name='Avatar/Oroborus' post='240430' date='Mar 10 2007, 02:45 AM']explain that more and i will try it out[/quote]

here's the psuedo code for it:
[code]function int sum ( int n ){
total = 0;
current = 0;

if( max < 0 ){
while(current <= n){
total = total + current
current = current + 1
}
}
return total;
}

main () {
print sum(5)
}[/code]

this example would output the result: 15


[this code isnt very psuedo now i look at it, but ah well, it gets the point across] Edited by rainwater_stillicide

Share this post


Link to post
Share on other sites
[quote name='Avatar/Oroborus' post='240504' date='Mar 10 2007, 01:06 AM']yhar code make absolutely no sense what so ever[/quote]

Then you need to read all [b]15[/b] of your C++ books again. I don't know C++ (don't care to) and I know what that code does.

Share this post


Link to post
Share on other sites
well i know it doesn't work and if you would please run me through cause im really confused i know you want to call the function but i don't see how you get 15 out of it or what header files you use or anything cause print is not a c++ function what language did you right this in

Share this post


Link to post
Share on other sites
I guess all [b]15[/b] of those fancy books never told you about pseudocode (which I'm sure they did, you just read it too fast to remember). Maybe you should go back and read all [b]15[/b] a bit more slowly.

Share this post


Link to post
Share on other sites
the main section calls the function with the argument 5 so the function computers:
1 + 2 + 3 + 4 + 5
which equals 15.

Share this post


Link to post
Share on other sites
ok thank you for that cause i figured out what it did but just not how it was added or the result

Share this post


Link to post
Share on other sites
Yeah, I saw that it was adding up the sum of individual digits ... but I couldn't figure out what the testing of max against a negative number was all about... That part threw me for a moment.

//edit

Could use a formula instead... print n, ((n+1)*n)/2

seems to work.. Edited by PurpleJesus

Share this post


Link to post
Share on other sites
[quote name='PurpleJesus' post='240563' date='Mar 10 2007, 04:02 PM']Yeah, I saw that it was adding up the sum of individual digits ... but I couldn't figure out what the testing of max against a negative number was all about... That part threw me for a moment.

//edit

Could use a formula instead... print n, ((n+1)*n)/2

seems to work..[/quote]

yes, you can (and in 'real life' probably should), but the point was for spongebob crazypants up there to get practice with functions and loops and whatnot. Edited by rainwater_stillicide

Share this post


Link to post
Share on other sites
Hi im spongebob crazypants, but that pseudo code confused the hell out of me about functions. So could someone else explain it to me a lil better.


also how is it that script kiddies get anything done i've tried hacking while others products , like brutus, shadow scan, nmap (nmap is a awesome port scanner), metasploit but i never get anything done could someone explain to me more about ports and ips or if they don't have the time to point me to a good tcp/ip document thanks

Share this post


Link to post
Share on other sites
[quote name='Avatar/Oroborus' post='240686' date='Mar 10 2007, 08:41 PM']Hi im spongebob crazypants, but that pseudo code confused the hell out of me about functions. So could someone else explain it to me a lil better.


also how is it that script kiddies get anything done i've tried hacking while others products , like brutus, shadow scan, nmap (nmap is a awesome port scanner), metasploit but i never get anything done could someone explain to me more about ports and ips or if they don't have the time to point me to a good tcp/ip document thanks[/quote]

Read your [b]15[/b] C++ books, I guarantee they tell you exactly how functions work.

Share this post


Link to post
Share on other sites
[quote name='tehbizz' post='240717' date='Mar 10 2007, 10:00 PM']Read your [b]15[/b] C++ books, I guarantee they tell you exactly how functions work.[/quote]

You don't let up, do you?

What we're all trying to get at is: read about them yourself. A function is not a difficult concept. You should have moved past functions within your first hour or two of learning. If you don't have the ambition to learn something so simple for yourself, no one is going to waste their time helping you. Why should they, you're just going to turn around and ask another question you'd know the answer to if you read your [b]15[/b] [size=1](sorry, couldn't help myself)[/size] C++ books. Edited by Ohm

Share this post


Link to post
Share on other sites
yes but apparently i dont get the concept of functions that well so im asking for a simpler explanation, then from the boks i read who are wrote for programmers by programmers and all beginner programming books focus more on abstract programming then functions. they give you a gay explanation and a even dumper example of adding 2+2 the returning the value but i get lost in translation some where and need help stop with the you need to read the books cause i think it would be muchg easier for you to explain this then write a paragraph on why i should read or what i should be doing

Share this post


Link to post
Share on other sites
Trust me, your "beginner programming books" cover functions. Why not read them? I'm not aware of any professional writers here on the board, so I'm sure functions explained better in the books. Try [i]reading[/i] them.

Share this post


Link to post
Share on other sites
as you saw from my last entry i did read them, i can't unstand the concept that they are trying to pass i know a function is like a global variable i can be called at anytime but i can't understand the concept of them in general and i know im hard to understand but im not a englisch major im psychology and philosophy im trying to move into computer science but i want to get a head start on it

Share this post


Link to post
Share on other sites
[quote name='Avatar/Oroborus' post='240176' date='Mar 8 2007, 07:31 PM']how does everyone here or on rohitab.com generate so much unique coe and know so much about the c++ lang. when all of the examples in ebooks or tutorials are all the damn same i/o or string handling you talk about win sock and api hooks and all of this stuff and when beginners ask how do you do it alot of the time google well im asking how do you find this info out is there some magical thing, i have some source code and it is amazing but i can't understand how exactly it works i know what each part basically does so all im asking is how do you find out about more practical programming[/quote]


You don't learn to be a good coder. Good coders are good coders. Basically they do it because their imagination (and skill) is essentially limitless. Your not gonna go anywhere, or learn anything through mindless droning through ebooks, tuts, and white papers. You have to understand what they are doing and take it slow enough so you understand it's uses. I help a kid weekly on C++ and he sounds exactly like you. What I did to learn was this. I went through the book and typed out all the examples. I did the tests at the end of the chapter and then read about the different things it's used for on the internet. Then when I finished a chapter I made a program using the knowledge from the previous sections and the current chapter all off my own ideas. That's what I believe taught me to be a practical programmer. It sounds like you think you know what you are doing, but you have no clue how to implement it in the real world. Maybe take on a few coding projects to ease your way into more practical programming through other's ideas.


It's all your imagination my friend and if your a hacker, you probably have one hell of one. Edited by deadc0de

Share this post


Link to post
Share on other sites
[quote name='Ohm' post='240728' date='Mar 10 2007, 10:15 PM'][quote name='tehbizz' post='240717' date='Mar 10 2007, 10:00 PM']Read your [b]15[/b] C++ books, I guarantee they tell you exactly how functions work.[/quote]

You don't let up, do you?

What we're all trying to get at is: read about them yourself. A function is not a difficult concept. You should have moved past functions within your first hour or two of learning. If you don't have the ambition to learn something so simple for yourself, no one is going to waste their time helping you. Why should they, you're just going to turn around and ask another question you'd know the answer to if you read your [b]15[/b] [size=1](sorry, couldn't help myself)[/size] C++ books.
[/quote]

Nope, there's no point in giving up.

While you "read" 15 books on C++, you seem to lack an extremely basic understanding of it so I'm not wondering if actually read them, I'm wondering if you only read the parts that showed code and tried to learn by only reading that.

Honestly, understanding functions is [i]simple[/i]. As Ohm said, you should have gotten past them in the first hour or two of reading and gained a working knowledge of them. But apparently, out of all 15 books, they're written so complex only experienced programmers can read them and the beginners books are "too" dumb (hint: there is never TOO dumb) for you to bother with.

Go back and read or admit that you really don't understand any of it and politely (read: without your ego) ask for help.

Share this post


Link to post
Share on other sites
well im not trying to seem like i have a big ego but i know the way i learn and no i read them i don't just look at the preety pictures of code i do lack a basic comprehension of functions and everyting the guy who replied before you helped more then any one else he understood the title of this discussion and that is how everyone else learnt and what helped them with learning and comprehension of the different data types and features of c++ i don't have to stroke my ego online im asking for simple help with by finding out what helped you. i read the books over and over again the more i read the less i understand

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.