Thread: can somebody help me with my homework in c++

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    can somebody help me with my homework in c++

    Can somebody there tell me whats wrong I do , that the Fireworks eploding and the color don't work?

    I think is wrong with the rocket.cpp.

    Please help me

    //Gosse

    [tag]
    Code:
     
    
    //Rocket.h
    
    #ifndef ROCKET
    # define ROCKET
    //#include <fstream>
    //#include <iostream>
    //using namespace std;
    
    #define  EXPLOSION_LEVEL 4
    
    
    class CRocket
    
    {
    
    private:
    
    int mColorRise;
    
    int mColorExplosion;
    
    
    public:
    
    void Rise(int TopDistance);
    
    void Explode(int ExplosionLevel);
    
    
     
    CRocket();
    
    ~CRocket();
    
    };
    #endif
    //fireworks.cpp
    
    #include "rocket.h"
    
    
    int main()
    
    {
    
    CRocket FirstRocket;
    
    FirstRocket.Rise(EXPLOSION_LEVEL);
    
    FirstRocket.Explode(EXPLOSION_LEVEL);
    
    
    return 0;
    
    } 
    
    //Rocket.cpp
    
    
    # include "curses.h"
    # include "rocket.h"
    
    #include <iostream>
    using namespace std;
    
    CRocket::CRocket()
     
    
      {
    	initscr();
    
    	 
    	  
    
                  start_color();	
    	
    	   init_pair(1, COLOR_CYAN, COLOR_BLACK);
    	  init_pair(2, COLOR_RED, COLOR_BLACK);
            mColorRise = 1;
    	  mColorExplosion = 2;
    	
    
    	  curs_set(0);  //hide cursor
    }//Initialize
    			
    			
    			
     CRocket::~CRocket()
    {
    	timeout(-1); // turn timeout off
    printw("\nDestructor called \n");
    refresh();
    getch();
    endwin();
    //return ;
    
    //{ cout << "Destructor called" << endl; }  
    	//printw("\n Press any key to exit\n");
    
    
    } 
    
    
    	
    
    void CRocket::Rise(int TopDistance)
    {
    	
    
    	int row,col,i;
       getmaxyx(stdscr,row,col);		/* get the number of rows and columns */
     
     //void  Rise(int TopDistance) ;    
       attron(mColorRise = 1);
       
    // mColorRise = 1;
     
    
    for(i=row; i> EXPLOSION_LEVEL;i--)
    	{
    		mvprintw(i, col/2,"%c", '|');
    		refresh();
    		timeout(100);  //hack to get a delay. We wait 100ms for user input
    		getch();    //then move on as the user does nothing
    	//return ;
    }
    //CRocket::mColorRise=1;  
    
    
    }//Rise
          void CRocket:: Explode(int ExplosionLevel)
    	{
    		
    int col,row, i;
    	//CRocket::mColorExplosion=2;
    
    	getmaxyx(stdscr,row,col);
    	mvprintw(ExplosionLevel, col/2,"%c", 'X');
    	refresh();
    	{
    	for(i=0; i< 3;i++)
    	
    	mvprintw( ExplosionLevel - + i, col/ 2-i,"%c", 'X');
    	mvprintw( ExplosionLevel    + i, col/2 +i,"%c", 'X');
    	mvprintw( ExplosionLevel  + i, col/2 -i,"%c", 'X');
    	mvprintw( ExplosionLevel  - i, col/ 2+i,"%c", 'X');
    	flash();
    	refresh();
    	timeout(1000);  //hack to get a delay. We wait 100ms for user input
    	getch();  
    	erase();  //then move on as the user does nothing
    	//counter++;
    	}
    	   //CRocket::mColorExplosion=2;
    }//Explode
    [/tag]

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    hello there, Is there sombody who want to help me with programming. I don,t anderstand what wrong I do

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You will need to tell us what is wrong with your program. What operating system, what special libraries, etc.

    Jim

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by jimblumberg View Post
    You will need to tell us what is wrong with your program. What operating system, what special libraries, etc.

    Jim
    I need to program the code so that it can shoot a rocket with two different colors.
    The problem is that the colors are not visible and that the rocket looks odd.
    There are three different files, header, and rocket.cpp fireworks.cpp. constructor and distruktor

    Must include curses.

    grateful for the help

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You may want to look at this link for setting colors in curses

    You need to call the attron() function with the pair number.
    Code:
    attron(COLOR_PAIR(1));
    Also you should not be using initscr() in your class constructor, this function should probably only be called once, not once for every class instance. Just call this function in main.

    Jim

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Thank you very much, but what do you mean that I will not use initscr ();

    My rocket looks uneven when it explodes, what can it be wrong?

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Can you give me some tips about what I can do with the rocket so the rocket can explode normally?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Thank you very much, but what do you mean that I will not use initscr ();
    The initscr() function initializes the structures required by the curses library. It should only be called once. If you put it in your class you could call it multiple times, each time you make another instance of your class.
    Can you give me some tips about what I can do with the rocket so the rocket can explode normally?
    I would start by looking at your Explode function.

    How many of the following lines do you think will be executed in each iteration of the for loop?
    Code:
    	for(i=0; i< 3;i++)
    	
    	mvprintw( ExplosionLevel - + i, col/ 2-i,"%c", 'X');
    	mvprintw( ExplosionLevel    + i, col/2 +i,"%c", 'X');
    	mvprintw( ExplosionLevel  + i, col/2 -i,"%c", 'X');
    	mvprintw( ExplosionLevel  - i, col/ 2+i,"%c", 'X');
    	flash();
    	refresh();
    	timeout(1000);  //hack to get a delay. We wait 100ms for user input
    	getch();  
    	erase();  //then move on as the user does nothing
    Jim

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    all?
    sorry if a are stupid

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    all?
    No, just the first one. If you want multiple lines you must use braces to enclose the lines:
    Code:
    for(int a = 0; a < 1000; ++a)
       puts("This will be in the loop\n");
    puts("This one is not in the loop\n");
    for(int a = 0; a < 1000; ++a)
    {
       puts("This will be in the loop\n");
       puts("This is also in this loop\n");
    }
    Jim

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Thank you, Thank you is an angel, you do not know how helpful you are, thank you very much.
    Hugs
    (I'm a girl, so you do not believe: D)

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    have to question if this task.


    how do you create a rocket setup - class. An object of this class sets up the color pairs and have a method which writes the color codes in the couple's two public variables. Rocket-class constructor has a pointer of type CRocketSetup as arguments, so it will rocket to setup - the object's variables.


    Rocket setup-class constructor / destruction handle curses-specific measures


    Code:
    # include "curses.h"
    # include "rocket1.h"
    # include "rocket.h"
    #include <iostream>
    using namespace std;
    
    
    
      CRocketSetup::CRocketSetup()
    	{
    initscr();
    
    	 
    
                  start_color();	
    	
    	   init_pair(1, COLOR_CYAN, COLOR_BLACK);
    	  init_pair(2, COLOR_RED, COLOR_BLACK);
            mColorRise = 1;
    	  mColorExplosion = 2;
    	
    
    	  curs_set(0);  //hide curso
    
    	}
     
      
      
      
      
      CRocketSetup::~CRocketSetup()
    	{
    timeout(-1); // turn timeout off
    printw("\n Press any key to exit \n");
    refresh();
    getch();
    endwin();
    //return ;
    
    
    
    }
    and main .cpp
    Code:
    # include "curses.h"
    #include "Rocket1.h"
    
    
    int main() 
    { 
    CRocketSetup MyRocket;
    
    	
    	initscr();
    	return(0);
    }
    Can some one tell me where is wrong?
    Last edited by gosse; 04-27-2011 at 12:22 PM.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please post your current code, including the class.

    Jim

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Cpp.file
    Code:
    # include "curses.h"
    # include "rocket.h"
    
    #include <iostream>
    using namespace std;
    
    CRocket::CRocket()
     
    
      {
    	initscr();
    
    	 
    
                  start_color();	
    	
    	   init_pair(1, COLOR_CYAN, COLOR_BLACK);
    	  init_pair(2, COLOR_RED, COLOR_BLACK);
            mColorRise = 1;
    	  mColorExplosion = 2;
    	
    
    	  curs_set(0);  //hide cursor
    }//Initialize
    			
    			
    			
     CRocket::~CRocket()
    {
    	timeout(-1); // turn timeout off
    printw("\n Press any key to exit \n");
    refresh();
    getch();
    endwin();
    //return ;
    
    
    
    } 
    
    
    	
    
    void CRocket::Rise(int TopDistance)
    {
    	
    
    	int row,col,i;
       getmaxyx(stdscr,row,col);		
     
       attron(COLOR_PAIR(1));
    
     
     
    
    for(i=row; i> EXPLOSION_LEVEL;i--)
    	{
    		mvprintw(i, col/2,"%c", '|');
    		refresh();
    		timeout(100);  //hack to get a delay. We wait 100ms for user input
    		getch();    //then move on as the user does nothing
    	
    }
    
    attroff(COLOR_PAIR(1));
    
    }//Rise
          void CRocket:: Explode(int ExplosionLevel)
    	{
    		
    int col,row, i;
    	attron(COLOR_PAIR(2));
    
    
    	getmaxyx(stdscr,row,col);
    	mvprintw(ExplosionLevel, col/2,"%c", 'X');
    	refresh();
    	
    	for(i=0; i< 3;i++)
    	{
    	mvprintw(ExplosionLevel + i, col/2-i,"%c", 'X');
    	mvprintw(ExplosionLevel - i, col/2+i,"%c", 'X');
    	mvprintw(ExplosionLevel - i, col/2-i,"%c", 'X');
    	mvprintw(ExplosionLevel + i, col/2+i,"%c", 'X');
    	}
    	
    	//mvprintw( ExplosionLevel -i, col/ 2+i ,"%c", 'X');
    	
    	flash();
    	refresh();
    	timeout(1000);  //hack to get a delay. We wait 100ms for user input
    	getch();  
    	erase();  //then move on as the user does nothing
    	
    	
    	   
    	attroff(COLOR_PAIR(2));
    	
    }//Explode

    Current code:
    Code:
    //fireworks demo application, spring of 2011
    // more complex version of the assignment used in the first programming course
    //uses a struct to keep rocket properties: add functions for initialization, 
    //rocket rise and explosion
    
    #include <curses.h> 
    #include "fireworks.h"
    
     
    
    int main() 
    { 
     rocket my_rockets[2];
    int level;
    	int counter;
    	initscr();
    
    Initialize(my_rockets, 2);
    counter =0;
    
    while(counter < 2)
    {
    level = Rise(&my_rockets[counter]);
    Explode(&my_rockets[counter], level);
    	
    	timeout(1000);  
    	getch();   
    	erase();
    	counter++;
    }//while
    
    timeout(-1); // turn timeout off
    printw("\n Press any key to exit\n");
    refresh();
    getch();
    endwin();
    return 0;
    } 
    
    
    void Initialize( rocket *init_rockets, int amount)
    {
    	start_color();	
    	// gör det med en loop enligt amount-variabeln
    	init_pair(1, COLOR_CYAN, COLOR_BLACK);
    	init_pair(2, COLOR_RED, COLOR_BLACK);
    	init_rockets -> color_explosion = 1;
    	init_rockets ->color_rise = 2;
    
    	init_rockets++;
    	init_rockets ->color_explosion = 2;
    	init_rockets ->color_rise = 1;
    
    curs_set(0);  //hide cursor
    }//Initialize
    
    
    int Rise(rocket *rising_rocket)
    {
    	int row,col,i;
    getmaxyx(stdscr,row,col);		/* get the number of rows and columns */
    
    attron(COLOR_PAIR(rising_rocket -> color_rise));
    
    for(i=row; i> EXPLOSION_LEVEL;i--)
    	{
    		mvprintw(i, col/2,"%c", '|');
    		refresh();
    		timeout(100);  //hack to get a delay. We wait 100ms for user input
    		getch();    //then move on as the user does nothing
    	}
    attroff(COLOR_PAIR(rising_rocket ->color_rise));
    
    return i;
    }//Rise
    
    void Explode(rocket *exploding_rocket,)
    {
    int col,row, i;
    	attron(COLOR_PAIR(exploding_rocket->color_explosion));    
    
    	getmaxyx(stdscr,row,col);
    	mvprintw(level, col/2,"%c", 'X');
    	refresh();
    	
    	for(i=0; i< 3;i++)
    	{
    	mvprintw(level - i, col/2+i,"%c", 'X');
    	mvprintw(level + i, col/2-i,"%c", 'X');
    	mvprintw(level + i, col/2-i,"%c", 'X');
    	mvprintw(level - i, col/2+i,"%c", 'X');
    	flash();
    	refresh();
    	timeout(100);  //hack to get a delay. We wait 100ms for user input
    	getch();    //then move on as the user does nothing
    	}
    	attroff(COLOR_PAIR(exploding_rocket->color_explosion)); 
    
    }//Explode
    the header:
    Code:
    #ifndef ROCKET
    # define ROCKET
    
    #define  EXPLOSION_LEVEL 4
    
    
    class CRocket
    
    {
    
    private:
    
    int mColorRise;
    
    int mColorExplosion;
    
    
    public:
    
    void Rise(int TopDistance);
    
    void Explode(int ExplosionLevel);
    
    
     
    CRocket();
    
    ~CRocket();
    
    };
    #endif
    Last edited by gosse; 04-27-2011 at 12:51 PM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Does this compile without error/warning messages?

    If not please post the complete error messages just as they appear in your development environment.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More Homework ??
    By dolfaniss in forum C Programming
    Replies: 4
    Last Post: 04-19-2011, 01:25 PM
  2. A little homework help?
    By nwkegan in forum C++ Programming
    Replies: 11
    Last Post: 03-07-2010, 09:45 AM
  3. Do my homework
    By heiroglikano in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 06:26 AM
  4. help with homework
    By abhiii in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 01:48 PM