Note that there are some explanatory texts on larger screens.

plurals
  1. POcopying an array of characters in c++ without using string functions
    text
    copied!<p>can anyone help me out with this code.. All i want to do is copy display the character arrays initialized in the main function am pretty new to cpp and i can't figure a way out</p> <pre><code>#include&lt;iostream&gt; using namespace std; class base1 { public: void setname(char *name); void setpubl(char *publisher); void setauth(char *author); char getname(); char getpubl(); char getauth(); private: char init_name[50], init_publ[50], init_auth[50]; }; void base1::setname(char *name) { int i=0; while(init_name[i] != '\0') { init_name[i]=name[i]; i++; } } void base1::setpubl(char *publisher) { int j=0; while(init_publ[j] != '\0') { init_publ[j]=publisher[j]; j++; } } void base1::setauth(char *author) { int k=0; while(init_auth[k] != '\0') { init_auth[k]=author[k]; k++; } } const char* base1::getname() const { return init_name; } const char* base1::getpubl() const { return init_publ; } const char* base1::getauth() const { return init_auth; } /* base1::base1() { init_name[0]=0; init_publ[0]=0; init_auth[0]=0; }*/ int main() { base1 hello; char name[]="cpp"; char publisher[]="dreamworks"; char author[]="random"; hello.setname(name); hello.setpubl(publisher); hello.setauth(author); cout&lt;&lt;hello.getname()&lt;&lt;endl; cout&lt;&lt;hello.getpubl()&lt;&lt;endl; cout&lt;&lt;hello.getauth()&lt;&lt;endl; return 0; } </code></pre> <p>when i try to compile it it gives me loads of errors. can some one help me out. i am using g++</p> <p>errors:</p> <blockquote> <p>copychar.cpp:49:13: error: prototype for ‘const char* base1::getname() const’ does not match any in class ‘base1’</p> <p>copychar.cpp:10:6: error: candidate is: char base1::getname()</p> <p>copychar.cpp:54:13: error: prototype for ‘const char* base1::getpubl() const’ does not match any in class ‘base1’</p> <p>copychar.cpp:11:6: error: candidate is: char base1::getpubl()</p> <p>copychar.cpp:58:13: error: prototype for ‘const char* base1::getauth() const’ does not match any in class ‘base1’</p> <p>copychar.cpp:12:6: error: candidate is: char base1::getauth()</p> </blockquote> <p>when i am using the following code, only the first character of each string is getting displayed</p> <pre><code>#include&lt;iostream&gt; using namespace std; class base1 { public: void setname(char *name); void setpubl(char *publisher); void setauth(char *author); char getname(); char getpubl(); char getauth(); private: char init_name[50], init_publ[50], init_auth[50]; }; void base1::setname(char *name) { int i=0; while(name[i] != '\0') { init_name[i]=name[i]; i++; } } void base1::setpubl(char *publisher) { int j=0; while(publisher[j] != '\0') { init_publ[j]=publisher[j]; j++; } } void base1::setauth(char *author) { int k=0; while(author[k] != '\0') { init_auth[k]=author[k]; k++; } } char base1::getname() { return *init_name; } char base1::getpubl() { return *init_publ; } char base1::getauth() { return *init_auth; } /* base1::base1() { init_name[0]=0; init_publ[0]=0; init_auth[0]=0; }*/ int main() { base1 hello; char name[]="cpp"; char publisher[]="dreamworks"; char author[]="random"; hello.setname(name); hello.setpubl(publisher); hello.setauth(author); cout&lt;&lt;hello.getname()&lt;&lt;endl; cout&lt;&lt;hello.getpubl()&lt;&lt;endl; cout&lt;&lt;hello.getauth()&lt;&lt;endl; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload