I'm a beginner in C++ (you'll be able to tell through my code), but I really need help with my project. I'm having a problem with my do-while loop. When the user selects to restart my program, the result from the first run is still stored. It's as if it's keeping a running total of "result"
for ex. If the answer to the first run is 20, when the program restarts 20 is being added to the answer when it should just be cleared.
What am I doing wrong and how can I fix it?
HWND hWnd = GetConsoleWindow(); //Get Console Window function
ShowWindow(hWnd, SWP_SHOWWINDOW); //show window in full screen
//variables
int x = 0;
int y = 0;
int result = 0;
char choice;
cout << "Welcome to the Egyptian Multiplication Calculator." << endl;
cout << "Instructions: Simply enter two numbers you'd like to multiply by and we will show you a cool way to multiply them" << endl;
cout << "Let's begin!" << endl;
cout << "------------------------------------------------------------------------------------------------------------------------" << endl;
do {
//Prompt for first input
cout << "Please enter your first number: " << endl;
cin >> x;
//Prompt for second input
cout << "Please enter your second number: " << endl;
cin >> y;
system("cls");
cout << "------------------------------------------------" << endl;
cout << setw(8) << x << " x " << y << endl;
cout << "------------------------------------------------" << endl;
However, your ALGORITHM IS WRONG. It will fail whenever the first number is odd (try 45 x 2). You need to go back and think how this algorithm works. All it does in principle is find the binary representation of the first number (and you will miss cases where the last digit is 1). Also, your layout is very unnatural as the written multiplies do not give the numbers in brackets.
Also, please put your code in code tags and remove all the completely unnecessary windows-related stuff: then people can run it in C++ shell.
Thank you for your input, I was able to fix my initial problem. I really appreciate your comments as I'm still learning. This is my first programming class, and my professor doesn't teach the subject well at all, so I almost have to teach myself this subject .
As for my algorithm. Can you give me an idea on how I would be able to fix that error?
I think it is my while statement,
while (x!=1)..i have played around with it, but I'm not getting the output I want.
I want it to display
45 x 2
_________
0 2
22 4
(etc) (etc)
....................
and add 2 (or y) from there.
It should stop dividing x by 2 once x is 1 in the chart.
You don't need to give me an answer, but I would like some guidance on how to fix this problem.
EITHER
You duplicate a little code and also do a test on x % 2 >= 1 (actually x % 2 == 1 would do) with update of result before the do loop starts; this way you capture the odd numbers as well. It's only that last binary digit which is being missed.
OR
You change your loop logic so that the x=x/2 etc lines come after the test for a binary digit; something like
1 2 3 4 5 6 7 8 9 10 11 12
while ( true )
{
....
if ( x % 2 == 1 ) // If original x contains this binary 1 then add 2^n * original y
.... // (ok as current)
if ( x == 1 ) break; // Exiting the loop from the middle
x = x / 2;
y = y * 2;
}
I prefer the latter because there is no code duplication, but the logic is harder to think through.
I would strongly encourage you to get rid of the unnecessary windows stuff and simplify your output until the algorithm is actually working.
Example of slightly less confusing output:
Welcome to the Egyptian Multiplication Calculator.
Please enter two integers you'd like to multiply
--------------------------------------------------
Please enter your first number: 45
Please enter your second number: 2
--------------------------------------------------
x y
45 2 [2]
22 4
11 8 [8]
5 16 [16]
2 32
1 64 [64]
--------------------------------------------------
Final Answer: 90
Would you like to run the program again [Y/N]?