string functions not working correctly

Apr 10, 2016 at 4:26pm
Hi all, I'm doing a question for my assignment on functions. The question wants me to create two functions to input and display data entered by the user but i'm new to strings and i cannot seem to get my program to work correctly, i am also not sure how i would use the information gained by the inputData function that im using to display the information later. Any help would be great.

Heres my code:

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

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

string inputData(string name,string addr1,string addr2,string postalCode)
{
cout << "Please enter your name:" << endl;
cin >>name;
cout << "Please enter your postal address1:" << endl;
cin >>addr1;
cout << "Please enter your postal address2:" << endl;
cin >>addr2;
cout << "Please enter your postalCode:" << endl;
cin >>postalCode;

}

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

cout << name << endl;
cout << addr1 << endl;
cout << addr2 << endl;
cout << postalCode << endl;

}

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

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

return 0;

}
Last edited on Apr 10, 2016 at 4:26pm
Apr 10, 2016 at 5:22pm
You should use the getline function to get a string of input instead of cin, that way if some puts in their full name you get the whole thing. Cin only grabs input until it finds a space, then discards the space from the input stream. Getline grabs input until it finds a newline character, then discards the newline character form the stream.
You can use getline like this:
1
2
getline(cin, name); //gets line from cin input stream, puts into name string
//getline also works for other types of input streams. 


If your running this on console you can include <cstdlib.h> and call the system function to pause the console, assuming your on windows:
1
2
3
4
5
#include <cstdlib.h>

system("pause");
//put this after you get your data, so that you can read it without
//console closing. 


display data function needs to take the strings as inputs instead of declaring them inside it, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void displayData(string name, string addr1, string addr2, string postalCode)
{
cout << name << endl;
cout << addr1 << endl;
cout << addr2 << endl;
cout << postalCode << endl;
}


//then in your main function you call it by passing the arguments to it:
int main()
{
string name,addr1,addr2,postalCode;

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

return 0;

}
Last edited on Apr 10, 2016 at 5:23pm
Apr 10, 2016 at 8:37pm
Thanks for the reply, ive changed my code around a bit after what you said i should do, the program runs until i enter anything for the postalCode variable and then the program stops working and windows shuts it down. Do you have any idea why this would be happening?
Apr 10, 2016 at 8:55pm
You should post your new code, I can't help you without seeing it.
Apr 10, 2016 at 10:12pm
#include <iostream>
#include <string>
using namespace std;

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

string inputData(string name,string addr1,string addr2,string postalCode)
{
cout << "Please enter your name:" << endl;
getline(cin,name);
cout << "Please enter your postal address1:" << endl;
getline(cin,addr1);
cout << "Please enter your postal address2:" << endl;
getline(cin,addr2);
cout << "Please enter your postalCode:" << endl;
getline(cin,postalCode);
}

void displayData(string name,string addr1,string addr2,string postalCode)
{
cout << name << endl;
cout << addr1 << endl;
cout << addr2 << endl;
cout << postalCode << endl;
}

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

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

return 0;

}
Apr 11, 2016 at 12:50am
Ah I see whats wrong, you need to take the string in by reference so the functions can modify them directly. Whats happening is the inputData function is copying them into its scope, instead of modifying the external strings.
If you change your functions to these it will work:
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
//added ampersand (&) to signify they are references
string inputData(string & name,string & addr1,string & addr2,string & postalCode);
void displayData(string & name,string & addr1,string & addr2,string & postalCode);

//added ampersand (&) to signify they are references
string inputData(string & name,string & addr1,string & addr2,string & postalCode)
{
cout << "Please enter your name:" << endl;
getline(cin,name);
cout << "Please enter your postal address1:" << endl;
getline(cin,addr1);
cout << "Please enter your postal address2:" << endl;
getline(cin,addr2);
cout << "Please enter your postalCode:" << endl;
getline(cin,postalCode);
}

//added ampersand (&) to signify they are references
void displayData(string & name,string & addr1,string & addr2,string & postalCode)
{
cout << name << endl;
cout << addr1 << endl;
cout << addr2 << endl;
cout << postalCode << endl;
}


If you don't know what references are you should read this page:
http://www.cplusplus.com/doc/tutorial/functions/
Apr 11, 2016 at 11:05am
Thanks it worked.
Topic archived. No new replies allowed.