void distinct (void) {
char sent;
int n1, n2, n3, n4, n5, n6, n7;
FILE *fp2 = fopen ("distinct.txt", "w");
while (sent != 'n') {
for (n1=2;n1<=9;n1++) {
for (n2=2;n2<=9;n2++) {
if (n2 != n1) {
for (n3=2;n3<=9;n3++) {
if (n3 != n2 && n3 != n1) {
for (n4=2;n4<=9;n4++) {
if (n4 != n3 && n4 != n2 && n4 != n1) {
for (n5=2;n5<=9;n5++) {
if (n5 != n4 && n5 != n3 && n5 != n2 && n5 != n1) {
for (n6=2;n6<=9;n6++) {
if (n6 != n5 && n6 != n4 && n6 != n3 && n6 != n2 && n6 != n1) {
for (n7=2;n7<=9;n7++) {
if (n7 != n6 && n7 != n5 && /* you get the idea */ && n7 != n1) {
fprintf (fp2, "%d", n1);
fprintf (fp2, "%d", n2);
fprintf (fp2, "%d", n3);
fprintf (fp2, "%d", n4);
fprintf (fp2, "%d", n5);
fprintf (fp2, "%d", n6);
fprintf (fp2, "%d\n", n7);
}
}
}
}
}
}
}
}
}
}
}
}
}
printf ("Press any key to continue or 'n' to stop writing the file\n");
scanf ("%c", &sent);
}
fclose (fp2);
}
I want the loop to pause whenever the first digit changes to ask whether to continue or stop.
2xxxxxx
2xxxxxx
2xxxxxx
Press any key to continue or 'n' to stop writing the file
// continues if 'y' is entered //
3xxxxxx
3xxxxxx
3xxxxxx
Press any key to continue or 'n' to stop writing the file
// stops when 'n' is entered //
The program I wrote doesn’t work as I wish…T___T
I think in terms of readability, you’re better putting it at the top of the relevant loop. Get rid of the outer while altogether and change:
into:
The change of
sentfrom a character to a string is so you don’t need to worry about getting linefeeds as characters (if you type yENTER, you’ll get the next two sections since y is one character and ENTER is another – even worse if you enter yesENTER). The use of a fixed size string andscanf()is dangerous however and shouldn’t be used in production code. I include it here only yo make your life a little easier.