Ok I checked google and got no results and I even searched the board. I found the answer on the board but I don't understand it.
So how do you pass a function a characater array, how do you declare it and how do I get it to work?
Printable View
Ok I checked google and got no results and I even searched the board. I found the answer on the board but I don't understand it.
So how do you pass a function a characater array, how do you declare it and how do I get it to work?
Code:void Func(char *arr)
{
//Do something with the str
}
int main(void)
{
char str[256];
Func(str);
return 0;
}
A character array meaning a string?
Code:void func(char* c);
int main(void)
{
func("string");
}
void func(char* c)
{
for (int i=0;c[i]!='\0';i++)
cout<<c[i];
}
I made some changes:
I changed what is in red and now I get 11 errors?Code:#inlcude <iostream>
using namespace std;
class Cat
{
public:
GetName(char* name)
}
void Cat::GetName(char* name)
{
cout <<"Enter a name." <<endl;
cin.getline(name,20);
cout <<"The name you have given is: " <<endl;
cout <<name;
}
int main()
{
char name[20];
Cat::GetName(name);
system("pause");
}
Read carefully :DCode:#inlcude <iostream>
hahaha!
a good way to approach your errors is to handle the first error that shows up first. :) I'm sure that little typo was the first.
BTW, prefer std::string to character arrays (and std::vector to arrays of other types) especially when passing to a function. Unlike java, an array is passed only as the pointer to the first array element -- there is no length information passed. Buffer overruns, etc. are tricky things. Further, a multidimentional array is MUCH better passed as a std::vector of std::vectors (for the 2D case); it's easier to declare, won't leak memory, and is just as easy to use.
I get the idea you like Stl, Cat. :)
I made some changes:
I changed what is in red and now I get 11 errors?Code:#inlcude <iostream>
using namespace std;
class Cat
{
public:
GetName(char* name)
}
void Cat::GetName(char* name)
{
cout <<"Enter a name." <<endl;
cin.getline(name,20);
cout <<"The name you have given is: " <<endl;
cout <<name;
}
int main()
{
char name[20];
Cat::GetName(name);
system("pause");
}
You need to call the function GetName on an instance of a Cat. Cat is an abstract idea, and clearly you cannot get the name of Cat, but you can get the name of a Cat. The syntax you are using only works for static funtions (which don't require an associated instance of the class).
Code:int main()
{
char name[20];
Cat felix;
felix.GetName(name);
return 0;
}
Changed it and it is still giving me the same errors.
11 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
semicolon
11 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
ISO
11 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
syntax
7 C:\Documents and Settings\Owner\My Documents\FucntionsCallingStrings.cpp
candidate
is C:\Documents and Settings\Owner\My Documents\ is
void Cat::GetName(char*)
You're missing a semi-colon after the function declaration of GetName() and after the structure definition.
So what you're really trying to say is....
Code:#include <iostream>
using namespace std;
class Cat
{
public:
void GetName(char* name);
};
void Cat::GetName(char* name)
{
cout <<"Enter a name." <<endl;
cin.getline(name,20);
cout <<"The name you have given is: " <<endl;
cout <<name;
}
int main()
{
char name[20];
Cat felix;
felix.GetName(name);
return 0;
}
Thank you all that helped me! :D