Oct 5, 2013 at 11:55pm UTC
I've gone over my code for hours now, I asked some people in class and they couldn't help me. I see no reason why it won't run as it should.
Program: This program is supposed to check if a sequence of 16 numbers makes a "Magic square" (all rows and columns, and both diagonals add up to the same number.)
Problem: The program tells me when I have a magic square, but when the 16 numbers don't make a magic square, I get no response. Can anyone tell me what I am doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
// Adam
// Magic Squares
// Sources:
// This will determine whether the user input is a magic square.
#include<iostream>
using namespace std;
int main() {
//initialize variables
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int b1 = 0;
int b2 = 0;
int b3 = 0;
int b4 = 0;
int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;
int d1 = 0;
int d2 = 0;
int d3 = 0;
int d4 = 0;
int row1;
int row2;
int row3;
int row4;
int col1;
int col2;
int col3;
int col4;
int dia1;
int dia2;
//prompt the user
cout << "Welcome to Adam's Magic Square Checker! Please enter your magic square:" ;
//get input
cin >> a1 >> a2 >> a3 >> a4 >> b1 >> b2 >> b3 >> b4 >> c1 >> c2 >> c3 >> c4 >> d1 >> d2 >> d3 >> d4;
//calculate and output results
//add variables to create rows, columns, and diagonals
row1 = a1 + a2 + a3 + a4;
row2 = b1 + b2 + b3 + b4;
row3 = c1 + c2 + c3 + c4;
row4 = d1 + d2 + d3 + d4;
col1 = a1 + b1 + c1 + d1;
col2 = a2 + b2 + c2 + d2;
col3 = a3 + b3 + c3 + d3;
col4 = a4 + b4 + c4 + d4;
dia1 = a1 + b2 + c3 + d4;
dia2 = a4 + b3 + c2 + d1;
//check if all rows, columns, and diagonals are equivelent and output results
if (row1 == row2
&& row2 == row3
&& row3 == row4
&& row4 == col1
&& col1 == col2
&& col2 == col3
&& col3 == col4
&& col4 == dia1
&& dia1 == dia2
&& dia2 == row1)
{
cout << "This was a magic square! Thank you!" ;
}
else
{
cout << "this was not a magic square" ;
}
//pause and exit
getchar();
getchar();
return 0;
}
Last edited on Oct 6, 2013 at 12:22am UTC
Oct 6, 2013 at 12:16am UTC
Your else is missing a } other than that it works for me.
Oct 6, 2013 at 12:22am UTC
Oh god, I missed that on my last time messing around with it. I added it, and it still doesn't tell me when I'm wrong...
Oct 6, 2013 at 1:41am UTC
what is the problem at the moment?
Oct 6, 2013 at 1:42am UTC
Any submissions that are magic numbers turn out as expected. when a non-magic number is the input, no reply is given upon return, when "this was not a magic square" is expected.
Oct 6, 2013 at 7:54pm UTC
Give me a minute to copy it and run to see the problem myself.
Oct 6, 2013 at 8:05pm UTC
I don't think you need the last two getchar;
Oct 6, 2013 at 8:05pm UTC
The programme seems to work for me. What example are you using where it doesn't work?
Oct 6, 2013 at 8:15pm UTC
The only thing i can think of is, have you recompiled it?
Oct 6, 2013 at 9:29pm UTC
Are you serious?!?!?!?!?!?! I just copy and pasted what I had here into visual basic and now it decides to work. Thank you guys for your help.