PDA

View Full Version : String Function in C++.



Wizzy
03-27-2003, 12:56 AM
Would someone mind giving me an example of one please? (First semester programmer)

Jason
03-27-2003, 01:09 AM
example of what? you want a function of a plain string call? be more specific


Jason

Wizzy
03-27-2003, 01:15 AM
Well my homework is to keep everything out of the int main() as much as possible. So I tried writing a string that asks you're name, but I am not sure where I am going wrong.


This is just the beginning of it. :-\




#include <iostream.h>
#include <string.h>

using namespace std;

int main()
{
Get_Name(string); // Get's your name.
system("Pause");
return 0;
}

String Get_Name(string name); // String to get user's name.
{
string name;
cout << "What is your name?" << endl;
cin >> name;
return name;
}

Jason
03-27-2003, 01:21 AM
ok...in the int main() you call the method Get_Name(string)

What you are doing when make this call is you are passing it a string the string used in the Get_Name call so you would want something more like

#include <iostream.h>
#include <string.h>

using namespace std;

int main()
{
Get_Name( ); // Get's your name.
system("Pause");
return 0;
}

String Get_Name( ); // String to get user's name.
{
string name;
cout << "What is your name? " << endl;
cin >> name;
return name;
}


does that make sense? Im not sure about the syntax being complete...but it should be alright.


Jason

Wizzy
03-27-2003, 01:25 AM
I think I got what you said. Sorta. I'll try this out. Thanks much. You're great. :-D

Wizzy
03-27-2003, 01:32 AM
You wouldn't happen to know why I get syntax errors on my cin and cout statements, would you?

sytax errors before `>' and `<'

Jason
03-27-2003, 01:46 AM
some compilers don't have the ".h" so try #include <iostream>

otherwise nope...



Jason

Jason
03-27-2003, 01:48 AM
ok, figured it out...simple stupid coding error....after the function Get_Name() there is a';' there....get rid of it.


Jason

Wizzy
03-27-2003, 01:54 AM
I give up.
*grumbles* Now I get an implict deleration of the Get_Name function, and that system pause function.

Thanks for your help Jason. Much appriciated.