1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Need C++ project ideas!

Discussion in 'Programming & Webmastering' started by Maelstrom, Jan 27, 2011.

  1. ctrain New Member

    Joined:
    Jan 12, 2010
    Messages:
    393 (0.20/day)
    Thanks Received:
    72
    Visual C++ is just the IDE, not a language, C++/CLI is the replacement for managed C++.
     
  2. Brandenburg

    Brandenburg New Member

    Joined:
    Feb 13, 2011
    Messages:
    171 (0.11/day)
    Thanks Received:
    31
    Location:
    Waco,Texas
    Yea.. Computers dont like it when you try to divide by zero...

    I guess you did something similar to this.. No surprise my code looks like VB.. VB just flows more naturally to me..

    if (dividend<>0) then
    quotient=divisor/dividend
    end if

    Code looks good... There is a reason I dont do C++ anymore LOL.. I did notice on your Cout statements.. you wrote them like std::cout<<"Blah".. IMO.. An easier way to write it would of been to use this statement right below your last include.. using namespace std;.. When using this.. You just have to say cout<<. To me.. its just cleaner looking

    Here is a small snippet of code for a game I did in C++ like 4 years ago..Its relatively simple I think.. One of my strong suits is nested statements...
    Code:
    case WM_LBUTTONDOWN:{
    	XCoord=(int)LOWORD(lparam);
    	YCoord=(int)HIWORD(lparam);
    		if (StartFlag==true){
    			for (int i=0;i<=MaxDie-1;i++){
    				if (DiceRolls[i]>=0){
    					if (Die_Selected[i]==false){
    						if (XCoord >=DiceShell[i].left && XCoord <=DiceShell[i].right){
    							if (YCoord >=DiceShell[i].top && YCoord <=DiceShell[i].bottom){
    										FillRect(g_Hdc,&DiceShell[i],RedBrush);
    										FrameRect(g_Hdc,&DiceShell[i],OutLineBrush);
    										Die_Selected[i]=true;
    										PrintNumbers();
    							}
    						}
    					}else{
    						if (XCoord >=DiceShell[i].left && XCoord <=DiceShell[i].right){
    							if (YCoord >=DiceShell[i].top && YCoord <=DiceShell[i].bottom){
    										FillRect(g_Hdc,&DiceShell[i],WhiteBrush);
    										FrameRect(g_Hdc,&DiceShell[i],OutLineBrush);
    										Die_Selected[i]=false;
    										PrintNumbers();
    							}
    						}
    					}
    				}
    			}
    	}
    	return(0);
    	break;
    }
    
     
    Maelstrom says thanks.
  3. Maelstrom

    Joined:
    Sep 10, 2008
    Messages:
    534 (0.22/day)
    Thanks Received:
    159
    Location:
    VA/PA
    Yeah I know about using namespace std, but from what I've read, it generally isn't advised as there can be potential conflicts (source) (although it probably doesn't matter to me since my programs aren't all that complex and such). But yeah, I've made a number of modifications and added some more stuff to the program as well. This will probably be my main project until school is over (I'd like it to be able to help me with all my math homework, and maybe even chem homework :roll:)
     
  4. Brandenburg

    Brandenburg New Member

    Joined:
    Feb 13, 2011
    Messages:
    171 (0.11/day)
    Thanks Received:
    31
    Location:
    Waco,Texas
    Thx for the link.. When I was in school, they told us to the the using namespace std;
     
  5. Maelstrom

    Joined:
    Sep 10, 2008
    Messages:
    534 (0.22/day)
    Thanks Received:
    159
    Location:
    VA/PA
    Yeah I was taught that too, but then they also taught us to use system("pause"), which is a huge no-no.
     
  6. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (0.95/day)
    Thanks Received:
    732
    Location:
    Philly
    You're only hearing that (and the "using std" bit) from veteran programmers who are working on non-trivial programs. For a program of the complexity you wrote it's not exactly going to break the bank with performance/portability; it's just "good practice" and programmers like their "good habits". :laugh:

    I still use "printf()" for debugging, and I know I shouldn't. ;)

    I didn't post earlier because I don't want to be a dick but you know there are quite a few (free) math libraries for C/C++ that implement those functions:eek:. I still think you wrote a solid program and I like your commenting style.:toast:
     
    Maelstrom says thanks.
  7. Maelstrom

    Joined:
    Sep 10, 2008
    Messages:
    534 (0.22/day)
    Thanks Received:
    159
    Location:
    VA/PA
    Yeah, I know why they tell us to use them, and for my programs it doesn't matter if I use them or not, but I still like doing it the more "correct way" I guess. As for your second comment: well that wouldn't be fun! I could of taken the easy way out and just had mathematica solve my math problems, but that wouldn't have given me c++ experience, lol
     
  8. Brandenburg

    Brandenburg New Member

    Joined:
    Feb 13, 2011
    Messages:
    171 (0.11/day)
    Thanks Received:
    31
    Location:
    Waco,Texas
    I believe in commenting based on the job i'm doing.. If I know others might need to maintain my code in the future, ill comment more heavily but if its just for me.. Ill comment ever so often and only at the function/procedure level.. Rarely line by line unless it is ASM..

    I was involved in a class project to program and build a stop light and to control a sequence of lights via microcontroller (PIC16F87XA) Since I had the most programming experience, I volunteered to do the bulk of the programming... No one argued with me BTW.. LMAO.. Mainly my part was a menu system via buttons and lights on a circuit board,debouncing of those buttons,timer,Demo mode and to incorporate everybody's light sequences into the program..NOW this project I commented ALOT because future students in the following semesters might edit the code... Its easy to read FOR ME.. but for others with little or no coding skills .. it might be a bit confusing

    On my personal projects, I just use very descriptive qualified names and that is sufficient in most cases to get away with few comments..Proper organization of the code helps I also mainly code in visual Basic and its more readable than C++ IMO

    Here is the code for the microcontroller..Just thought id post it... Pretty simple EVEN though the PIC High C compiler is quirky as hell. Spent ALOT of time wrestling with that.. The demo.cpp is left out .. basicall its just all the sequences in a big main loop and a loop for each sequence.. Iterations can be varied by the GLOBAL.H file.. Organizational of a programn are just as important as comments IMO.. DIGBIRT function is mine btw.. Wasnt obligated to write a light sequence but i did it.. yea.. im a showoff .. I also modified everyones code to work with the main code.I used defines on the LEDS and wanted it to be consistent throughout.:rockout:
    TRAFFICLIGHTS.ccp
    Code:
    //Neil W. Reich
    //4-1-10
    
    //CREDITS:
    
    #include <htc.h>
    #include "Sequences.h"
    #include "Demo.h"
    
    //*********************************
    // CONFIGURATION FLAGS	
    //DEBUGGER MODE			
    __CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN &
    DEBUGEN & DUNPROT & UNPROTECT);
    
    ////PROGRAMMER MODE
    //__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPEN & WRTEN &
    //DEBUGEN & DUNPROT & UNPROTECT);
    
    //Prototyped functions
    void Init();
    void ProgramStart();
    
    //Variable Declarations
    int Selection;
    long Idle;
    bit DemoFlag;
    
    //Functions
    void Init(){
    	ADON=0;			//Analog turned off
    	PORTA=0;			
    	PORTB=0;			
    	TRISB= 0b00000001;			//Sets port B0 for input
    	TRISA4=1;			//Sets port A for input
    	Selection=0;
    	Idle=0;
    	DemoFlag=0;
    }
    /////////////////START OF PROGRAM////////////////////////////
    void main(){
    	Init();
    	ProgramStart();
    /////////////////////M E N U /////////////////////////
    		switch(Selection){
    			case 1:
    				FancyDance_LEE();
    				break;
    			case 2:
    				StopLight_MC();
    				
    				break;
    			case 3:
    				Chaser_Willie();
    				break;
    			case 4:
    				SpeedTrap_LEE();
    				break;
    			case 5:
    				thingamajig_Jensen();
    				break;
    			case 6:
    				DOGBIRT();  
    				break;
    			case 7:
    				DEMO();
    				break;
    			default:
    				 DEMO();
    				break;
    		}
    }
    void ProgramStart(){
    	do{
    		Idle++;						//counts inactivity
    		if(is_button_RA4_pressed()==1){//listens for button activity.  a return value of 1 is yes,0 is no
    			Idle=0;						//resets because of Activity
    			Selection++;					//counts the times the butten is pressed
    				PORTB=Selection<<1;	//Displays the count in binary
    				if (Selection>7){			//resets the counter when 7 is reached
    					Selection=1;
    					PORTB=Selection<<1;
    				}
    		}
    							
    		Delay(3);
    		if (Idle > LIMIT-1){		//Starts demo if true
    			Idle=0;
    			DEMO();
    			Init();
    		}
       	}while(is_button_RB0_pressed() ==0);	//Exits loop when RB0 is pressed
    }
    
    BUTTONS.H
    Code:
    //DELAY FOR LED'S AND BUTTONS
    void Delay( int duration){
    	unsigned int i;
    	for(;duration!=0;duration--){
    		for(i=0;i<=TIME;i++){
    			NOP();
    			NOP();
    			NOP();
    		}
    			NOP();
    			NOP();
    	}
    }
    ///////////////////DEBOUNCED BUTTONS//////////////////////////
    unsigned char is_button_RA4_pressed(void){
    	if (RA4==0)	{				// is button pressed?
    		Delay(60);				// wait 
    		if (RA4==0)	{			// is button still has pressed status after delay?
    			return 1;			//  if key pressed,return 1
    		}
    	}
    		return 0;		// if key is not pressed, return 0
    }
    unsigned char is_button_RB0_pressed(void){
    	if (RB0==0)	{				// is button pressed?				
    		Delay(60);				// wait 
    		if (RB0==0)	{			// is button still has pressed status after delay?
    			return 1;			//  if key pressed,return 1
    		}
    	}
    	return 0;					// if key is not pressed, return 0
    }
    
    GLOBAL.H
    Code:
    //CALIBRATION DATA
    #define 	TIME 			100			//for calibrating the delay.. # of NOP's X3
    #define 	LIMIT			2000		//Maximum inactivity( interations of while) allowed
    
    #define 	MAXDEMO		1			//Maximum time demo will repeat
    //Used for Dogbirt function only
    #define		BASEDELAY	10			//Default Delay Time
    #define		MAXTIMES	10			//Maximum interations in one direction
    #define		PLACER			50			//Multiplier for delay
    
    SEQUENCES.H
    Code:
    //Neil W. Reich
    //4-0-10
    #include <htc.h>
    #include "Global.h"
    
    //Header file that contains all of the various light sequences
    //DEFINES LEDS
    #define 	RED			0x02
    #define 	YELLOW		0x04
    #define 	GREEN			0x08
    #define		CLEAR			0x00
    //***************LIGHT SEQUENCES**************
    void SpeedTrap_LEE(){
    	while (is_button_RB0_pressed() ==0){
    		PORTB = GREEN;
    		Delay(700);
    	
    		PORTB =YELLOW;
    		Delay(150);
    	
    		PORTB = RED;
    		Delay(700);
    	}
    }
    void FancyDance_LEE(){
    	while (is_button_RB0_pressed()==0){
    		PORTB = GREEN+RED;
    		Delay(400);
    
    		PORTB = YELLOW;
    		Delay(100);
    
    		PORTB =GREEN+RED;
    		Delay(400); 
    	}
    }
    void StopLight_MC(){
    	while (is_button_RB0_pressed()==0){
    		PORTB = RED;
    		Delay(400);
    	
    		PORTB = YELLOW;
    		Delay(400);
    	
    		PORTB =RED;
    		Delay(400);
    	
    		PORTB = GREEN;	
    		Delay(400);
    
    		PORTB = YELLOW;
    		Delay(400);
    	
    		PORTB =GREEN;
    		Delay(400);
    	}
    }
    void Chaser_Willie(){
    	while (is_button_RB0_pressed()==0){
    		PORTB = RED;
    		Delay(253);
    
            PORTB =YELLOW;
    		Delay(253);
    
            PORTB = GREEN;
    		Delay(253);
    
    		PORTB = YELLOW;
    		Delay(253);
    	}
    }
    void thingamajig_Jensen(){
    	while (is_button_RB0_pressed()==0){
    		PORTB =YELLOW+GREEN;
    	//	RB3=CLEAR;
    		Delay(400);
    
            	PORTB = GREEN+RED;//
    		Delay(300);
    
            	PORTB = GREEN+YELLOW;
    		Delay(300);
    
            	PORTB = GREEN+RED;//
    		Delay(300);
    	}	
    }
    void DOGBIRT(){
    	int Count=0;
    	int Count1=0;
    
    	while(1){
    		Count++;
    		if (Count<MAXTIMES){
    		//	BaseDelay=(BaseDelay+PLACER);
    			for(int j=1;j<=4;j++){
    				PORTB=1<<j;     //interation j1=2;j2=4;j3=8\
    				Delay(BaseDelay);
    			//	MaxLimit=BASEDELAY+(PLACER*Count);
    			}
    		}else{	
    			for(int j=0;j<=3;j++){
    				PORTB=8>>j;     //interation j0=2;j1=4;j2=8
    				//Delay((PLACER*Count)-MaxLimit);
    			}
    		}
    		if (Count>=MAXTIMES*2){
    			Count=0;
    		}
    	}//end while
    }
    
     
    Maelstrom says thanks.
  9. Maelstrom

    Joined:
    Sep 10, 2008
    Messages:
    534 (0.22/day)
    Thanks Received:
    159
    Location:
    VA/PA
    Very impressive Brandenburg! :toast:
     
  10. Brandenburg

    Brandenburg New Member

    Joined:
    Feb 13, 2011
    Messages:
    171 (0.11/day)
    Thanks Received:
    31
    Location:
    Waco,Texas
    thx.. was just lucky it all fell into place.. volunteering to take the lead in such a project is taking a gamble IMO.. If my part wasn't done right.. It literally effected everyone else in the class.. LOL..Main reason I volunteers because I knew I could do it better than anyone in class.. Hell.. I suspected they would of fucked it up.. Already had a CS degree and was working on my electronics degree.. So I guess I had slightly more experience being a code monkey than anyone in my department

    everyone else spent maby an hour doing their part.. I spent maby 15 in total.. but I was excused from building the modified street light for the most part even if i did do a bit of soldering... Basic street light that was modded extensively..Turned out very clean and is used as a recruitment tool now:roll:

    That compiler is damn quirky though.. You literally had to hook shit up to the circuit board in a strict sequence or you had to reboot.. Took me alot of trial and error to nail the sequence down. I got so sick of rebooting.. Had to reboot because if the sequence wasn't just right.. it would refuse to recognize the port I needed to use.. i.e. wouldn't turn on the lights.. Anyone that has ever used PIC MicroChip PicDemo 3 board knows what I mean..

    As I said... I don't do C/C++ that much anymore though.. Mainly I do Visual basic 2008 which I love. I'm much more proficient at VB..
     
  11. Maelstrom

    Joined:
    Sep 10, 2008
    Messages:
    534 (0.22/day)
    Thanks Received:
    159
    Location:
    VA/PA
    Yeah, I think my father taught himself VB recently and is writing an options trading program to work along with excel.

    As for my program, I now have it solving determinants of an an nxn matrix (I have tested a 4x4 and a 5x5, it calculated the right answer for both). All I can say is, yay for recursion!
     
    Last edited: Feb 23, 2011

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page