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
#include<iostream>
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<<hello.getname()<<endl;
cout<<hello.getpubl()<<endl;
cout<<hello.getauth()<<endl;
return 0;
}
when i try to compile it it gives me loads of errors. can some one help me out.
i am using g++
errors:
copychar.cpp:49:13: error: prototype for ‘const char* base1::getname() const’ does not match any in class ‘base1’
copychar.cpp:10:6: error: candidate is: char base1::getname()
copychar.cpp:54:13: error: prototype for ‘const char* base1::getpubl() const’ does not match any in class ‘base1’
copychar.cpp:11:6: error: candidate is: char base1::getpubl()
copychar.cpp:58:13: error: prototype for ‘const char* base1::getauth() const’ does not match any in class ‘base1’
copychar.cpp:12:6: error: candidate is: char base1::getauth()
when i am using the following code, only the first character of each string is getting displayed
#include<iostream>
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<<hello.getname()<<endl;
cout<<hello.getpubl()<<endl;
cout<<hello.getauth()<<endl;
return 0;
}
Your loops are wrong, not
but
Your loop should check for the end of the
publisherstring that you pass into the function, not for the end of theinit_publstring. That doesn’t make much sense since you haven’t given that string a value yet.Same for the other loops.
Also look at this
and this
See the difference? First you say getname is
char, then you say it’sconst char*andconst. You have to be consistent. The second one is correct, replacewith
Same with getauth and getpubl.