How i write this program Please help me

M

mohammaditraders

a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.
 
D

Devon Null

a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 
P

Pramod

a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.

only copy and paste this programmm and run

#include <iostream>
#include <conio>
#include <string>
class Student
{
private:
char *name;
int Ob_marks;
int otal_marks;
public:
student();
Student(char&,int);
void cal_percentage();
void display()
};
student::student()
{
int len=strlen(name);
name=new char[len+1];
ob_marks=0;
}
student::sudent(char &n,int om)
{
int len=strlen(n);
name=new char[len+1];
strcpy(name,n);
Ob_marks=om;
}
void student::cal_percentage()
{
otal_marks=(ob_marks*100)/total_marks;
}
void display();
{
cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<endl;
}
int main()
{
student s1("mohammad",67);
s1.cal_percentage();
s1.display();
return 0;
}
 
D

Devon Null

Pramod said:
only copy and paste this programmm and run

#include <iostream>
#include <conio>

I didn't have conio on my system (should I?)
#include <string>
class Student
{
private:
char *name;

use a string here
int Ob_marks;

IMHO you should probably should make this lower case for ease of typing,
i.e. ob_marks
int otal_marks;
public:
student();
Student(char&,int);

there are 2 constructors defined here - that maybe legal, I leave that
to others more knowledgeable than me, but it would not compile on my
system. Also you should define the default values here - see below.
void cal_percentage();
void display()

missing semi-colon
};
student::student()

Typo here - C++ is case sensitive. You are trying to use student (lower
case 's') when you defined Student (upper case 'S'). Anyways, this whole
block should be done away with
{
int len=strlen(name);

not needed, use sizeof(name)
name=new char[len+1];
ob_marks=0;

doesn't match the Ob_marks defined earlier (case sensitive)
}
student::sudent(char &n,int om)

Student::Student(string n, int om)
{
int len=strlen(n);
name=new char[len+1];
strcpy(name,n);
Ob_marks=om;
}

just about this whole block should be re-worked
void student::cal_percentage()

case sensitive
{
otal_marks=(ob_marks*100)/total_marks;

total_marks? you mean otal_marks? (as defined above), otherwise
total_marks was never defined.
}
void display();

no semi-colon goes here - remove it
{
cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<endl;

have to use std:: for cout and endl since you didn't declare your
namespace earlier
}
int main()
{
student s1("mohammad",67);

again, case sensitive
s1.cal_percentage();
s1.display();
return 0;
}

Now here is a working version (at least on my system):

#include <iostream>
//#include <conio>
#include <string>

class Student
{
private:
std::string name;
int ob_marks;
int otal_marks;
public:
//Student();
Student(std::string n = "",int om = 0);
void cal_percentage();
void display();
};
/*Student::Student()
{
int len=sizeof(name);
name=new char[len+1];
ob_marks=0;
}*/
Student::Student(std::string n,int om)
{
//int len=sizeof(n);
//name=new char[len+1];
//strcpy(name,n);
name = n;
ob_marks = om;
}
void Student::cal_percentage()
{
otal_marks=(ob_marks*100)/otal_marks;
}
void Student::display()
{
std::cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<std::endl;
}
int main()
{
Student s1("mohammad",67);
s1.cal_percentage();
s1.display();
return 0;
}

Now only one problem remains - you never set otal_marks to a meaningful
value.

Anyone want to critique my advice, I am also a beginner.

DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 
B

BobR

Did you read this FAQ?
only copy and paste this programmm and run

#include <iostream>
#include <conio>

Where did this come from? Does it compile? ( I've always seen it as
"conio.h". non-standard.)

#include <string>
class Student{ private:
char *name;

int Ob_marks;
int otal_marks;
public:
student();

WHAT?
// error: ISO C++ forbids declaration of `student' with no type
Student(char&,int);
void cal_percentage();
void display()
}; // class Student
student::student(){

Does not exist.
int len=strlen(name);
name=new char[len+1];
ob_marks=0;
}
student::sudent(char &n,int om){

'sudent' is not declared/defined for, for, ... what the heck is 'student'.
int len=strlen(n);
name=new char[len+1];
strcpy(name,n);
Ob_marks=om;
}
void student::cal_percentage(){

'cal_percentage()' is not declared/defined in, in, ... what the heck is
'student'?

otal_marks=(ob_marks*100)/total_marks;
}
void display();
{
cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<endl;

'name', 'ob_marks' and 'otal_marks' were never declared/defined!
int main(){
student s1("mohammad",67);

'student' was never declared/defined!!
s1.cal_percentage();
s1.display();
return 0;
}

If you are going to do peoples homework for them, at least give them
something that will compile (or close to it)!
 
O

osmium

:

Now only one problem remains - you never set otal_marks to a meaningful
value.

Anyone want to critique my advice, I am also a beginner.

Before trying to fix a program it is nice to know what the program is
supposed to do. In this case, the original problem is upstream in this
thread. You should look at the problem statement before you try to fix
someone else's code. In this case Total_marks had to be provided in a
constructor, there is no way to deduce it from other information. Since
Premed screwed up, you also screw up, so your program can not work. Pramod
has, in general, a very casual attitude towards typing. Furthermore, he
does not know the meaning of "copy and paste", copy and paste means, I
*have* compiled this program and I assert that it *works*. There are weaker
forms to use such as "try this" or "here is something".

I too, thought the variable naming was bad. But in my experience,
instructors can sometimes be picky, why cross them? He is going to give you
a grade, which is really the point of the whole exercise. So let him have
his screwed up names, a hodge-podge of inappropriate capitalization and
underscores.
 
B

BobR

Devon Null said:
Now here is a working version (at least on my system):

And *you* should not do their homework either! <G>

We get this very 'assignment' every three months. If the OP does not want to
learn, (s)he should just 'google' for the code.
#include <iostream>
file://#include <conio>
#include <string>

class Student{ private:
std::string name;
int ob_marks;
int otal_marks;
public:
file://Student();
Student(std::string n = "",int om = 0);
void cal_percentage();
void display();
};
/*Student::Student()
{
int len=sizeof(name);
name=new char[len+1];
ob_marks=0;
}*/
Student::Student(std::string n, int om){
file://int len=sizeof(n);
file://name=new char[len+1];
file://strcpy(name,n);
name = n;
ob_marks = om;
}
void Student::cal_percentage(){
otal_marks=(ob_marks*100)/otal_marks;
}
void Student::display(){
std::cout<<name<<"\t"<<ob_marks<<"\t"<<otal_marks<<std::endl;
}
int main(){
Student s1("mohammad",67);
s1.cal_percentage();
s1.display();
return 0;
}

Now only one problem remains - you never set otal_marks to a meaningful
value.

Anyone want to critique my advice, I am also a beginner.

Here's one, use constructor 'init lists':

Student::Student( std::string n, int om) : name( n ), ob_marks( om ){}
 
Z

Zeppe

Pramod said:
a program which consists of a class named Student, the class should
consists of three data members Name, Ob_marks, Total_marks and two
member functions Cal_percentage() which calculate the percentage of
the student by the formula (Ob_marks * 100 ) / Total_marks and
Display() which show all information of the student. The class should
also contain the default constructor which initializes all the data
member of the class.
In main program define an object of the Student class and call the
member functions with this object.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.

only copy and paste this programmm and run

WTF... do NOT solve homework. Don't do this. Don't. It's evil. Evil, you
know? Refrain to do so. I know, it's difficult sometimes to STFU when
you feel like you know something. But It's better to learn, sooner or later.

Regards,

Zeppe
 
J

James Kanze

I presume that Pramod was being sarcastic here, and threw in a
number of errors intentionally, since we aren't here to do
people's homework for them.
I didn't have conio on my system (should I?)

No. It's not standard, and I don't know of any system which has
it. (Windows usually still has a <conio.h>, which is, however,
mainly there for historical reasons---it was an MS-DOS standard
header.)
use a string here
IMHO you should probably should make this lower case for ease
of typing, i.e. ob_marks

There are two widespread conventions: separating names using an
_, and mixed case, e.g.: ob_marks, or obMarks. However, the
homework requirements said that the name must be Ob_marks.
(It's rather unusual to find a convention in which a variable
name starts with an upper case, too.)
there are 2 constructors defined here - that maybe legal, I leave that
to others more knowledgeable than me, but it would not compile on my
system. Also you should define the default values here - see below.

There is only one constructor declared here. The first is not a
constructor (case is significant in C++), but a function
declaration, and it is missing the return type.

The actual constructor is, shall we say, "interesting". To call
it, you need a variable of type char---an array will not work,
it must be a single variable.
missing semi-colon

Typo here - C++ is case sensitive. You are trying to use student (lower
case 's') when you defined Student (upper case 'S'). Anyways, this whole
block should be done away with
not needed, use sizeof(name)

Not at all the same thing! name is an uninitialized pointer.
sizeof(name) gives defined behavior, returning the size of a
pointer on his system (usually 4 or 8, today). Calling strlen
with an uninitialized pointer causes undefined behavior. A lot
more fun to throw at a learner who is too lazy to even try to do
his own homework.
name=new char[len+1];
ob_marks=0;
doesn't match the Ob_marks defined earlier (case sensitive)
Student::Student(string n, int om)

It's generally more common practice to pass strings by
reference, i.e.:

Student::Student( string const& n, int om )

See above for my comment on the constructor's signature.
{
int len=strlen(n);
name=new char[len+1];
strcpy(name,n);
Ob_marks=om;
}
just about this whole block should be re-worked

Actually, it's about the only thing that's correct in the entire
program. Or would be correct, if the signature were the
expected:

Student::Student( char const* n, int om )

Of course, in well written code, the members would be
initialized in an initializer list, and not in the body of the
constructor.
case sensitive

total_marks? you mean otal_marks? (as defined above), otherwise
total_marks was never defined.

That's part of the fun of it, no?
no semi-colon goes here - remove it

have to use std:: for cout and endl since you didn't declare your
namespace earlier
again, case sensitive
Now here is a working version (at least on my system):

I think the point was not to provide a working version.
 
J

James Kanze

Since
Premed screwed up, you also screw up, so your program can not work.

The only way he screwed up was not making his irony apparent
enough. (Although I wonder. I found using char& to pass a
string a dead give away.)

Many years ago, when the usual language in CS 101 was C, there
was a tradition among the regulars in comp.lang.c to see who
could come up with the most "creative" responses. The best
could come up with fully working programs which gave the right
results, but which were either obviously wrong (because the
poster found a clever way of making to errors cancel each
other), or worthy of IOCCC, or in some otherway amusing. I
found it a rather pleasant way of handling people asking for
others to do their homework.
 
J

James Kanze

Did you read this FAQ?

I think maybe he did:).

[...]
If you are going to do peoples homework for them, at least
give them something that will compile (or close to it)!

The goal of homework is for the person to learn something.
Trying to get what Pramod posted to compile and run will
probably teach the original poster a lot more than if he'd
posted a correct solution:).
 
J

James Kanze

WTF... do NOT solve homework.

Look at the code a little bit. Then reconsider this answer.
Don't do this. Don't. It's evil. Evil, you
know? Refrain to do so. I know, it's difficult sometimes to STFU when
you feel like you know something. But It's better to learn, sooner or later.

If the original poster gets the posted code to compile and run,
he'll have learned a lot. Both about C++ and about how
newgroups work:).
 
Z

Zeppe

James said:
Look at the code a little bit. Then reconsider this answer.

Oh, sorry! Really, at a glance it seemed something like a solution.
Sorry for having been rude, dudes! ;P

Regards,

Zeppe
 
O

osmium

:

Since
Premed screwed up, you also screw up, so your program can not work.

The only way he screwed up was not making his irony apparent
enough. (Although I wonder. I found using char& to pass a
string a dead give away.)

Many years ago, when the usual language in CS 101 was C, there
was a tradition among the regulars in comp.lang.c to see who
could come up with the most "creative" responses. The best
could come up with fully working programs which gave the right
results, but which were either obviously wrong (because the
poster found a clever way of making to errors cancel each
other), or worthy of IOCCC, or in some otherway amusing. I
found it a rather pleasant way of handling people asking for
others to do their homework.

Yes. Pramod has also cleverly covered his tracks by using variants on the
spelling Pramod and posting beginner type questions to throw naive types
such as me off the trail. Such as asking, in effect, "how do you swap two
values without using a temporary variable?" Or perhaps that was posted by a
different Pramod, perhaps a younger brother? They use the same NNTP posting
host.
 
B

BobR

James Kanze wrote in message...
On Jun 12, 8:27 pm, "BobR" wrote:
[...]
The goal of homework is for the person to learn something.
Trying to get what Pramod posted to compile and run will
probably teach the original poster a lot more than if he'd
posted a correct solution:).

In other words, I've been had ...AGAIN!! Dang, I hate when that happens!!
<G>

I think it would have been better to post something like I've seen Mr.
osmium (I think) post:
// -----
class Student{ /* minimal declarations */ };

int main(){
Student TheStudent(/*parms*/);
// use the TheStudent here
return 0;
}
// -----

Then ask the student to post his/her problem code.

But, different teachers have different methods (and can all be correct). I
had a teacher in 'continuation school' teach me in 20 minutes what I failed
to learn in 3 years of high school. He simply explained it differently, and
suddenly I got the picture. (Great teacher!)
 
D

Devon Null

Zeppe said:
Oh, sorry! Really, at a glance it seemed something like a solution.
Sorry for having been rude, dudes! ;P

So I'm lost, did I screw up bad? As I read the FAQ, the only time I was
absolutely forbidden to answer was when there was no code posted. As
there was code posted (albeit mangled code, but an attempt) I was then
allowed to comment on it. I know I probably screwed a lot things up,
thus the disclaimer at the bottom about being a beginner also, but I did
it to the best of my understanding. There I go thinking again. I didn't
mean to rankle anybody's feathers. I apologize if I did and I'll leave
the question answering to others of higher caliber from now on.

DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 
Z

Zeppe

Devon said:
So I'm lost, did I screw up bad? As I read the FAQ, the only time I was
absolutely forbidden to answer was when there was no code posted. As
there was code posted (albeit mangled code, but an attempt) I was then
allowed to comment on it. I know I probably screwed a lot things up,
thus the disclaimer at the bottom about being a beginner also, but I did
it to the best of my understanding. There I go thinking again. I didn't
mean to rankle anybody's feathers. I apologize if I did and I'll leave
the question answering to others of higher caliber from now on.

DN

The problem is that as you can notice the OP didn't post any code. I
actually suspect that he's not even able to write down an "hello world"
application. Then Pramod wrote down a (intentionally) buggy program to
let the OP think about the problem, but I didn't realised and I
complained. You actually corrected the bugs in that code. The rule about
the homework is not a rigid "if there is code you can correct, if there
is no code you can't write anything". You should put a bit of
common-sense, which means: somebody assigned a homework to the OP in
order to let him learn something. If you want to help, you have to teach
something to that guy. The group is about sharing knowledge, not code.
So, if you can understand that the OP is not looking for knowledge, but
just for somebody that does his homework for him, you'd better not
provide him with the information he asks, that wouldn't help anybody. On
the opposite, it's disruptive for the OP.

When who posts is looking for information and is interested in
understanding something, you are more than welcome to help if you want
to! :)


Regards,

Zeppe
 
O

osmium

Devon Null said:
So I'm lost, did I screw up bad? As I read the FAQ, the only time I was
absolutely forbidden to answer was when there was no code posted. As
there was code posted (albeit mangled code, but an attempt) I was then
allowed to comment on it. I know I probably screwed a lot things up,
thus the disclaimer at the bottom about being a beginner also, but I did
it to the best of my understanding. There I go thinking again. I didn't
mean to rankle anybody's feathers. I apologize if I did and I'll leave
the question answering to others of higher caliber from now on.

To me, it was a minor screw up. I think the mistake you made was not going
back to find the original post, the one where the subject is not prefixed
with Re: on my newsreader; I think you got started in the middle of a
thread without knowing it. In case my sarcasm was too dense, or whatever,
the post you answered was a *serious* attempt to answer, and most certainly
not a "witty programmer" response, as theorized here. He (Pramod) shouldn't
have done that.
 
D

Devon Null

Zeppe said:
The problem is that as you can notice the OP didn't post any code. I
actually suspect that he's not even able to write down an "hello world"
application. Then Pramod wrote down a (intentionally) buggy program to
let the OP think about the problem, but I didn't realised and I
complained. You actually corrected the bugs in that code. The rule about
the homework is not a rigid "if there is code you can correct, if there
is no code you can't write anything". You should put a bit of
common-sense, which means: somebody assigned a homework to the OP in
order to let him learn something. If you want to help, you have to teach
something to that guy. The group is about sharing knowledge, not code.
So, if you can understand that the OP is not looking for knowledge, but
just for somebody that does his homework for him, you'd better not
provide him with the information he asks, that wouldn't help anybody. On
the opposite, it's disruptive for the OP.

When who posts is looking for information and is interested in
understanding something, you are more than welcome to help if you want
to! :)


Regards,

Zeppe


My bad - I thought the one who posted the code WAS the OP, i.e. I told
him/her the go look at the FAQ and he/she came back and posted code.
Again, my mistake. As for the hard and fast rule thing, I was simply
referring to what the FAQ says, though I do understand that there is
some human flexibility in that. As the above showed though, I have a
long way to go in fully comprehending it all. IMHO until you understand
the rules enough to know how and when to bend them, it is best to stick
by them.


DN

P.S. there is a reason I came up with my sig and used it ;)
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 
D

Devon Null

osmium said:
To me, it was a minor screw up. I think the mistake you made was not going
back to find the original post, the one where the subject is not prefixed
with Re: on my newsreader; I think you got started in the middle of a
thread without knowing it. In case my sarcasm was too dense, or whatever,
the post you answered was a *serious* attempt to answer, and most certainly
not a "witty programmer" response, as theorized here. He (Pramod) shouldn't
have done that.

Check the response to Zeppe. But I do feel better now. I have been
reading this news group a lot lately and hold a pretty high opinion of
some of you guys (and gals?) so I was kind of concerned about having
made a major screw-up.

DN
--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top