954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Equation (sum) doesnt add up properly.

hello everyone...

i wrote a basic program for my C++ class that includes SUMs and while loops.
its fairly self explanatory, but i am not getting the correct SUMs for my outputs.
apparently, i am missing something extremely important somewhere here.
can someone take a look and give me a few hints as to why my outputs are coming out the way they are...

thank you.

note: why i have so much variables here is because my teacher wants it that way. i would use an array to make the program look neater, but i was instructed to keep it as simple and as basic as possible. =)

#include <iostream>
using namespace std;

int main()
{
    int numX;
    int numY;
    int numZ;
    
    int sum1 = numX - numZ;
    int sum2 = numX - numY + numZ;
    int sum3 = numX + numY;
    
    int sum4 = numX + numY + numZ;
    int sum5 = numX;
    int sum6 = numX - numY - numZ;
    
    int sum7 = numX - numY;
    int sum8 = numX + numY - numZ;
    int sum9 = numX + numZ;
    
    int totalA = sum1 + sum2 + sum3;
    int totalB = sum4 + sum5 + sum6;
    int totalC = sum7 + sum8 + sum9;
    int totalD = sum1 + sum4 + sum7;
    int totalE = sum2 + sum5 + sum8;
    int totalF = sum3 + sum6 + sum9;
    
    while (cin)
    {
          
          cout << "Please enter 3 integers separated by spaces: (enter non-digit to quit)" << endl;
          cin >> numX >> numY >> numZ;
          cin.ignore(1000, 10);
          
          cout << "The magic square is:" << endl;
          cout << sum1 << sum2 << sum3 << " = " << totalA << endl;
          cout << sum4 << sum5 << sum6 << " = " << totalB << endl;
          cout << sum7 << sum8 << sum9 << " = " << totalC << endl;
          cout << "---------------" << endl;
          cout << totalD << totalE << totalF << endl;          
          cout << " " << endl;
          cout << " " << endl; 
   
    }
    
cin.get();    
return 0;
}
xiikryssiix
Light Poster
25 posts since Jul 2009
Reputation Points: 46
Solved Threads: 0
 

Hi... it seems that it is precedence problem of operations....you are not using parenthesis when you are performing two operations at the same time subtraction and addition... do it this way int sum2 = (numX - numY) + numZ; hope it is going to solve your problem.

Agent-of-Chaos
Light Poster
30 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

Move this

int sum1 = numX - numZ;
    int sum2 = numX - numY + numZ;
    int sum3 = numX + numY;
    
    int sum4 = numX + numY + numZ;
    int sum5 = numX;
    int sum6 = numX - numY - numZ;
    
    int sum7 = numX - numY;
    int sum8 = numX + numY - numZ;
    int sum9 = numX + numZ;
    
    int totalA = sum1 + sum2 + sum3;
    int totalB = sum4 + sum5 + sum6;
    int totalC = sum7 + sum8 + sum9;
    int totalD = sum1 + sum4 + sum7;
    int totalE = sum2 + sum5 + sum8;
    int totalF = sum3 + sum6 + sum9;


behind the

cin.ignore(1000, 10);
jencas
Posting Whiz
366 posts since Dec 2007
Reputation Points: 395
Solved Threads: 71
 

If you didn't realize this code :

int numX;
    int numY;
    int numZ;
    
    int sum1 = numX - numZ;
    int sum2 = numX - numY + numZ;
    int sum3 = numX + numY;
    
    int sum4 = numX + numY + numZ;
    int sum5 = numX;
    int sum6 = numX - numY - numZ;
    
    int sum7 = numX - numY;
    int sum8 = numX + numY - numZ;
    int sum9 = numX + numZ;
    
    int totalA = sum1 + sum2 + sum3;
    int totalB = sum4 + sum5 + sum6;
    int totalC = sum7 + sum8 + sum9;
    int totalD = sum1 + sum4 + sum7;
    int totalE = sum2 + sum5 + sum8;
    int totalF = sum3 + sum6 + sum9;


Is useless because numX,numY,numZ is not initialized. You need
to first get num* then calculate it. What you are doing is
calculating junk, then getting num* then showing the junk you
calculated.

Again get user input first into numX,numY,numZ, then calculated
whatever you need to calculate.

-------
num* = numX, numY, numZ

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

hey thanks for all the help. fixed it and all works well now. =) thank yous!!

xiikryssiix
Light Poster
25 posts since Jul 2009
Reputation Points: 46
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You