Thread: error: undeclared (need some help)

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    9

    Need some logic help (another problem)

    Hi everyone. I'm currently trying to learn programming (taking a class for it) but am stuck on why this isn't working. I'm sure its something stupid. This is the program in question:

    main.cpp:
    Code:
    #include <iostream>
    #include "Segment.h"
    using namespace std;
    
    
    int main()
    {
    	//Decleration of varibles.
    	int option;
    	float x1, x2, y1, y2, X, slope;
    
    	Segment segment1;
    
    	//the initial looping statement.
    	do
    	{
    		//The menu in which the user has a choice of which option to choose.
    		cout << "1- Imput two points." << endl;
    		cout << "2- Calculate slope and Y-Intercept values." << endl;
    		cout << "3- Calculate Y" << endl;
    		cout << "4- Calculate X" << endl;
    		cout << "5- exit program" << endl << endl;
    
    		cin >> option;
    		cout << endl << endl;
    
    		//switch statement for which option has been chosen.
    		switch(option)
    		{
    		case 1:
                cout << endl;
    			cout << "Please enter X1" << endl;
    			cin >> x1;
    			cout << "Please enter Y1" << endl;
    			cin >> y1;
    			cout << "Please enter X2" << endl;
    			cin >> x2;
    			cout << "Please enter Y2" << endl;
    			cin >> y2;
    			cout << endl;
    
    			segment1.setPoints(x1,y1,x2,y2);
    			break;
    
    		case 2:
    			cout << "The slope is: " << segment1.getSlope() << endl;
    			cout << "The y Intercept is: " << segment1.getIntercept() << endl;
    			break;
    		case 3:
    			cout << "Enter a value for X." << endl;
    			cin >> X;
    			cout << "The value of Y is: " << segment1.getY(X) << endl;
    			break;
    
    		case 4:
    			break;
    
    		case 5:
    			cout << "Have a nice day." << endl << endl;
    			break;
    		default:
    			cout << "Invalid option, try again." << endl << endl;
    
    		}
    
    	}
    	while (option!=5);
    
    
    
    	return 0;
    }
    Keep in mind that it isn't actually done yet, I haven't gotten to 'case 4' yet.

    segment.cpp:
    Code:
    #include "Segment.h"
    
    Segment::Segment()
    {
    	pointsSet = false;
    }
    
    float Segment::getIntercept()
    {
    	float yIntercept;
    
    	yIntercept=Y1-(slope*X1);
    
    	return yIntercept;
    }
    
    float Segment::getSlope()
    {
    	float slope;
    
    	slope=(Y2-Y1)/(X2-X1);
    
    	return slope;
    }
    
    float Segment::getX(float y)
    {
    	float x;
    	// add code
    	return x;
    }
    
    float Segment::getX1()
    {
    	return X1;
    }
    
    float Segment::getX2()
    {
    	return X2;
    }
    
    float Segment::getY(float x)
    {
    	float y;
    	y=(slope*x)+yIntercept;
    	return y;
    }
    
    float Segment::getY1()
    {
    	return Y1;
    }
    
    float Segment::getY2()
    {
    	return Y2;
    }
    
    bool Segment::pointsAdded()
    {
    	return pointsSet;
    }
    
    void Segment::setPoints(float x1, float y1, float x2, float y2)
    {
    	pointsSet = true;
    	X1 = x1;
    	X2 = x2;
    	Y1 = y1;
    	Y2 = y2;
    }


    edit:

    I'm getting a new problem. Right now it's giving me a garbage value for yIntercept, but it changes with what i imput. For example I imput:

    X1: 4
    Y1: 5
    X2: 6
    Y2: 6

    and I get displayed to the screen:

    The slope is: 1
    The y Intercept is: -3.16992e+034


    so obviously it isn't calculating the y Intercept correctly. Any ideas?
    Last edited by Nadril; 02-22-2008 at 12:24 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is slope a member variable of Segment? The only variable named slope that I see in your given code is a local variable in main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Very stupid question, but how would I go along making it a member variable?

    The thing that is bugging me is that X1,X2,Y1,Y2 are all working fine for other parts of the program but slope or yIntercept isn't.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Actually I believe I just got it. Only problem now is a bit of a logic issue (getting the wrong number for yIntercept) but I don't think that will be difficult.

    Thanks for the help.


    edit: it's giving me a garbage value (I think) now. Whenever I get to calculating the Y intercept it shows me some bogus number, any ideas?
    Last edited by Nadril; 02-22-2008 at 12:13 PM.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Some variable is probably uninitialized.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Just updated the main post. I no longer get any errors but it is giving me a bad value for y Intercept.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you should post "Segment.h".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    segment.h
    Code:
    class Segment
    {
    	private:
    		float X1;
    		float Y1;
    		float X2;
    		float Y2;
    		float slope;
    		float yIntercept;
    		bool  pointsSet;
    
    	public:
    		Segment();
    		float getIntercept();
    		float getSlope();
    		float getX(float);
    		float getX1();
    		float getX2();
    		float getY(float);
    		float getY1();
    		float getY2();
    		bool pointsAdded();
    		void setPoints(float, float, float, float);
    };

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, now it is clear that Segment has a yIntercept member variable, but you create your own yIntercept local variable in getIntercept() for some reason. Also, you should initialise all member variables in the constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Well when I take out float yIntercept; in getIntercept() it didn't actually do anything. If I take it out of the header file than it gives me an error.

    Sorry if I'm not really grasping what you're saying here.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well when I take out float yIntercept; in getIntercept() it didn't actually do anything. If I take it out of the header file than it gives me an error.
    What is getIntercept() supposed to do?

    Sorry if I'm not really grasping what you're saying here.
    Do you understand the concept of scope?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you know what a constructor is and what initialize means?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    What is getIntercept() supposed to do?
    It's just supposed to get the value of the yIntercept for the points and slope you entered earlier.

    Do you understand the concept of scope?
    For the most part I do, yes.


    Do you know what a constructor is and what initialize means?
    constructor is whats in .h if I'm not mistaken (where you declare the class). Initialize is stuff such as float yIntercept or int X, it's where you declare the variable.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It's just supposed to get the value of the yIntercept for the points and slope you entered earlier.
    What do you mean by "get"? Clearly it is supposed to return a value, but does it also compute the value?

    For the most part I do, yes.
    Then why are you hiding your member variables with local variables of the same name?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    What do you mean by "get"? Clearly it is supposed to return a value, but does it also compute the value?
    It does. It is supposed to compute the value and return it.

    Then why are you hiding your member variables with local variables of the same name?
    With the way it was set up the instructor was initializing those variables locally (yIntercept and slope) so I only put them in the .h file after it was giving me an error message (the one I had earlier).


    Either way I got rid of the initialization in the local part of the program and it didn't change anything.

Popular pages Recent additions subscribe to a feed