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?
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?
Knowledge is power and I want it all
-0RealityFusion0-
Code:void Func(char *arr) { //Do something with the str } int main(void) { char str[256]; Func(str); return 0; }
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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]; }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
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"); }
Last edited by RealityFusion; 08-15-2003 at 01:15 PM.
Knowledge is power and I want it all
-0RealityFusion0-
Read carefullyCode:#inlcude <iostream>
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
hahaha!
Knowledge is power and I want it all
-0RealityFusion0-
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.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
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"); }
Knowledge is power and I want it all
-0RealityFusion0-
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; }
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
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*)
Knowledge is power and I want it all
-0RealityFusion0-
You're missing a semi-colon after the function declaration of GetName() and after the structure definition.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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; }
Last edited by funkydude9; 08-15-2003 at 05:18 PM.
Well, there are a few things wrong with your code:
1) It does not work.
2) It does not work.
3) It does not work.
Hope this helps.
Thank you all that helped me!
Knowledge is power and I want it all
-0RealityFusion0-