A DO...WHILE loop in main() would handle your problem. This would require the "functionalization" of the code you currently have in main(), though.
(First, change void main() to int main() and terminate with 'return 0;' )
[code]
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include <conio.h> // add for getch();
// copy a part of string to another string
void copystring(char t[], const char s[], int p1, int p2);
// test if an input consists all digits
int all_digits(char s[], int size);
char get_ss(); // declare a new function to handle input
int main()
{
char yn;
do
{
yn = get_ss();
}while (yn == 'Y');
return 0;
}
Now, your new function might look like this:
Code:
char get_ss()
{
char ss_number_input[10] = "000000000",
number_1to3[4] = "000",
number_4to5[3] = "00",
number_6to9[5] = "0000",
ss_number_output[12] = "";
char yn;
cout << "\nEnter your ss number in all digits: ";
cin >> ss_number_input;
if (all_digits(ss_number_input, 10))
{
// separate into 3 parts
copystring(number_1to3, ss_number_input, 1, 3);
copystring(number_4to5, ss_number_input, 4, 5);
copystring(number_6to9, ss_number_input, 6, 9);
// reorganize ss number with insertation of '-'
strcat(ss_number_output, number_1to3);
strcat(ss_number_output, "-");
strcat(ss_number_output, number_4to5);
strcat(ss_number_output, "-");
strcat(ss_number_output, number_6to9);
cout << "\nYour ss number is ";
cout << ss_number_output<<endl;
cout << "\nTry again? (Y/n) -> ";
yn = getch();
yn = toupper(yn);
cin.ignore(2, '\0');
}
else
{
// a ss number should not have non-digit characters
cout << "\n Invaild ss number, Please try again...\n";
ss_number_input[10] = get_ss();
}
return yn;
}
The remainder of your code is good as is, though my example for the "new" function could (should) be refined even further.
The primary thing is to contain control in main() through the DO...WHILE loop until the user opts to bail out of the program by entering anything but a 'y'.