COS1511 Assign 2 Q4

My program cant display but accepts data. HELP

#include <iostream>
#include <string>

using namespace std;

string inputData (string name, string addr1, string addr2, string postalcode)
{
cout<<"Enter your name ";
getline(cin, name);
cout<<"Enter address line 1 ";
getline (cin, addr1);
cout<<"Enter Address line 2 ";
getline(cin, addr2);
cout<<"Enter your postal code ";
getline(cin, postalcode);
return (name, addr1, addr2, postalcode);
}

void displayData (string name, string addr1, string addr2, string postalcode)
{
cout<<name; '/n';
cout<<addr1; '/n';
cout<<addr2; '/n';
cout<<postalcode; '/n';

}

int main()
{
string name;
string addr1;
string addr2;
string postalcode;

inputData(name, addr1, addr2, postalcode);
displayData(name, addr1, addr2, postalcode);

}
What do you mean by "cant display"? What does happen after last input?


Please add code tags to your post. They make reading and commenting easier.

Why does inputData() return a value? You don't use the returned value in main().

Your functions take arguments by value. Do you know about by reference function arguments?


A function can return only one object. Your return (a, b); returns only one of the two. Which? Read the description of the comma-operator. (There is a reason why it is not used often.)

Writing cout<<name; '/n'; is same as writing
1
2
3
cout<<name;

'/n';

What does line 3 do? What does your compiler say about it? If nothing, crank up the verbosity of the compiler.
I want the inputData fxn to take up 4 values and pass them to displayData fxn
To pass values like you try you have to change 2 things


To pass values by reference you need pointers in the function head.
string inputData (string* name, string* addr1, string* addr2, string* postalcode)

Now you have to call the function with pointers or adresses (&varName)
inputData(&name, &addr1, &addr2, &postalcode);

This way when you make a change to an variable in inputData() the change is done directly to for example string name of your main function.

Anyways what you did was pass values of your variables to inputData() change their values and return. That way the data was input but you can't use them because they only exist in inputData().

Also for your next post please use code tags.
Last edited on
To pass values by reference you need pointers
No!

To pass objects by reference one should use references.
See http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.