Thread: Problem displaying (structs functions)

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Problem displaying (structs functions)

    ok in my header file i got this function:
    Code:
    [
    STRUCT myS
    {
    int Points;
    
    void Display();
    void SetPoints(int n)		{ Points = n; }
    int GetPoints()			{ return ( Points ); }
    }

    in my main function
    i set the points using this:

    Pset.SetPonts(nPoints);


    how would i display my points in display function?
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Well, it appears as if you want a simple output statement.
    Code:
    cout<< points <<endl;

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    hmm no
    that wouldnt work.

    i use the variable "Points" which is the the struct to hold my points using the Pset.SetPonts(nPoints);

    now i call the function myS:isplay()
    which is suppose to output my points in a cout statement such as this one:
    Code:
    cout<< "My points are: " << myS.GetPoints();
    but i think that doesnt work snce i am getting garbage values.

    ne suggestions?
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Post a small working example. I don't quite understand what problems you are having.

  5. #5
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    ok here is my other version of it

    my header file:
    Code:
    struct sROUND1
            {
    	   int GolferScore;
               int CoursePar;
               
    		 	//Initializes a round by asking the user the different values through IO statements
    		  	int InitRound();
    			
    		   		   
    			//Neatly outputs to the screen the different values of the round
    			void DisplayRound();
    
    					// **************** Inspectors and Modifiers, one for each property ******************//
    			
    			void SetGolferScore(int n)		{ GolferScore = n; }
    			int GetGolferScore()			{ return ( GolferScore ); }
    			void SetCoursePar(int n)		{ CoursePar = n; }
    			int GetCoursePar()				{ return ( CoursePar ); }
    			
    		};
    MAIN FUNCTION FILE:
    Code:
    #define MAX 1
    
    void main( void )
    {
    	sROUND1* ptrRound;
    	int i;
    
    	//Allocate space for MAX sROUND1 objects and make ptrRound points to them
    	for (i=0; i<MAX; i++)
    		ptrRound[i].InitRound();
    
    	for (i=0; i<MAX; i++)
    		ptrRound[i].DisplayRound();
    }
    My Functions file:
    Code:
    int sROUND1::InitRound()
    {
    	int nResult = 0;
    	//Add your code below
    	
    	//******* Initializng Variables ********
    	int x=0, nScore, nPar;
    	float fHand, fSlope;
    	char szName[50];
    	sROUND1 rnd;
    	//** Taking input from the user **
    		
    	cout << "\n Enter Golfer Score: ";
    	cin >> nScore;
    	while (nScore < 69 || nScore > 130)		//Checks if the input is valid
    	{
    		cout << " \n Invalid Input, please re-enter: ";
    		cin >> nScore;
    	}
    	rnd.SetGolferScore(nScore);				//Copies the contents of nScore into the struct		
    	
    	cout << "\n Enter Course Par: ";
    	cin >> nPar;
    	while (nPar < 68 || nPar > 74)			//Checks if the input is valid
    	{
    		cout << " \n Invalid Input, please re-enter: ";
    		cin >> nPar;
    	}
    	rnd.SetCoursePar(nPar);					//Copies the contents of nPar into the struct
    	
    	return(nResult);
    
    }
    
    void sROUND1::DisplayRound()
    {
    	//Add your code below
    	sROUND1 rnd;
    	//	cout << "\********** Displaying Data **********\n";
    	cout << "Golfer Score: " <<rnd.GetGolferScore() << endl;			//Displays GolferScore
    	cout << "Course Par: " <<rnd.GetCoursePar() << endl;				//Displays CoursePar
    	
    }
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    You have two distinct problems. First is that you try to use an uninitialized pointer. If you want a dynamic array then you must allocate the memory for it before using it. Your second problem is that you use temporary objects and think that calling their member functions will somehow effect the object you are working from.

    Find the differences between this and your code. Then try to come up with reasons for the changes.
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct sROUND1
    {
        int GolferScore;
        int CoursePar;
    
        //Initializes a round by asking the user the different values through IO statements
        int InitRound();
    
        //Neatly outputs to the screen the different values of the round
        void DisplayRound();
    
        // **************** Inspectors and Modifiers, one for each property ******************//
        void SetGolferScore(int n)		{ GolferScore = n; }
        int GetGolferScore()			{ return ( GolferScore ); }
        void SetCoursePar(int n)		{ CoursePar = n; }
        int GetCoursePar()				{ return ( CoursePar ); }
    };
    
    int sROUND1::InitRound()
    {
        int nResult = 0;
        //Add your code below
    
        //******* Initializng Variables ********
        int x=0, nScore, nPar;
        //** Taking input from the user **
    
        cout << "\n Enter Golfer Score: ";
        cin >> nScore;
        while (nScore < 69 || nScore > 130)		//Checks if the input is valid
        {
            cout << " \n Invalid Input, please re-enter: ";
            cin >> nScore;
        }
        SetGolferScore(nScore);				//Copies the contents of nScore into the struct		
    
        cout << "\n Enter Course Par: ";
        cin >> nPar;
        while (nPar < 68 || nPar > 74)			//Checks if the input is valid
        {
            cout << " \n Invalid Input, please re-enter: ";
            cin >> nPar;
        }
        SetCoursePar(nPar);					//Copies the contents of nPar into the struct
    
        return(nResult);
    }
    
    void sROUND1::DisplayRound()
    {
        //	cout << "\********** Displaying Data **********\n";
        cout << "Golfer Score: " <<GetGolferScore() << endl;			//Displays GolferScore
        cout << "Course Par: " <<GetCoursePar() << endl;				//Displays CoursePar
    }
    
    #define MAX 1
    
    void main( void )
    {
        sROUND1* ptrRound = new sROUND1[MAX];
        int i;
    
        //Allocate space for MAX sROUND1 objects and make ptrRound points to them
        for (i=0; i<MAX; i++)
            ptrRound[i].InitRound();
    
        for (i=0; i<MAX; i++)
            ptrRound[i].DisplayRound();
    
        delete [] ptrRound;
    }

  7. #7
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    if i apply the above suggested changes, then my prgram crashes after the first input, otherwise after all inputs . if i am right, u r assumin g my functions are not in struct, but since they are i have to say somthing like this
    rnd.SetGolferScore(nScore);
    rnd.GetGolferScore();

    i could be wrong tho.
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  8. #8
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    my bad u r right

    srybout that :P

    forgot this :
    sROUND1* ptrRound = new sROUND1[MAX];


    thanks
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu problem involving structs
    By NyHoK in forum C Programming
    Replies: 5
    Last Post: 03-31-2009, 10:00 AM
  2. problem w/ color functions && win98 :P
    By DarkMortar in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 04:45 PM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 4
    Last Post: 04-01-2003, 12:49 AM