Thread: Basic problem in C++ Pls help

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    14

    Basic problem in C++ Pls help

    Hi all ,

    Pls let me is anything wrong with this code

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void printpoint(struct point *p);
    void readpoint(struct point *p);
    
    struct point
    {
    public :
    int x;
    int y;
    char ch;
    
    	void readpoint(struct point *p)
    	{
    		printf("\n Enter the first value ");
    		scanf("%d",&(*p).x);
    		printf("\n Enter the second value ");
    		scanf("%d",&(*p).y);
    		printf("\n Enter the charecter to be entered ");
    		p->ch=getch();
    	}
    
    	void printpoint(struct point *p)
    
    	{
    		gotoxy(p->x,p->y);
    		putch(p->ch);
    	}
    
    };
    
    void main()
    {
    struct point p1,p2;
    	clrscr();
    	point.readpoint(&p1);
    	point.printpoint(&p1);
    	getch();
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean the use of nonstandard conio.h

    Or the use of void main

    Or the fact you posted your "C++" code on the C board (it's moved by the way)

    Or the fact that since you've created member functions for the struct, there is in fact no need to pass a pointer to yourself, since you implicitly get a 'this' pointer anyway.

    What did your compiler tell you?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    14
    HI salem

    At first where this post has been moved . I searched but could'ntt get it .

    My compiler gave an error : Improper use of Typedef "point"

    regards
    Anil

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    [Edited by Kermi3]here's the point: there's no standard way to do what you want to do. I fixed all your logic errors, now it's up to you to find a way to adapt this to your needs:
    Code:
    //#include <stdio.h>	<-- let's stick with C++ here, okay?
    //#include <conio.h>	<-- let's not use non-standard headers...
    
    #include <iostream>
    
    //the followng declarations are only right because I made them right.
    struct pnt;
    void prntpnt(struct pnt *p);
    void readpnt(struct pnt *p);
    
    struct pnt
    {
    	//public :	//it's a struct, things are public by default.
    	int x;
    	int y;
    	char ch;
    };	//you forgot your semicolon here
    
    void readpnt(struct pnt *p)
    {
    	std::cout<<"Enter the first value: ";
    	std::cin>>p->x;
    	std::cout<<"Enter the second value: ";
    	std::cin>>p->y;
    	std::cout<<"Enter the charecter to be entered: ";
    	std::cin>>p->ch;
    }
    
    void prntpnt(struct pnt *p)
    {
    	//gotoxy(p->x,p->y);	<-- can we avoid this, please?
    	//putch(p->ch);		<-- maybe you should work out basic data
    	//				structures before moving into stuff
    	//				like this...
    	std::cout<<"X axis: "<<p->x<<"\nY axis: "<<p->y
    		<<"\nChar: "<<p->ch<<std::endl;
    }
    
    //void main() <-- NO NO NO NO NO NO NO NO NO NO
    int main()
    {
    	struct pnt p1;	//you declared p2 but never used it
    	//clrscr(); <-- This is nonstandard, please don't use it
    	readpnt(&p1);
    	prntpnt(&p1);
    	//getch();	//NO.
    	std::cin.get();
    	return 0;	//returning values is good.
    }
    [edited by K3]I changed your print and point names because it was the easy way to fix the colors my sytnax highlighter was throwing in there...

    and I got bored, so here's an additional "classy" example:
    Code:
    #include <iostream>
    
    class pnt
    {
    	public:
    		pnt(const int x=-1,const int y=-1,const char ch='\0');
    		~pnt();
    		void readpnt(const int x=-1,const int y=-1,const char ch='\0');
    		void prntpnt();
    	private:
    		int x;
    		int y;
    		char ch;
    };
    
    pnt::pnt(const int x,const int y,const char ch)
    {
    	this->x=x;
    	this->y=y;
    	this->ch=ch;
    }
    pnt::~pnt(){}
    
    void pnt::readpnt(const int x,const int y,const char ch)
    {
    	if(x==-1)
    	{
    		std::cout<<"Enter the X value: ";
    		std::cin>>this->x;
    		std::cin.ignore(1);
    	}
    	else
    	{
    		this->x=x;
    	}
    	
    	if(y==-1)
    	{
    		std::cout<<"Enter the Y value: ";
    		std::cin>>this->y;
    		std::cin.ignore(1);
    	}
    	else
    	{
    		this->y=y;
    	}
    	
    	if(ch=='\0')
    	{
    		std::cout<<"Enter the charecter to be entered: ";
    		std::cin>>this->ch;
    		std::cin.ignore(1);
    	}
    	else
    	{
    		this->ch=ch;
    	}
    }
    
    void pnt::prntpnt()
    {
    	std::cout<<"X axis: "<<x<<"\nY axis: "<<y
    		<<"\nChar: "<<ch<<"\n\n"<<std::flush;
    }
    
    int main()
    {
    	pnt p1,p2,p3(10,20,'x');
    
    	p2.readpnt(1,2,'3');
    	p1.readpnt();
    	
    	p1.prntpnt();
    	p2.prntpnt();
    	p3.prntpnt();
    	
    	std::cin.get();
    	return 0;
    }
    Last edited by major_small; 02-09-2006 at 03:51 AM. Reason: color-happy syntax highlighter
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Salem


    Or the fact you posted your "C++" code on the C board (it's moved by the way)
    Why would you consider this C++ and not C?

    It uses C headers, it uses structs consistent with the way C programmers use them. It has global functions to operate on those structs. The only problem is the way he tried calling those functions(which was c++ like) but major_small cleaned those up in his code.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Why would you consider this C++ and not C?
    Well there's the title of the thread
    And the member functions in the struct.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Salem
    > Why would you consider this C++ and not C?
    Well there's the title of the thread
    And the member functions in the struct.
    yea I saw the title after I posted, but as far as the member functions, there weren't any, though he was calling them as if there were.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes there are
    void readpoint(struct point *p)
    was inside the struct, and therefore that makes it C++.

    C doesn't have such a notation, so as posted it is invalid C.

    The ONLY difference between struct and class in C++ is that struct defaults public, and class defaults private. In every other respect, they are inter-changeable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Space problem...Pls help
    By Helmet_King in forum C Programming
    Replies: 3
    Last Post: 04-07-2006, 03:40 AM
  2. hi guys, pls help me with this problem
    By amits in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2004, 08:12 PM
  3. floating point unit programming problem
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-15-2003, 02:16 PM
  4. FPS Problem, pls help
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 06-21-2002, 12:03 PM
  5. Visual Basic Adodc Problem
    By rahat in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 06:55 AM