-
Project
Hello, the function of this program is to read in a student id number and convert it into a format like ddd-dd-dddd. Where d represents any digit from 0 to 9. The program should be able to deal with id input as many times as the user wants. Using 0 ID to terminate the program. When the user enters a wrong ID, he/she should be allowed to re-enter an id.
How can i incorporate these things in the following program. What loops should I use, if any.
Thanxs
Here is my program:
#include<iostream.h>
#include<string.h>
#include<ctype.h>
// 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);
void main()
{ char ss_number_input[10] = "000000000",
number_1to3[4] = "000",
number_4to5[3] = "00",
number_6to9[5] = "0000",
ss_number_output[12] = "";
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;
}
else
// a ss number should not have non-digit characters
cout << "\n Invaild ss number, Please try again..";
}
void copystring(char t[], const char s[], int p1, int p2)
{ int idx;
// p1: starting position
// p2: ending position
for (idx = 0; idx <= p2 - p1; idx++)
t[idx] = s[p1 + idx - 1];
t[idx] = '\0';
}
int all_digits(char s[], int size)
{ int idx;
char ch;
idx = 0;
ch = s[idx];
while (idx < size && ch != '\0' && isdigit(ch)) {
idx++;
ch = s[idx];
}
if (idx == 9)
return 1;
else
return 0;
}
-
this sounds like a good project for overloading, can you do that? if you use code tags i'll take a harder look at your code. :)
-
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'.
:)
-
Thank you for your sugguestions. They are helping a lot. If there are anymore please feel free to post them. I need all the help I can get. Thanks again.