-1
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

6
  • 6
    I think my head just exploded. Too much code that looks too much the same. Reduce it.
    – user166390
    Oct 23, 2009 at 6:20
  • Try reading it without indention...
    – carl
    Oct 23, 2009 at 6:22
  • 4
    If I ever write a text editor, I will be sure to make it crash if it detects that many nested levels.
    – jrockway
    Oct 23, 2009 at 6:39
  • 1
    @jrockway, you may have trouble making any money out of a text editor that crashes with perfectly legal (though not necessarily perfectly readable) C code :-) You may just as well have it crash because the variable names are a bit suspect.
    – paxdiablo
    Oct 23, 2009 at 6:46
  • 3
    @jrockway: Can we add paxdiablo's suggestion ("You may just as well have it crash because the variable names are a bit suspect.") to your editor's feature list, please?
    – sbi
    Oct 23, 2009 at 11:22

3 Answers 3

2

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.

0
2

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.

0
2

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_to_generate_permutations there are two pseudo code examples. (one for unordered and one for lexicographical orderd permutations)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.