Do-While Loop HELP!!!!

Hello! There is a lot of code here for a relatively simple question. My Do-While loop at the bottom of main() is supposed to restart the program. (I know there isnt an option to exit() for a 'N' or 'n'. I will get to that later) But when the program runs again after pressing 'Y' or 'y' the output looks something like..

"Enter your street address: Enter your city: "

where it wont allow me to assign a value to Street address but it will for city.

After all the info has been typed in for this loop and it compiles, it outputs all the information from the first time running the program as well as the 2nd time running.

How can i make it so my program will be able to restart flawlessly?

Thank you :)

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

void getInfo(char street[], char city[], char state[], char zip[]);
void displayAddress(char address[], int numItems);

int main()
{
	char street[30] = {'\0'};
	char city[20] = {'\0'};
	char state[3] = {'\0'};
	char zip[6] = {'\0'};
	char address[60] = {'\0'};
	char runAgain;

	do{
	getInfo(street, city, state, zip);

	strcat(address, street);
	strcat(address, ", ");
	strcat(address, city);
	strcat(address, ", ");
	strcat(address, state);
	strcat(address, " ");
	strcat(address, zip);
	cout << "\n\n";

	displayAddress(address, 60);

	cout << "Would you like to run the program again (Y/N)? ";
	cin >> runAgain;
	cout << '\n';

	} while ((runAgain == 'Y') || (runAgain == 'y')); 

	return 0;
}
//----------------------------------------------------------------------
void getInfo(char street[], char city[], char state[], char zip[])
{
	cout << "Enter your street address: ";
	cin.getline(street, 30);
	cout << "Enter your city: ";
	cin.getline(city, 20);
	cout << "Enter your state (2 digits): ";
	cin.getline(state, 3);
	cout << "Enter your 5-digit zip code: ";
	cin.getline(zip, 6);
}
//----------------------------------------------------------------------
void displayAddress(char address[], int numItems)
{
	cout << "Your full address is:\n\n";
	for (int i = 0; i < strlen( address ); i++)
	{
		cout << address[i];
	}
	cout << "\n\n";
}
//---------------------------------------------------------------------- 
Add cin.sync(); on line 35. This will make sure anything remaining in the input stream is ignored.
Topic archived. No new replies allowed.