Jump to content


Photo
* * * * - 1 votes

c++ learning and comprehension


  • This topic is locked This topic is locked
77 replies to this topic

#21 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 09 March 2007 - 08:48 PM

yes i am seeing a therapist but not for goto

#22 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 09 March 2007 - 09:00 PM

and whats wrong with goto

ok and i reread my code and the whole threatening and havioin sex this is not a rape it's more like getting her alone talking to her and seeing where it goes from there and this code was written ages ago like 3 months after we broke up cause she had a abortion i got drunk and spit in her face she said she did it for us i called her a murderer and done some shit i regret like telling her parents and scaring the shit out of her and just yeah i have worked on it alot (not really)

Edited by Avatar/Oroborus, 09 March 2007 - 09:09 PM.


#23 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 09 March 2007 - 09:03 PM

There's nothing wrong with the goto technically (except it's a goto and generally goto's are frowned upon).

instead of a goto its usually better to move the repeating code into a function and call that instead.

see section 3.8 in K&R (page 65)

Edited by rainwater_stillicide, 09 March 2007 - 09:10 PM.


#24 livinded

livinded

    Dangerous free thinker

  • Agents of the Revolution
  • 1,942 posts
  • Location:~/

Posted 09 March 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.

#25 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 09 March 2007 - 09:15 PM

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

#26 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 09 March 2007 - 09:32 PM

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


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, 09 March 2007 - 10:03 PM.


#27 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 09 March 2007 - 09:45 PM

explain that more and i will try it out

#28 tiocsti

tiocsti

    rekcah-rebÜ

  • Banned
  • 676 posts

Posted 09 March 2007 - 09:50 PM

gotos should never be used unless it is absolutely necessary. It creates code that is hard to read and hard to debug.



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

/* 
		 * 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); 
}

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, 09 March 2007 - 09:52 PM.


#29 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 09 March 2007 - 09:56 PM

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
#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;
}
#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;
}
#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;
}
i just did them and all are pretty cool for some trojan or virus attributes

#30 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 09 March 2007 - 10:02 PM

/* 
		 * 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); 
}

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.

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:

#31 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 09 March 2007 - 10:26 PM

explain that more and i will try it out


here's the psuedo code for it:
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)
}

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, 10 March 2007 - 03:06 AM.


#32 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 10 March 2007 - 01:06 AM

yhar code make absolutely no sense what so ever

#33 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

Posted 10 March 2007 - 01:07 AM

yhar code make absolutely no sense what so ever


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

#34 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 10 March 2007 - 01:13 AM

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

#35 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 10 March 2007 - 01:19 AM

http://en.wikipedia....wiki/Psuedocode

#36 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

Posted 10 March 2007 - 02:15 AM

I guess all 15 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 15 a bit more slowly.

#37 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 10 March 2007 - 03:01 AM

i still don't know how it out puts 15

#38 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 10 March 2007 - 03:06 AM

the main section calls the function with the argument 5 so the function computers:
1 + 2 + 3 + 4 + 5
which equals 15.

#39 Avatar/Oroborus

Avatar/Oroborus

    elite

  • Members
  • 109 posts
  • Location:san jose, CA

Posted 10 March 2007 - 04:46 AM

ok thank you for that cause i figured out what it did but just not how it was added or the result

#40 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 10 March 2007 - 11:02 AM

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, 10 March 2007 - 01:10 PM.





BinRev is hosted by the great people at Lunarpages!