URGENT PLEASE HELP

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?


#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<string>
#include <Windows.h>
using namespace std;

int main(int argc, TCHAR*argv[]) {

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;



//Display multiples


while (x != 1) {


x = x / 2;
y = y * 2;

cout << "\n\n\t" << x;
cout << "\t" << "x" << setw(8) << y;

if (x % 2 >= 1)
{
result += y;
cout << setw(8) << "[" << y << "]" << endl;
}
if (x % 2 == 0) {
cout << setw(5) << "+" << endl;
cin.ignore();
}
}

//Display answer

cout << "------------------------------------------------" << endl;

cout << "\n\n\tFinal Answer: " << result << endl;



//Ask user if they want to run entire program again
{
cout << "\n\n\tWould you like to run the program again? [Y/N] ";
cin >> choice;

choice = toupper(choice); // Uppercase Answer

if (choice != 'Y' && choice != 'N') {

cout << "\n\n\tInvalid input. Please re-enter.";
cin >> choice;
system("cls");
}
else

system("cls");
}

cin.clear();

} while (choice != 'N');

cout << "This program will now end" << endl;

cin.clear();
_getch();
return 0;
}
Last edited on
Just set
result = 0
immediately after the start of the do loop.

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.
Last edited on
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.
Last edited on
I think you have two choices:

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]? 

Last edited on
The latter worked!!! Thank you so much for your help! I deleted a lot of the unnecessary stuff, and my program is easier to read. Thank you again!
Topic archived. No new replies allowed.