Thread: the program just stops working. I don't know what is the problem with it! *frustated*

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    6

    the program just stops working. I don't know what is the problem with it! *frustated*

    Code:
    #include<iostream>
    #include<conio.h>
    #include<string.h>
    using namespace std;
    class name
    {
    
    
    	public:
    		int m;
    		int n;
    		char *p;
    		char *q;
    		name()
    		{
    			}
    				name(char *mno,int m,char *xyz,int n)
    				{
    					p=new char[m+1];
    					q=new char[n+1];
    					strcpy(p,mno);
    					strcpy(q,xyz);
    					}
    					name(name &y1)
    					{
    						strcpy(p,y1.p);
    						strcpy(q,y1.q);
    						}
    						void show()
    						{
    							strcat(p,q);
    							cout<<p;
    							}
    };
    main()
    {
    	char *name1;
    	char *name2;
    	int l1,l2;
    	cout<<"enter string1 :";
    	cin>>name1;
    	cout<<"enter string2 :";
    	cin>>name2; 
    	l1=strlen(name1);
    	l2=strlen(name2);
    	name n1(name1,l1,name2,l2);
    	name n2(n1);
    	n2.show();
    	getch();
    	return 0;
    }
    Attached Files Attached Files
    Last edited by Priyanka Mandal; 08-31-2013 at 10:31 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following snippet:
    Code:
        char *name1;
        char *name2;
        int l1,l2;
        cout<<"enter string1 :";
        cin>>name1;
    Where have you allocated memory for those pointers?

    Probably more importantly why are you using the character pointers at all? You should be using std::string instead of all those C-strings.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    Code:
    char *name1;	char *name2;
    	name1=new char[20];
    	name2=new char[20];
    	int l1,l2;
    	cout<<"enter string1 :";
    	cin>>name1;
    the problem remains the same.... i have to use pointers...and i dont know how to use std::string yet....

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Same problem of using unallocated pointers in copy constructor.

    Use std:string if you are learning C++ and skip C-strings till you are comfortable with C++ approach
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yes, you need to allocate memory in the copy constructor.

    In show() you do a strcat(), so you'll need to make sure that the allocated memory is enough to handle that.

    You wouldn't have to worry about any of this if you were using std::string. It'd all be much easier!

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    Code:
    #include<iostream>
    #include<conio.h>
    #include<string.h>
    using namespace std;
    class name
    {
    
    
    	public:
    		int m;
    		int n;
    		string p;
    		string q;
    		name()
    		{
    			}
    				name(string mno,string xyz)
    				{
    				
    					p=mno;
    					q=xyz;
    					}
    					name(name &y1)
    					{
    						string final=y1.p+y1.q;
    						cout<<"the final string is :"<<final;
    						}
    };
    main()
    {
    	string name1;
    	string name2;
    	int l1,l2;
    	cout<<"enter string1 :";
    	cin>>name1;
    	cout<<"enter string2 :";
    	cin>>name2;
    	name n1(name1,name2);
    	name n2(n1);
    	getch();
    	return 0;
    }
    thank u... std::string worked!

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I strongly suggest that you read about indent style.
    Moreover, we tend to have the members of the class as private, not as public.
    For example:
    Code:
    class name
    {
    private:
        int m;
    public:
        int getM() { return m;}
        void setM(int M) { m = M;}
    ...
    };
    This is really useful and when your projects get bigger and more than one people read/modify it, it really helps.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program suddenly stops working.
    By KittenAqua in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 05:45 AM
  2. compiler stops working
    By akkiphadnis in forum C Programming
    Replies: 2
    Last Post: 04-04-2011, 03:42 AM
  3. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:18 AM
  4. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  5. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM