Jump to content
  • Advertisement
Sign in to follow this  
zergdeath1

I just cant get it

This topic is 5081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I now have program that is supposed to move a stick on screen but it has a problem. So here it is:
#include<iostream>
using namespace::std;


class stick {

 public:
  stick(int start=0) {int place=start;}
  void move(int distance,int time);

 private:
  int place, distance, start,time;
};

void stick::move(int distance,int time) {

for (int t=0;t<=time;t++){

 cout<<"\n";

 for(int j=start;j<=place;j++){

  cout<<" g";

 }
 for(int i=place;i<=distance;i++)
 {
 cout<<" ";
 place=place+distance;
 }

 cout<<"I";

}
}


int main(){
stick stik1;
stik1.move(3,10);
return 0;



}
It is supposed to move stick around on screen. The " g" is there becuase that is the problem, as in normally it would be a space but it just keeps repeating the space. The for loop with " g" is the problem i just cant find it. It is supposed to see where start is then where place is, then it keeps adding spaces until it hits place.

Share this post


Link to post
Share on other sites
Advertisement
I'm a bit unsure of what you really are tring to achive here,
but the constructor is wrong.
Your declaring place while place is already a member variable, plus the other variables isn't being set at all.
I would rewrite the constructor to something like this

stick( int stickstart )
{
place = stickstart;
start = 0;
distance = 0;
time = 0;
}

the same thing occurs in the move function, try change it abit to

void stick::move( int d, int t )
{
distance = d;
time = t;

// ... more code ...
}

that should probably clear up some stuffs, but I'm unsure what you mean with the " g" problem ...

Share this post


Link to post
Share on other sites

Your constructor is wrong. It should be:
stick(int start) {place=start;}

And then you should do "stick stik1(0)" in main;
good luck

Share this post


Link to post
Share on other sites
The " g" problem is that all it does is display " g", the problem is in that loop. I just dont know how to fix it. The program is supposed to move the letter I around the screen, but all i care about now is getting past the " g" problem.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <stdio.h>
using namespace::std;


class stick {

public:
stick(int xStart, int yStart);
stick();
void move(int distance,int time);

private:
int x, y;
};

void stick::move(int newX,int newY) {

x = newX; y = newY;
for (int i=0;i<y;i++) cout<<"\n";
for(i=0;i<x;i++) cout<<" ";

cout<<"I";

}

stick::stick(int xStart, int yStart) {
x = xStart;
y = yStart;}

stick::stick() {
x = 0;
y = 0;}



int main(){
stick stik1;
stik1.move(3,10);
cout << "\n\n";
return 0;
}




There ya go =)

[Edited by - Deadpenguin on October 24, 2004 11:19:48 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by zergdeath1
thx for ur help but can someone plz try to show me y mine wont work.


umm

Quote:
Original post by Willbo
I'm a bit unsure of what you really are tring to achive here,
but the constructor is wrong.
Your declaring place while place is already a member variable, plus the other variables isn't being set at all.
I would rewrite the constructor to something like this

stick( int stickstart )
{
place = stickstart;
start = 0;
distance = 0;
time = 0;
}

the same thing occurs in the move function, try change it abit to

void stick::move( int d, int t )
{
distance = d;
time = t;

// ... more code ...
}

that should probably clear up some stuffs, but I'm unsure what you mean with the " g" problem ...


and

Quote:
Original post by Mille

Your constructor is wrong. It should be:
stick(int start) {place=start;}

And then you should do "stick stik1(0)" in main;
good luck


seemed to explain a few things nicely.

Share this post


Link to post
Share on other sites
Quote:
Original post by zergdeath1
thx for ur help but can someone plz try to show me y mine wont work.


Ok Zergdeath1, your probably reading the answers to your question thinking "Why the heck does everyone keep talking about my constructor? I need to know what's wrong with my for loop!"
The PROBLEM with your for loop IS in your CONSTRUCTOR.
To understand this you need some more info on variables:
In your constructor you initialize a parameter with the same name as one of your instance variables (start). This is not the same variable as your instance variable (start). While in the constructor you now have two variables named (start): the parameter variable and the instance varaible. Parameter variables take precedence over instance variables. So if you set (start) to a value in your constructor you change the value of your parameter variable, NOT your instance variable. You make a similar mistake in your constructor with (place). You declare a local variable (place), so once again you now have two variables inside your constuctor with the same name (place): a local variable and your instance variable. Local variables also take precedence over instance variables. So when you try and change the value of (place) in your constructor you change the value of the local variable, NOT the instance variable.
Another problem with your constructor is you take in a parameter and immediately set the value of that parameter to something else (int start = 0). There is no point in passing a parameter if its never used in the constructor. <It's like asking for a pizza at a bagel store. Why bother to tell me you want a pizza when the only order I take is for a bagel?>
These errors in your constructor never allow your instance variables to be initialzed to a value and that's why your for loop doesn't work, because your for loop is trying to evaluate the expression (int j=start;j<=place;j++) but it was never told the values of (start) and (place).
NOW FOR THE GOOD NEWS! That for loop is absolutely useless anyway! Even if you had it working properly, its pointless. Your filling a line with spaces and then making a newline. Do you know the visual difference between a line with nothing but spaces and a line with just a newline is (in the console window)? THE ANSWER: nothing! Delete that for loop as it does nothing to help you in the slightest.
NOW FOR THE BAD NEWS! You still have more bugs in your program!
This line (place=place+distance;) in your last for loop shouldn't be inside the loop. Put it after the for loop. What you want this line to do is this:
place is 5. Distance is 3. After I go that Distance I'm at place 8.
With that line in the for loop your code will say your at 11. The reason is beacuse each time you move out another single space your adding the total distance each time. If you put that line after the for loop it will do what you expect it to.

Take a look at the modified version of your code I posted previously to help you understand.

Well, I hope that helps. I reccomend you hit the books and read up on classes, variable initialization, and shadowing (the effect of declaring variables of the same name within your class and your class methods)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!