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
Ok this one is a doosey! I don't know if anyone will understand but I will try my best to explain what I want done.
Here is an example class:
Could I make it where if gasInTank<=0 then they could not PutInGear? Not sure if that sounds clear to anyone. Basically I want the function StartUp to determine, based on user input, if there is enough gass in the car to be able to use the PutInGear Function. I think I saw this in another thread, will I have to use bool type arguments? Such as:Code:class Car
{
public:
int StartUp(int gasInTank);
void PutInGear();
void Accelerate();
void SlowDown();
void FloorIT();
char GetMake(char* carMake);
char GetModel(char* carModel);
int GetYearMade();
private:
int YearMade;
char Model;
char Make;
};
In other words: If StartUp fails then the user will not be able to PutInGear.Code:
Car::StartUp(int gasInTank)
{
if (gasInTank<=0)
{
StartUp = fail
cout <<"You need more gas to start your car" <<endl;
}
else
cout <<"Your car starts-up smoothly" <<endl;
}
If this makes anysense at all please give me INFORMATION (i.e. threads, links, google search keywords, to things that I will need to know in order to pull this off. (if its possible)
1)First of all, the most intuitive way to declare an array function parameter is like this:
void my_func(char my_array[]);
It is more intuitive because you're declaring the array like this:
char my_array[30];
so the syntax is similar, and you don't have to confuse yourself with pointers.
2)You're the programmer aren't you? You're the one that's going to be calling the functions for your class, right? So, don't call the PutInGear() function if the conditions aren't met. After all the user doesn't enter input like:
my_car.PutInGear()
do they? It's not like the user gets to decide what's going to happen.
3) You don't have any constructors for your class nor any SetMember() functions, so that might be a problem seeing how your data members are private.Code:#include<iostream>
using namespace std;
int main()
{
switch (menu_choice)
{
case 1:
...
case 2:
...
case 3:
if(gas<=0)
cout<<"Sorry, you can't put "
<<"the car in gear: no gas"<<endl;
else
my_Car.PutInGear();
break;
default:
}
return 0;
}
I like the STL, boost, etc. Their containers and algorithms, besides being incredible time savers, make it vastly easier to write memory-leak free code, make it easier to declare and use dynamically sized multidimentional arrays, make it easier to write exception-safe code, etc. They make it safe to return a pointer to a newly allocated resource without the possibility of a leak, and in general, they're not only safer, but more elegant and readable as well.Quote:
Originally posted by FillYourBrain
I get the idea you like Stl, Cat. :)
You know all your member functions can access the private variables...Code:class Car
{
public:
int StartUp(int gas);
void PutInGear();
void Accelerate();
void SlowDown();
void FloorIT();
char GetMake(char* carMake);
char GetModel(char* carModel);
int GetYearMade();
private:
int YearMade;
char Model;
char Make;
int gasInTank;
};