What i'm trying to do is use a loop that prompts the user to re-enter a new string of numbers if what he previously entered is not within the specified range of 1-16. What I have is not working as expected. What am I doing wrong?

/*
Program assignment name: Input-Output

Author:Christopher D*****
*/

#include <stdio.h>

int main (void)
{
   int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;
   int n=0,o=0,p=0;
   
   do
   {
      printf("Enter the numbers from 1 to 16 in any order, separated by spaces: \n\n");
      scanf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", &a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l,&m,&n,&o,&p);
   }
   while((a<1 || a>16) && (b<1 || b>16) && (c<1 || c>16) && (d<1 || d>16) && (e<1 || e>16) && (f<1 || f>16) && (g<1 || g>16) && (h<1 || h>16) && (i<1 || i>16) && (j<1 || j>16) && (k<1 || k>16) && (l<1 || l>16) && (m<1 || m>16) && (n<1 || n>16) && (o<1 || o>16) &&(p<1 || p>16));
   {
      printf("You have entered a number outside of the specified range of 1 to 16. Please try again.\n");
   }

   printf("\n");
   printf("%d %d %d %d\n", a,b,c,d);
   printf("%d %d %d %d\n", e,f,g,h);
   printf("%d %d %d %d\n", i,j,k,l);
   printf("%d %d %d %d\n\n", m,n,o,p);
   printf("Row sums: %d %d %d %d\n", a+b+c+d, e+f+g+h, i+j+k+l, m+n+o+p); 
   printf("Column sums: %d %d %d %d\n",a+e+i+m, b+f+j+n, c+g+k+o, d+h+l+p); 
   printf("Diagonal sums: %d %d\n", a+f+k+p, m+n+o+p);
   return 0;  
}

I've modified it to the following but while it's an improvement, it's still not working correctly.

#include <stdio.h>

int main (void)
{
   int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;
   int n=0,o=0,p=0;
   
   do
   {
      printf("Enter the numbers from 1 to 16 in any order, separated by spaces: \n\n");
      scanf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", &a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l,&m,&n,&o,&p);
   }
   while(a<1 || a>16 || b<1 || b>16 || c<1 || c>16 || d<1 || d>16 || e<1 || e>16 || f<1 || f>16 ||g<1 || g>16 || h<1 || h>16 || i<1 || i>16 || j<1 || j>16 || k<1 || k>16 || l<1 || l>16 || m<1 || m>16 || n<1 || n>16 || o<1 || o>16 || p<1 || p>16);
   {
      printf("You have entered a number outside of the specifed range of 1 to 16. Please try again.\n");
      while (getchar() != '\n'); /*Clear input buffer*/
   }

   printf("\n");
   printf("%d %d %d %d\n", a,b,c,d);
   printf("%d %d %d %d\n", e,f,g,h);
   printf("%d %d %d %d\n", i,j,k,l);
   printf("%d %d %d %d\n\n", m,n,o,p);
   printf("Row sums: %d %d %d %d\n", a+b+c+d, e+f+g+h, i+j+k+l, m+n+o+p); 
   printf("Column sums: %d %d %d %d\n",a+e+i+m, b+f+j+n, c+g+k+o, d+h+l+p); 
   printf("Diagonal sums: %d %d\n", a+f+k+p, m+n+o+p);
   return 0;  
}

Edited 4 Years Ago by cdea06: n/a

Ever hear of arrays?

How do you expect to debug a statement like

while(a<1 || a>16 || b<1 || b>16 || c<1 || c>16 || d<1 || d>16 || e<1 || e>16 || f<1 || f>16 ||g<1 || g>16 || h<1 || h>16 || i<1 || i>16 || j<1 || j>16 || k<1 || k>16 || l<1 || l>16 || m<1 || m>16 || n<1 || n>16 || o<1 || o>16 || p<1 || p>16);

and at the same time notice that one error is the ; at the end?

You need to use the KISS principle, and a separate variable for each value is not it.

Thanks but not really helpful WaltP. I just learned arrays but have no idea how one would apply to this. I rewrote the program and it works so nvm.

This question has already been answered. Start a new discussion instead.