943,723 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 440
  • C++ RSS
Aug 25th, 2009
0

Equation (sum) doesnt add up properly.

Expand Post »
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. =)


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int numX;
  7. int numY;
  8. int numZ;
  9.  
  10. int sum1 = numX - numZ;
  11. int sum2 = numX - numY + numZ;
  12. int sum3 = numX + numY;
  13.  
  14. int sum4 = numX + numY + numZ;
  15. int sum5 = numX;
  16. int sum6 = numX - numY - numZ;
  17.  
  18. int sum7 = numX - numY;
  19. int sum8 = numX + numY - numZ;
  20. int sum9 = numX + numZ;
  21.  
  22. int totalA = sum1 + sum2 + sum3;
  23. int totalB = sum4 + sum5 + sum6;
  24. int totalC = sum7 + sum8 + sum9;
  25. int totalD = sum1 + sum4 + sum7;
  26. int totalE = sum2 + sum5 + sum8;
  27. int totalF = sum3 + sum6 + sum9;
  28.  
  29. while (cin)
  30. {
  31.  
  32. cout << "Please enter 3 integers separated by spaces: (enter non-digit to quit)" << endl;
  33. cin >> numX >> numY >> numZ;
  34. cin.ignore(1000, 10);
  35.  
  36. cout << "The magic square is:" << endl;
  37. cout << sum1 << sum2 << sum3 << " = " << totalA << endl;
  38. cout << sum4 << sum5 << sum6 << " = " << totalB << endl;
  39. cout << sum7 << sum8 << sum9 << " = " << totalC << endl;
  40. cout << "---------------" << endl;
  41. cout << totalD << totalE << totalF << endl;
  42. cout << " " << endl;
  43. cout << " " << endl;
  44.  
  45. }
  46.  
  47. cin.get();
  48. return 0;
  49. }
Similar Threads
Reputation Points: 46
Solved Threads: 0
Light Poster
xiikryssiix is offline Offline
25 posts
since Jul 2009
Aug 25th, 2009
0

Re: Equation (sum) doesnt add up properly.

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
Agent-of-Chaos is offline Offline
30 posts
since Jul 2009
Aug 25th, 2009
0

Re: Equation (sum) doesnt add up properly.

Move this

C++ Syntax (Toggle Plain Text)
  1. int sum1 = numX - numZ;
  2. int sum2 = numX - numY + numZ;
  3. int sum3 = numX + numY;
  4.  
  5. int sum4 = numX + numY + numZ;
  6. int sum5 = numX;
  7. int sum6 = numX - numY - numZ;
  8.  
  9. int sum7 = numX - numY;
  10. int sum8 = numX + numY - numZ;
  11. int sum9 = numX + numZ;
  12.  
  13. int totalA = sum1 + sum2 + sum3;
  14. int totalB = sum4 + sum5 + sum6;
  15. int totalC = sum7 + sum8 + sum9;
  16. int totalD = sum1 + sum4 + sum7;
  17. int totalE = sum2 + sum5 + sum8;
  18. int totalF = sum3 + sum6 + sum9;

behind the

C++ Syntax (Toggle Plain Text)
  1. cin.ignore(1000, 10);
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Aug 25th, 2009
0

Re: Equation (sum) doesnt add up properly.

If you didn't realize this code :
C++ Syntax (Toggle Plain Text)
  1. int numX;
  2. int numY;
  3. int numZ;
  4.  
  5. int sum1 = numX - numZ;
  6. int sum2 = numX - numY + numZ;
  7. int sum3 = numX + numY;
  8.  
  9. int sum4 = numX + numY + numZ;
  10. int sum5 = numX;
  11. int sum6 = numX - numY - numZ;
  12.  
  13. int sum7 = numX - numY;
  14. int sum8 = numX + numY - numZ;
  15. int sum9 = numX + numZ;
  16.  
  17. int totalA = sum1 + sum2 + sum3;
  18. int totalB = sum4 + sum5 + sum6;
  19. int totalC = sum7 + sum8 + sum9;
  20. int totalD = sum1 + sum4 + sum7;
  21. int totalE = sum2 + sum5 + sum8;
  22. 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
Last edited by firstPerson; Aug 25th, 2009 at 3:09 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 26th, 2009
0

Re: Equation (sum) doesnt add up properly.

hey thanks for all the help. fixed it and all works well now. =) thank yous!!
Reputation Points: 46
Solved Threads: 0
Light Poster
xiikryssiix is offline Offline
25 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need daily test and homework
Next Thread in C++ Forum Timeline: Need help! with encryption program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC