What is wrong with my code

I am working to create a contact card type-thing where the user enters their information and at the end they will see a contact card. The first name, last name and address part are working fine but after that the next three items (city, state, zip) are showing up all at once without the user entering the items in and I can't figure out why.

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
//ContactCard.cpp

#include <iostream>
using namespace std;

int main ()
{
	//declare variables
	char firstName[20] = "";
	char lastName[20]	= "";
	char streetAddress[45] = "";
	char city[45] = "";
	char state[2] = "";
	char zip[5] = "";
	char phone[10] = "";

	cout <<"Enter the customer's first name: ";
	cin>> firstName;
	cout <<"Enter the customer's last name: ";
	cin>> lastName;
	cout <<"Enter the customer's street address: ";
	cin>> streetAddress;
	cout <<"Enter the customer's city: ";
	cin>> city;
	cout <<"Enter the customer's state as a two-letter abbreviation: ";
	cin>> state;
	cout <<"Enter the customer's zip code: ";
	cin>> zip;
	cout <<"Enter the customer's phone number: ";
	cin>> phone;
	cout << firstName << lastName << "lives at" << streetAddress << city << ", " << state << zip << ". Call them at " << phone << ".";


return 0;
}
Try:

1
2
3
string szInput;
getline (cin, szInput, '\n');
// get text (from where? the console input, store it where? szInput,  keep getting text until a newline, \n) 


Instead of cin, problem solved.

There's a few solutions for this, but I think this is one of the better ones.
Last edited on
closed account (zb0S216C)
Works fine only if you avoid spaces. For input to accept spaces, use std::istream::getline()[1].
Here's an example:

1
2
3
4
5
6
7
8
9
int main()
{
    unsigned int const ARRAY_SIZE(56);
    unsigned char input_[ARRAY_SIZE] = {0};

    // Get input:
    std::cin.getline(input_, (ARRAY_SIZE - 1u), '\n');
    std::cout << input_;
}

The code is simple enough: std::istream::getline() simply extracts the specified amount of characters (ARRAY_SIZE - 1 in this case) from the input stream. The extracted data is placed within input_. If the delimiting character (the 3rd parameter) is found, the delimiting character is removed from the input stream.

References:
[1] http://www.cplusplus.com/reference/iostream/istream/getline/


Wazzak
Oh ok, thanks!

Also, if I wanted to take the first letter of the person's first and last name to form their initials.. what kind of procedure would that be
Okay I tried to do it as a substring and this is now what I have. When I compile it, it says no error. When I run the program, it lets me input all the information then crashes..

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//ContactCard.cpp

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

//function prototypes
string firstName = "";
string lastName	= "";
string firstInitial = "";
string lastInitial = "";
string streetAddress = "";
string city = "";
string state = "";
string zip = "";
string phone = "";
void displayLine();
void displayInitial();
void displayName();
void displayStreet();
void displayCityStateZip();
void displayPhone();


int main ()
{
	//declare variables



	cout <<"Enter the customer's first name: ";
	getline (cin, firstName, '\n');
	cout <<"Enter the customer's last name: ";
	getline (cin, lastName, '\n');
	cout <<"Enter the customer's street address: ";
	getline (cin, streetAddress, '\n');
	cout <<"Enter the customer's city: ";
	getline (cin, city, '\n');
	cout <<"Enter the customer's state: ";
	getline (cin, state, '\n');
	cout <<"Enter the customer's zip code: ";
	getline (cin, zip, '\n');
	cout <<"Enter the customer's phone number: ";
	getline (cin, phone, '\n');

	firstInitial = firstName.substr (1,1);
	lastInitial = lastName.substr (1,1);


	//display output items
	cout << "This is what " << firstName << " " << lastName << "'s contact card will appear.";
	cout <<"\n";
	displayLine();
	displayInitial();
	displayLine();
	displayName();
	displayStreet();
	displayCityStateZip();
	displayPhone();
	displayLine();

return 0;
} //end of main function

//**************function definitions***************
void displayLine()
{
	cout <<"****************************************"<< endl;
}	//end of displayLine function

void displayInitial()
{
	cout <<"**********         " << firstInitial << lastInitial << "         **********"<<endl;
} //end of displayInitial function
void displayName ()
{
	cout <<"**********         " << firstName << lastName <<endl;
}//end of displayName function
void displayStreet ()
{
	cout <<"**********         " << streetAddress << endl;
}//end of displayStreet function
void displayCityStateZip ()
{
	cout <<"**********         " << city <<", "<< state << " " << zip << endl;
}//end of displayCityStateZip function
void displayPhone ()
{
	cout <<"**********         " << phone << endl;
}//end of displayPhone function
Last edited on
Wow, do you "really" need all of those functions? One of the reasons to use a function is to save space in your int main() function, and in your program, it actually ended up taking more space than a program similar to yours that doesn't use functions like those.

A few things I would suggest:

1. Don't declare external variables - so put them into int main()
2. IF you're going to keep using these functions, then learn to pass arguments to them.
1
2
3
4
5
6
string szInput = "Display this!"; // somewhere in int main(), must come before the function call
display_string (szInput); // somewhere in int main()
void display_string (string szInput) // comes after int main() with the rest of your other functions
{
cout << szInput << endl;
}


3. If you want to print initials, then use this:
1
2
string szInput "ABCDE";
cout << szInput[0] << endl;


This will print the first character in the string szInput, which is A.

If your program currently allows you to input all of the information, then crashes, then look at the code that follows all the console inputs. You should take a look at this:

1
2
	firstInitial = firstName.substr (1,1);
	lastInitial = lastName.substr (1,1);
Last edited on
Topic archived. No new replies allowed.