3 Replies - 837 Views - Last Post: 29 June 2012 - 07:48 PM Rate Topic: -----

#1 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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  Icon User is online

  • member icon

Reputation: 3060
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon




Reputation: 1325
  • View blog
  • 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";
    }


Was This Post Helpful? 0
  • +
  • -

#4 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1