Suraj Jadhav
Suraj Jadhav

Reputation: 39

String is not passing through constructor argument

#include<iostream>
using namespace std;
#include<conio.h>

class student{
    int roll_no;
    char name[15];
    float per;
public:
    student(int a, char b[15], float c){
        roll_no = a;
        name[15] = b[15];
        per = c;
    }
    ~student(void){
        cout << "Student Details : \n\n"
             << "Roll No : " << roll_no << "\n"
             << "Name : " << name[15] << "\n"
             << "Percentage : " << per << endl;
    }
};

int main(){
    student s(60,"Suraj Jadhav",25.25);
    getch();
    return 0;
}

Output is : Student Details:

Roll No : 60
Name : 
Percentage : 25.25

Name is not displaying string.. Not sure what is the problem but want to solve.. Please help..

Upvotes: 0

Views: 218

Answers (4)

Slava
Slava

Reputation: 44238

You should not use raw pointers if you do not understand them, use std::string instead. Anyway your constructor can be fixed like this:

student(int a, const char *b, float c){
    roll_no = a;
    strncpy( name, b, sizeof( name ) );
    per = c;
}

There is issue with strncpy() when string length (what strlen() returns and excluding \0 terminator) where b points to is equal or longer than size of name - it will not put \0 terminator into target string. So code could be safer this way:

student(int a, const char *b, float c){
    roll_no = a;
    name[ sizeof( name ) - 1 ] = 0;
    strncpy( name, b, sizeof( name ) - 1 );
    per = c;
}

Again working with raw pointers is pretty complex and you need to deeply understand what is going on there to write safe code. Using std::string in C++ will make your life much simpler.

Upvotes: 2

Joky
Joky

Reputation: 1628

when you declare

char name[15];

name is an array of 15 char. The parameter b is a pointer ("expected" to point to an array of 15 chars). The statement

name[15] = b[15];

copy only the 16th element of the array pointed by "b" to the 16th element in the array "name" (count starts at zero), since there is 15 elements in the array there is no defined behavior here (the same way when you print name[15]).

In C you have to copy each character one after the other. Functions like strcpy takes care of that for you, it might be unsafe if destination is not large enough to accommodate source. In C++ you should try to avoid using array of char and use std::string instead take will take of copy safely. You should also use initializer list (the syntax to initialize member in the constructor). For instance:

#include<iostream>
using namespace std;
#include<conio.h>

class student{
    int roll_no;
    string name;
    float per;
public:
    student(int a, const string &b, float c) 
         : roll_no(a), name(b), per(c)
    {
    }
    ~student(){
        cout << "Student Details : \n\n"
             << "Roll No : " << roll_no << "\n"
             << "Name : " << name << "\n"
             << "Percentage : " << per << endl;
    }
};

int main(){
    student s(60,"Suraj Jadhav",25.25);
    getch();
    return 0;
}

Note: #include <conio.h> is not standard C/C++, it is a specific MS-DOS header. Try to avoid if possible :)

Upvotes: 4

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

Instead of invalid statement

name[15] = b[15];

you shall use C standard function strcpythat is declared in header

std::strcpy( name, b );

Also the correct declaration of the constructor will look the following way

student(int a, const char b[15], float c);

or

student(int a, const char b[], float c);

or

student(int a, const char *b, float c);

These three declarations declare the same function.

To make the constructor safe I would define it as

student(int a, const char b[], float c){
        roll_no = a;
        std::strncpy( name, b, 15 );
        name[14] = '\0';
        per = c;
}

Also it is a good idea to assign a name to the magic number 15 either using an enumerator or static constant.

Upvotes: 1

Brian Bi
Brian Bi

Reputation: 119089

name[15] = b[15];

won't copy the string. It'll just copy one character from b to name, specifically the one at index 15. (In fact, this is actually undefined behaviour, since each array only has indices 0..14.) Try this:

strcpy(name, b);

Upvotes: 3

Related Questions