this program needs to input information of two people and out it

A little assistance, this need to input names and addresses of two people then output.
When i input address like "P. O Box 2548" it terminates and shouldn't do that.

The function is perfect the only problem is the out put.

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

void displayData(string name, char addr1[30], string addr2, string postalCode);

int main()
{
string name, addr2, postalCode;
char addr1 [30];
int i = 2;

for (i = 0 ; i < 2 ; i++)
{
cout <<"Enter your name: ";
cin >> name;
cout <<"Enter your Address 1: ";
cin >> addr1;
cout <<"Enter your Address 2: ";
cin >> addr2;
cout <<"Enter Postal Code: ";
cin >> postalCode;
cout <<endl;
}

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

return 0;
}

void displayData(string name, char addr1[30], string addr2, string postalCode)
{
cout<<name<<endl;
cout<<addr1<<endl;
cout<<addr2<<endl;
cout<<postalCode<<endl;

}
Last edited on
void displayData(string name, char addr1[30], string addr2, string postalCode);

why use 3 strings and 1 char[30]? why not use 4 strings?
when i used 4 strings after entering the address like 'P. O Box 0000" the program suddenly terminated so i thought the problem was "P.O Box 0000" string because it contained spaces.

But after changing that the problem stayed the same.
Try getline(cin, addr1) instead of cin >> addr1.
try:

getline( cin, addr1, '\n' );

i don't know for sure, but i think cin>> addr1 reads a single word instead of a line. so the rest of the words is placed in the other strings.
Last edited on
tried it no luck..
What about using good old getch() to fill buffer until enter. Worked for me many times.
Following code with take whatever until ENTER. You can string copy the buffer to where you want it.

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
#include <stdio.h>
#include <conio.h>

#define CR 13
void ReadString(void);
char szBuffer[100];

void ReadString(void)
{

char cCh;
int  iPtr;

    iPtr = 0;
    while ((cCh = getch()) != CR)
    {
        putch(cCh);
        szBuffer[iPtr++] = cCh;
    }

   // End string with NULL
    szBuffer[iPtr] = 0;
}

int main()

{
    cprintf("Type address: ");
    ReadString();
    cprintf("\r\nAddress = %s\r\n", szBuffer);

   // Wait for keypress
    getch();
}
i tested this, and it works:

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
#include <iostream>
#include <string>
using namespace std;

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

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

	for (i = 0 ; i < 2 ; i++)
	{
		cout <<"Enter your name: ";
		getline( cin, name );
		cout <<"Enter your Address 1: ";
		getline( cin, addr1 );
		cout <<"Enter your Address 2: ";
		getline( cin, addr2 );
		cout <<"Enter Postal Code: ";
		getline( cin, postalCode );

		cout <<endl;

		displayData (name, addr1, addr2, postalCode);

		
	}

	getchar();
	return 0;
}

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

}
thanks guys..
Topic archived. No new replies allowed.