Jump to content


Photo
* * * * - 1 votes

c++ learning and comprehension


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

#21 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

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

Posted 09 March 2007 - 08:21 PM

ok one problem i am having is putting a while loop in a if statement can one of you give me a non specific example of this please


I couldn't think of a good example but this is how you do it.

#include <stdio.h>

main()
{
	int x = 10, y = 0;;
	if( x != y )
		while( y < x )
			printf("%d", y++);
}

Edited by rainwater_stillicide, 09 March 2007 - 08:39 PM.


#22 Avatar/Oroborus

Avatar/Oroborus

    elite

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

Posted 09 March 2007 - 08:28 PM

I can get past the while loop, I can understand that. Can you please stop flaming on my punctuation.


#include <iostream>
using namespace std;
int main()
{
	for(int i = 1; i<266;i++)
	cout << (char) i;
	cin.get();
	return 0;
}

here is some code i did to change a integer to a character. everyone really needs to stop worry about legitimacy of my claims and focus on the subject.
its useless code at this level but code be used in a encryption program. i have more simple stuff like this but i have no real use for them yet cause i can't do much more i suck and need help in general. like this one im trying to put a gui to it for now it is just console
/******************************************************************************* program: thoughts.exe v0.1.1* file name: 1.cpp* compiled with: Dev-C++ 4.9.9.2* OS: MS Windows XP* author: Oroborus* date: 10.20.2006* desciption: shows some of my inner most thoughts with a menu.* revised: 10.21.06 v0.1.2  <--------------------------\* added:False entry restart                                      see the huge gap in the dates i was still learning  conditional looping * revised: 2.20.07 v0.1.3    <------------------------- /* added:choice to choose another option or end*******************************************************************************/#include <iostream>  //include the pre-processor iostreamusing namespace std;int main() //initialize the main function{   // print out the menu and intialize var: choicestart:    cout << "\n\n\ntype in your choice for what you want to do. \n"    "1. read my theory of god.\n"    "2. read how i feel about kat.\n"    "3. read my plans on how i am going to kill kat.\n"    "4. read what my plans for the future are.\n"    "make the selection: ";    int choice;    cin >> choice;    // allow access to the different options of the menu based on her choice    switch(choice){    case 1:{         cout << "\tgod is there it just doesn\'t care.\n"         "\tand why should it, what have you done\n"         "\tto deserve its attention. he loves you\n"         "\tbut why would such a great being try to have\n"         "\tsuch a hands on approach about everday life\n"         "\tif i knew everything i would want to distance\n"         "\tmyself from my creation to better see if my\n"         "\tassumptions with the future were true to see if\n"         "\tmy absence would make a difference\n";         char n;         cout << "Do you want to continue?(y/n): ";         cin >> n;         if(n == 'y')         goto start;         else             break;         }    case 2:{         cout << "\tI love kat, i love kat more then anything in\n"         "\tthis world i would gladly die for her i want to\n"         "\tbe with her she makes me so happy and so mad at\n"         "\tthe same time i mean if she would just give me\n"         "\tone chance so i can love her again. when i heard\n"         "\tshe had sex with nick my heart stopped i died that\n"         "\tday inside and when i would totally forgive her and\n"         "\tjust move on with our lives but it seems she wont\n"         "\teven talk to me anymore i hate it that she wont\n"         "\tman what i would do to get back with her shit i\n"         "\twould murder thousands of people just for her attention\n"         "\tbut only time will tell when i will get my chance\n";         char n;         cout << "Do you want to continue?(y/n): ";         cin >> n;         if(n == 'y')         goto start;         else             break;         }    case 3:{         cout << "\ti don't know how i'm going to do it really either i\n"         "\tknow i am going to do it but not how really what i am\n"         "\tthinking of doing is hiding out at her house like in the\n"         "\tgarage wait for her to come home from a show or go out\n"         "\tto school and jump out from this room near her car where\n"         "\tit is always parked when she walks past grabbing her and\n"         "\tslitting her throat then running like i stole something\n"         "\thiding out for years or killing myself by od or hanging\n"         "\ti also have plans of kidnapping and torture but those\n"         "\tseem unlikely i wouldn't hurt her or kill her or do\n"         "\tanythign bad to her if she would just love me and\n"         "\tgive me one chance everything would be ok but it seems\n"         "\tthat this is the only chance i have to put my seriousness\n"         "\tthere and show people im not one to just be walked over\n";         char n;         cout << "Do you want to continue?(y/n): ";         cin >> n;         if(n == 'y')         goto start;         else             break;         }    case 4:{         cout << "\ti want to for me and kat to be together if not i'll kill\n"         "\ther i mean really i am going kill her but she doesn't\n"         "\tthink so i mean really i am just going to fucking kill\n"         "\ther and throw my life away but if my plan of getting\n"         "\ther alone and maybe threatening her life but ultimately\n"         "\thaving sex with me and i cum in her and get her pregnant\n"         "\tor persauding her to give me my second chance i mean if\n"         "\tshe does that i mean really god damn it i fucking love her\n"         "\tand i will kill her if she gives me my chance then i have no\n"         "\treason to be angry with her and i could get a job and go to school\n"         "\tand pay off my debts and just get my life back on track and\n"         "\tmy life with her my one and only true love i love you babygirl\n";         char n;         cout << "Do you want to continue?(y/n): ";         cin >> n;         if(n == 'y')         goto start;         else             break;         }    default:{         cout <<"\t\tYour selection was not valid choose again\n";         goto start;         }      }}
notice my bad punctuation i know these programs suck but they are just my practices to get the syntax scheme of C++ and yes i have been through therpy the bitch had a abortion but thats for another forum

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


#23 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 09 March 2007 - 08:42 PM

After reading your second program... Really, you need help. Consider seeing a therapist. I mean, come on, you used goto. That's just a cry for help.

(Seriously, see a therapist.)

#24 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

#25 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.


#26 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.


#27 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.

#28 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

#29 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.


#30 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

#31 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.


#32 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

#33 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:

#34 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.


#35 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

#36 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.

#37 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

#38 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

#39 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.

#40 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




BinRev is hosted by the great people at Lunarpages!