Help getting program to count each position
May 16, 2017 at 1:30am UTC
So for this program I had to write and display a bug turning 180 degrees and moving one position right horizontally. How would I get the position number of each bug move to display.
So in the case below I've got position 1 which is bug display. then I move bug three times which should be position 4 correct? How would I do this?
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
#include <string>
using namespace std;
class Bug
{
int pos;
string lines[3];
public :
Bug () // Initial pos
{
pos = 0;
lines[0] = " 1 1" ;
lines[1] = "*******" ;
lines[2] = " *****" ;
}
void displayBug ()
{
system ("cls" );
for (int i=0; i<3; i++)
{ for (int j=0; j<pos; j++)
cout << " " ;
cout << lines[i] << endl;
}
}
// swap lines[0] and lines[2] for 180 degree turn
void turnBug ()
{
string temp;
temp = lines[0];
lines[0] = lines[2];
lines[2] = temp;
}
void Move ()
{
pos++;
turnBug ();
displayBug ();
}
void Position()
{
for (int i=0;i<; pos++ )
{
cout<<endl<<"Bug is at: " <<endl<<pos<<endl;
}
}
};
int main()
{
Bug bug;
bug.displayBug();//position 1
bug.Move();//after each move the bug turns 180 degrees
bug.Move();//this is in move function
bug.Move();//position 4
bug.Position();
return 0;
}
Last edited on May 16, 2017 at 3:25am UTC
May 16, 2017 at 4:52am UTC
@kirby3422
You mainly had to move the position statement.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
include <iostream>
#include <string>
using namespace std;
class Bug
{
int pos;
char x;
string lines[3];
public :
string _Right;
Bug () // Initial pos
{
pos = 1;
lines[0] = " 1 1" ;
lines[1] = "*******" ;
lines[2] = " *****" ;
}
void displayBug ()
{
system ("cls" );
for (int i=0; i<3; i++)
{
for (int j=0; j<pos; j++)
cout << " " ;
cout << lines[i] << endl;
}
cout<<"bug is now on position: " << pos << endl;
}
// swap lines[0] and lines[2] for 180 degree turn
void turnBug ()
{
string temp;
temp = lines[0];
lines[0] = lines[2];
lines[2] = temp;
}
void Move ()
{
cout<<"Press '0', then enter to see each move" <<endl;
//Enter any letter or number. Just printed a '0' for user to see.
cin>> x;
pos++;
turnBug ();
displayBug ();
}
};
int main()
{
Bug bug;
bug.displayBug();//position 1
bug.Move();
bug.Move();
bug.Move();//position 4
return 0;
}
May 16, 2017 at 5:11am UTC
Awesome I've been staring at this and have been saying that somehow its got to go in there and just couldn't figure it out thank you so much.
Topic archived. No new replies allowed.