# problem with my magic square assignment

Page 1 of 1

## 3 Replies - 837 Views - Last Post: 29 June 2012 - 07:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284335&amp;s=2cf6ad2ecc797da51ef757edbb282c2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 robertc

Reputation: 3
• Posts: 20
• Joined: 29-June 12

# problem with my magic square assignment

Posted 29 June 2012 - 06:02 PM

a magic square is a square of numbers where the sum of every row and every column are equal to one another.

My assignment is to write a program that checks if a given 4x4 square of numbers is "magic"

I got the program to tell me if it IS a magic square.
If the square i enter is NOT a magic square, the program should tell me so. I used an else statement to do this but for some reason it's not working. When i enter a square that is not "magic" the program just ends without telling me so. any help would be awesome!

here is my code....
```#include <iostream>
#include <string>
using namespace std;

int main() {
//Declare variables
int a, b, c, d;
int e, f, g, h;
int i, j, k, l;
int m, n, o, p;

//Tell user to enter magic square
cout << "Welcome to Robert's Magic Square Checker! Please enter your magic square:" << "\n";

//Lets user input the magic square
cin >> a; cin >> b; cin >> c; cin >> d;
cin >> e; cin >> f; cin >> g; cin >> h;
cin >> i; cin >> j; cin >> k; cin >> l;
cin >> m; cin >> n; cin >> o; cin >> p;

//if statements used to check if every line and column are equal
if (a + b + c + d == e + f + g + h)
if (e + f + g + h == i + j + k + l)
if (i + j + k + l == m + n + o + p)
if (m + n + o + p == a + f + k + p)
if (a + f + k + p == d + g + j + m)
{
//If every line and column is equal, the program will output this...
cout << "This was a magic square! Thank you!" << "\n";
}
else
{
//Otherwise, the program will output this...
//This is where i am having trouble!!!
cout << "This was not a magic square! Thank You!" << "\n";
}

}

```

Is This A Good Question/Topic? 0

## Replies To: problem with my magic square assignment

### #2 jimblumberg

Reputation: 3060
• Posts: 9,309
• Joined: 25-December 09

## Re: problem with my magic square assignment

Posted 29 June 2012 - 06:20 PM

Is your else statement in the correct position, you may want to add the braces {} for all your if statements to insure your else is tied correctly. If any of your first three if statements evaluate to false then the else will never execute.

Jim

### #3 Kilorn

• XNArchitect

Reputation: 1325
• Posts: 3,504
• Joined: 03-May 10

## Re: problem with my magic square assignment

Posted 29 June 2012 - 07:04 PM

Or you could use some &&'s and combine all of the ifs into a single if, then the else would fire if at any time the values were evaluated to false.
```    if ((a + b + c + d == e + f + g + h) && (e + f + g + h == i + j + k + l) && (i + j + k + l == m + n + o + p) && (m + n + o + p == a + f + k + p) && (a + f + k + p == d + g + j + m))
{
//If every line and column is equal, the program will output this...
cout << "This was a magic square! Thank you!" << "\n";
}
else
{
//Otherwise, the program will output this...
//This is where i am having trouble!!!
cout << "This was not a magic square! Thank You!" << "\n";
}

```

### #4 robertc

Reputation: 3
• Posts: 20
• Joined: 29-June 12

## Re: problem with my magic square assignment

Posted 29 June 2012 - 07:48 PM

Thanks! it works fine now