+ 2

Same answer keeps reappearing...

https://code.sololearn.com/c4DUfYeyDu2a/?ref=app ... Ok so I am making the Magic Square code for the recent challenge. How it works is you enter the 9 digits of the square top left to right, then middle, then bottom row and 2 things went wrong. 1st, the if and else statements both happened for incorrect or correct entered 9 numbers as you see in the code. I fixed that by adding a return 0; in the if statement so that it would reset and I was wondering if that was the correct way to fix it? 2nd, if you type: 8 1 6 3 5 7 4 9 2 (8 1 6 3 5 7 4 9 2 in code) its a valid Magic Square and the answer is 15. However when you type something incorrect, it always shows 15 anyway. So what I'm trying to do is have it evaluate the 9 numbers you type, and if the answer is a whole number (since the answer has to be a non-decimal), it will say the said answer. But I dont know what to do for the argument of the if statement so to works, and why the codes answer always evaluates to 15.

7th Aug 2018, 1:02 AM
Slushie
Slushie - avatar
10 Respostas
+ 2
Oh wow that did work. Thanks! But what would you do for the if argue ment to include all whole numbers?
7th Aug 2018, 1:32 AM
Slushie
Slushie - avatar
+ 2
No it already does that. I meant like if your trying to find a Magic answer of like 16, 17, etc. Like the code will search for those answers.
7th Aug 2018, 1:44 AM
Slushie
Slushie - avatar
+ 1
Does that mess it up in any way? Like try it. Type 9 numbers that are spaced and enter it. Always 15.
7th Aug 2018, 1:12 AM
Slushie
Slushie - avatar
+ 1
I mean that makes sense... but if you try it with random numbers it works out as 15 even though it is clearly not 15.
7th Aug 2018, 1:21 AM
Slushie
Slushie - avatar
+ 1
Dominic, Carter Mitchell you need to check whether answer is whole number or not for magic number... just use below code: long double answer = 12; if(static_cast<long int>(answer)==answer) { cout << "magic number" << endl; } else { cout << "not magic number" << endl; } note : change data type of answer variable and put if condition at bottom of your code
7th Aug 2018, 2:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
You put a semi colon on your else statement.
7th Aug 2018, 1:10 AM
Dominic Nicholas
Dominic Nicholas - avatar
0
I accidentally posted too early. You need to write if (answer == 15).
7th Aug 2018, 1:19 AM
Dominic Nicholas
Dominic Nicholas - avatar
0
I got it to work like this. #include <iostream> using namespace std; int main() { int first, second, third, fourth, fifth, sixth, seventh, eighth, ninth; cin >> first; cin >> second; cin >> third; cin >> fourth; cin >> fifth; cin >> sixth; cin >> seventh; cin >> eighth; cin >> ninth; //------------------------------------------// cout << "Your Magic Square is:" << endl; cout << first << " " << second << " " << third << endl; cout << fourth << " " << fifth << " " << sixth << endl; cout << seventh << " " << eighth << " " << ninth << endl; first *= 3; second *= 2; third *= 3; fourth *= 2; fifth *= 4; sixth *= 2; seventh *= 3; eighth *= 2; ninth *= 3; long answer = (first + second + third + fourth + fifth + sixth + seventh + eighth + ninth)/8; if (answer == 15) { cout << "\nThe Magic Answer is " << answer << endl; system("pause"); //return 0; } else { cout << "\nYour Magic Square does not have a Magic Answer..." << endl; } //system("pause"); return 0; }
7th Aug 2018, 1:26 AM
Dominic Nicholas
Dominic Nicholas - avatar
0
What do you mean? Do you want to display the first to ninth values?
7th Aug 2018, 1:41 AM
Dominic Nicholas
Dominic Nicholas - avatar
0
You could do it like if (answer == 15 || answer == 16 || answer == 17) or you could set a range if (answer >= 15 && answer < 18)
7th Aug 2018, 1:59 AM
Dominic Nicholas
Dominic Nicholas - avatar