Classes Help

Passing char array.

Page 1 of 1

6 Replies - 594 Views - Last Post: 14 March 2010 - 08:26 AM Rate Topic: -----

#1 ahaykal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 29-April 09

Classes Help

Posted 13 March 2010 - 01:41 AM

I have a homework which states that I should do a class called course. It has several data members such as course name and the credits etc... Everything worked except when i want to pass an array to the member function. I get an error and the program exits. I will post the class:

class course
{
	private:
        char Type;
		int Credit;
		int maxStud;
		char Title[29];
		char crsName[7];
	   
	     
	
public:
		course();
	    void set_Credit(int);
	    void get_Credit();
	    
	    void set_maxStud(int);
	    void get_maxStud();
	   
		void set_Type(char);
		void get_Type();
		

		

        void set_crsName(char []);
        void get_crsName();
};



Constructor:
course::course()
{
	Credit = 3;
	maxStud = 20;
	Type = 'U';
	Title[0] = '\0';
	
}



These are the functions :

The set_crsName and get_crsName are the functions that are not working:




void course::set_crsName(char name[])
{
     strncpy(crsName , name, 7);
     name[2] = '\0';
}


void course::get_crsName()
{
     for (int i = 0; i <29 ; i++)
         cout<<crsName[i];
         
}



The main function:
course math;

math.set_crsName("MAT");


Could you guide me on the problem please.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Classes Help

#2 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Classes Help

Posted 13 March 2010 - 02:21 AM

Is it necessary in your homework to use a char array?

This post has been edited by Tapas Bose: 13 March 2010 - 02:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Classes Help

Posted 13 March 2010 - 03:05 AM

Quote

Everything worked except when i want to pass an array to the member function. I get an error and the program exits.

Please mention the error.
Was This Post Helpful? 0
  • +
  • -

#4 ahaykal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 29-April 09

Re: Classes Help

Posted 13 March 2010 - 03:40 AM

I could also use a string.

Thanks for your help!

Also the error is this :

Attached image(s)

  • Attached Image

This post has been edited by ahaykal: 13 March 2010 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: Classes Help

Posted 13 March 2010 - 03:53 AM

From reading the code, two errors:
void course::set_crsName(char name[])
{
     strncpy(crsName , name, 7);
     // you are acessing the argument, not your own array
     name[2] = '\0';   //
}


void course::get_crsName()
{
     // crsName is only 7 bytes long, you are accessing past the array boundary
     for (int i = 0; i <29 ; i++)
         cout<<crsName[i];
         
}


and:
From the screenshot I get the idea that you are not testing your program in Debug mode.
Please try to run the program in Debug, you will get much more helpful errors, and the debugger will show you where things go wrong.

This post has been edited by Ferencn: 13 March 2010 - 03:55 AM

Was This Post Helpful? 1
  • +
  • -

#6 ahaykal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 29-April 09

Re: Classes Help

Posted 14 March 2010 - 02:14 AM

View PostFerencn, on 13 March 2010 - 02:53 AM, said:

From reading the code, two errors:
void course::set_crsName(char name[])
{
     strncpy(crsName , name, 7);
     // you are acessing the argument, not your own array
     name[2] = '\0';   //
}


void course::get_crsName()
{
     // crsName is only 7 bytes long, you are accessing past the array boundary
     for (int i = 0; i <29 ; i++)
         cout<<crsName[i];
         
}


and:
From the screenshot I get the idea that you are not testing your program in Debug mode.
Please try to run the program in Debug, you will get much more helpful errors, and the debugger will show you where things go wrong.



I still Do not know how to debug :(.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_bou el layl*


Reputation:

Re: Classes Help

Posted 14 March 2010 - 08:26 AM

for(int =0,i<=6,i++){
x[i]
if(x[i]>='A'&&>='Z') continue
elseif (x[i]>='0'&&<='9')
contine
else
x[0]='\0';]
Was This Post Helpful? 0

Page 1 of 1