Bug

I have to construct a "bug" in int main and use class to move the mug horizontally right then for each move the bug rotates 180 degrees.I have the program set up correctly I think but I just am not sure what you would use to get the bug to move horizontally and rotate. Any and all help is much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <iostream>
using namespace std;

class Bug
{
private:
bool 

Bug(int initial_position):Bug(0)
{
} //constructor

// Member functions
 void turn();
 {
 	
 	
 } // The bug turns 180 degrees; it can only go in two directions
 void move();
 {
 	
 	
 }
 int get_position();
 {
 	
 }

};





int main()
{
	cout<<"  1 1"<<endl;
	cout<<"*******"<<endl;
	cout<<" *****"<<endl;

return 0;
}
Last edited on
I have the program set up correctly I think

No, you don't.
1) You display the bug once in main and then quit.
2) You never instantiate your bug object.
3) Your functions are empty.

Line 9: Your constructor is recursive.

Lines 14,19,24: Function definitions do not end with ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
using namespace std;

class Bug
{   int position;
    string lines[3];
    
public:
    Bug ()      //  Initialize the instance 
    {   position = 0;
        lines[0] = "  1 1"; 
	    lines[1] = "*******";
	    lines[2] = " *****"; 
    }

    void Display ()
    {   system ("cls");
        for (int i=0; i<3; i++)
        {   for (int j=0; j<position; j++)
                cout << " ";                            
            cout << lines[i] << endl;
        }                            
	}
	
	//  swap lines[0] and lines[2]
	void Turn ()
	{   string  temp;
	
	    temp = lines[0];
	    lines[0] = lines[2];
	    lines[2] = temp;
    }
    	       
    void Move ()
    {   position++;
        Turn ();
        Display ();        
    }
};

int main()
{   Bug     bug;

    bug.Display();
    for (int i=0; i<40; i++)
    {   bug.Move ();
        bug.Display ();
    }        
    system ("pause");
    return 0;
}




What is that system cls operator?
When the program is ran it says it cannot open the output file. Would an error be occurring in the void display function? Is that why it is saying can not open the output file?
What is that system cls operator?
system("cls") invokes a system shell command to clear the screen on Windows. If you're on another OS, the command may be different.

When the program is ran it says it cannot open the output file.

The program I posted only writes to cout. It does not open any other output file.
cout should be opened automatically.



Okay awesome is there a way to display the bug after each move? Because the program you display runs thorough each move and displays the last one. Would that be possible?
Add the following after line 38:
1
2
 
  system ("pause");


That will pause the program after each move.

If I do that then it only prints the first position.
Okay awesome is there a way to display the bug after each move?
If I do that then it only prints the first position.

Which way do you want it?

The initial code I posted moved the bug 40 times to the right displaying each move after clearing the screen each time.

Adding the pause after line 38 requires you to press a key after each move before the next move is displayed.

That is freakin awesome thank you for your help I really appreciate it!
Topic archived. No new replies allowed.