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;
}

Recommended Answers

All 4 Replies

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.

Member Avatar for jencas

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);

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.