if else statement trouble

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?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` `````` // Adam // Magic Squares // Sources: // This will determine whether the user input is a magic square. #include 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
Your else is missing a } other than that it works for me.
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...
what is the problem at the moment?
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.
Give me a minute to copy it and run to see the problem myself.
I don't think you need the last two `getchar;`
The programme seems to work for me. What example are you using where it doesn't work?
The only thing i can think of is, have you recompiled it?
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.
Topic archived. No new replies allowed.