More Cstrings....

im not sure what im doing wrong, i thought everything was right until i compiled it. i guess the way im retrieving the user input is incorrect.. please let me know what corrections i should make and if there are alternatives to making this code simple and clean. thank you in advance

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  
#include <cstdlib>
#include <iostream>

void getInfo(char &street,char &city, char &state, char &zip);
void displayInfo( char address);
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    char street[30];
    char city [20];
    char state[3];
    char zip[6];
    char address[60];
    
    
    getInfo(street,city,state);
    
    strcpy(address,street);
    strcat(address, " ", city);
    strcat(address, " ", state);
    strcat(address, " ", zip);
    
    
    return 0;
}
void getInfo(char &street,char &city, char &state, char &zip)
{
    cout << "enter your street address: "<< endl;
    cin.getline(street,30);
    cout << "enter your city"<< endl;
    cin.getline(city,20);
    cout << "enter your state"<< endl;
    cin.getline(state,3);
    cout << "enter you zip code"<< endl;
    cin.getline(zip, 6);
}
void displayInfo( char address[])
{
    cout << " your full address is: "<<
         << address << endl;
}
It makes the life of the helper a lot easier if you describe what you or your code is trying to do.
Last edited on
void getInfo(char &street,char &city, char &state, char &zip)

You probably meant to have char* for all of these. Not char&

char& gives you a reference to a single char.
char* gives you a pointer to (in this case) an array of chars.
so when using an array i used * ? im a bit confused, under what circumstances to i used & or * ?
Yes, arrays are a bit confusing.

Long story short: when you have an array, you want to pass by pointer (*).


Long story long:

There are generally 3 ways to pass something to a function:

1
2
3
void func(int v);  // by value
void func(int& v); // by reference
void func(int* v); // by pointer 


"By value" creates a copy of whatever you pass. The variable inside your function will be a completely separate variable from the one in main.

"By reference" creates a reference, which is kind of like another name for the same var. When you pass by reference, there is no copy. Instead, the var in the function is the exact same variable as the one you have in main... you're just accessing it with a different name.

"By pointer" creates a pointer which points to the data you want. Similar to passing by reference in that you're not actually copying the data.

What makes "by pointer" useful is that... since it's a pointer and not the actual variable... it can point to any number of consecutive elements. For example... if you have a pointer which has the address 0x12345678... and you know that the data there is several bytes... you know the address of the next byte will be +1 from the address of the first byte.

This makes it practical to pass arrays by pointer. Rather than copying the array entirely, you just give the function a pointer to the first element in the array. Then, all further elements in the array can be found and accessed as well.
Disch answered while I was fixing the code. I got bored and did C Strings and std::string. Disadvantage of being busy while coding, you use whatever excuse you can to keep coding (hence the two different code sections).
C Strings (your code):
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdlib>
#include <cstring>
#include <iostream>

void getInfo(char *street,char *city, char *state, char *zip);
void displayInfo(char *address);
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    char street[30];
    char city [20];
    char state[3];
    char zip[6];
    char address[60];
    
    
    getInfo(street, city, state, zip);
    
    strcpy(address,street);
    strcat(address, " ");
    strcat(address, city);
    strcat(address, " ");
    strcat(address, state);
    strcat(address, " ");
    strcat(address, zip);
    
    displayInfo(address);
    
    return 0;
}
void getInfo(char *street,char *city, char *state, char *zip)
{
    cout << "enter your street address: "<< endl;
    cin.getline(street,30);
    cout << "enter your city"<< endl;
    cin.getline(city,20);
    cout << "enter your state"<< endl;
    cin.getline(state,3);
    cout << "enter you zip code"<< endl;
    cin.getline(zip, 6);
}
void displayInfo(char *address)
{
    cout << " your full address is: "<< address << endl;
}

Strings
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
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

void getInfo(std::string &street, std::string &city, std::string &state, std::string &zip);
void displayInfo(std::string address);

int main(int argc, char **argv)
{
	std::string street, city, state, zip;
	std::string address;
	
	getInfo(street, city, state, zip);
	
	address = street + " " + city + " " + state + " " + zip;
	
	displayInfo(address);
	
	return 0;
}

void getInfo(std::string &street, std::string &city, std::string &state, std::string &zip)
{
	std::cout << "Enter your street address: " << std::endl;
	std::getline(std::cin, street);
	std::cout << "Enter your city: " << std::endl;
	std::getline(std::cin, city);
	std::cout << "Enter your state: " << std::endl;
	std::getline(std::cin, state);
	std::cout << "Enter your zip code: " << std::endl;
	std::getline(std::cin, zip);	
}

void displayInfo(std::string address)
{
	std::cout << "Your full address is: " << address << std::endl;
}
Last edited on
very useful and informative! thank you Disch and BHX specter!
Topic archived. No new replies allowed.