using void function to display string

what seems to be the problem everything seems al-right with me

#include <iostream>
#include <string>
using namespace std;

//Insert function displayData here
//using std::string;
void displayData(string);

int main()
{
string name, addr1, addr2, postalCode;

name = "Mr R.S.Bopape";
addr1 = "P.O. Box 50741";
addr2 = "Sandton";
postalCode = "2146";

displayData(name, addr1, addr2, postalCode);
return 0;
}
Last edited on
The displayData function has two problems:

1) The prototype indicates that it takes a single string, but you're trying to feed it four strings.
2) It doesn't exist. You have to write it.
Not quite sure of what is said on point (2).

Not quite sure of what is said on point (2).


Well, you've created a prototype of a function. Basically, that's something that tells the program that this function exists and it's definition should be later in the code. However, you don't define it at all. So your program is expecting that function to exist but it doesn't.

You need something after the main function:

1
2
3
4
void displayData (string)
{
   // Code to print string that's been passed in
}
still have a problem with this program..assistance please
We're not psychic. What exactly do you want the function displayData to do?

Do you know how to write a function?
Last edited on
I can take a guess at what you want here, but I'm reluctant to write the code because I don't think you'll learn from it properly.

If you still have a problem, that indicates you've tried something to get it working. Post your current progress and I'll be happy to take a look.
this is how far i have tried.

#include <iostream>
#include <string>
using namespace std;

void displayData(string name, addr1, addr2, postalCode)

int main()
{
string name, addr1, addr2, postalCode;

name = "Mr R.S.Bopape";
addr1 = "P.O. Box 50741";
addr2 = "Sandton";
postalCode = "2146";

displayData(name, addr1, addr2, postalCode);

return 0;
}

void displayData(string name, addr1, addr2, postalCode)
{
cout<<name, addr1,addr2,postalCode;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>
#include <string>
using namespace std;

void displayData(string name, string addr1, string addr2, string postalCode);


int main()
{
string name, addr1, addr2, postalCode;

name = "Mr R.S.Bopape";
addr1 = "P.O. Box 50741";
addr2 = "Sandton";
postalCode = "2146";

displayData(name, addr1, addr2, postalCode);

return 0;
}

void displayData(string name, string addr1,  string addr2, string postalCode)
{
  cout<<name << addr1 << addr2 <<postalCode;
} 
Last edited on
i was so close but hey thanks
Topic archived. No new replies allowed.