tags:

views:

317

answers:

3
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

+2  A: 

It looks like the prompt is inside the while loop but outside the outermost for loop. Hence the prompt will appear only after all iterations are done. Move the prompt to inside the outermost for loop.

Conal
+1 for answering the actual question
csj
+2  A: 

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:

char sent;
: :
for (n1=2;n1<=9;n1++) {
    for (n2=2;n2<=9;n2++) {

into:

char sent[100];
: :
for (n1=2;n1<=9;n1++) {
    if (n1 > 2) {
        printf("press any key to continue or 'n' to stop writing the file\n");
        scanf("%s",sent);
        if (*sent == 'n') break;
    }
    for (n2=2;n2<=9;n2++) {

The change of sent from 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 and scanf() is dangerous however and shouldn't be used in production code. I include it here only yo make your life a little easier.

paxdiablo
thanks!!=) this really help me a lot..
keitamike
@keitamike: Then you should accept this answer.
sbi
+2  A: 

If i correctly understand your code you are computing all possible permutations of 8 different numbers. Try using much faster algorhitm:

http://en.wikipedia.org/wiki/Permutation#Algorithms%5Fto%5Fgenerate%5Fpermutations there are two pseudo code examples. (one for unordered and one for lexicographical orderd permutations)

ralu