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

Feb 16, 2012 at 1:22pm
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 Feb 16, 2012 at 1:28pm
Feb 16, 2012 at 1:40pm
void displayData(string name, char addr1[30], string addr2, string postalCode);

why use 3 strings and 1 char[30]? why not use 4 strings?
Feb 16, 2012 at 1:47pm
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.
Feb 16, 2012 at 2:00pm
Try getline(cin, addr1) instead of cin >> addr1.
Feb 16, 2012 at 2:05pm
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 Feb 16, 2012 at 2:10pm
Feb 16, 2012 at 2:26pm
tried it no luck..
Feb 16, 2012 at 2:43pm
What about using good old getch() to fill buffer until enter. Worked for me many times.
Feb 16, 2012 at 3:03pm
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();
}
Feb 16, 2012 at 3:15pm
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; 

}
Feb 16, 2012 at 6:08pm
thanks guys..
Topic archived. No new replies allowed.