Hello, I am making a program where you are entering 2 numbers which should be product AND sum of other 2 numbers. The program then outputs what are the other 2 numbers. For example if I enter sum 13 and product 22 the program will output 2 and 11. But if I enter -13 (!) and 22 the program will not do anything. I changed the code that if it doesnt do anything then it should subtract decrement num1 instead of incrementing it (i.e. in the first way I increment number starting from 0 and now I decrement it). But this way doesnt work eaither. THis is my code

#include <iostream>

using namespace std;

int main()
{
    while(true) {
bool b = false;

    float sum;
    float product;


    cout << "\nEnter sum: ";
    cin >> sum;

    cout << "\nEnter product: ";
    cin >> product;

   float num1 = 0;
   float num2;

int j = 100000;
    while((b == false) && (j>0)) {
      num2 =  sum- num1;

      if((product / num1) != num2 )
      {
          b = false;
          num1++;
          num2 =  sum - num1;
      }
      else {
      cout << "\nNumber one is " << num1 << " and number two is " << num2;
      b = true;
      }

    j--;
    }

if (j<0) {
    b = false;
num1 = 0;

j = 100000;

    while((b == false) && (j>0)) {
      num2 =  sum - num1;

      if((product / num1) != num2 )
      {
          b = false;
          num1--;
      }
      else {
      cout << "\nNumber one is " << num1 << " and number two is " << num2;
      b = true;
      }

    j--;
    }

}







    }
    return 0;
    }

What should I do to make it work with -13(sum) and 22 (product)? It should say -2 and -11 for output.

Recommended Answers

All 4 Replies

Never mind, I solved it, I had to just tweak with the code a little bit:

#include <iostream>

using namespace std;

int main()
{
    while(true) {
bool b = false;

    float sum;
    float product;


    cout << "\nEnter sum: ";
    cin >> sum;

    cout << "\nEnter product: ";
    cin >> product;

   float num1 = 0;
   float num2;

int j = 100000;
    while((b == false) && (j>0)) {
      num2 =  sum- num1;

      if((product / num1) != num2 )
      {
          b = false;
          num1++;
          num2 =  sum - num1;
      }
      else {
      cout << "\nNumber one is " << num1 << " and number two is " << num2;
      b = true;
      }

    j--;
    }

if ((j<0) || (j==0)) {
    b = 0;
   num1 = -0;
   num2;

j = 100000;
    while((b == 0) && (j>0)) {
      num2 =  sum - num1;

      if((product / num1) != num2 )
      {
          b = 0;
          num1--;
          num2 =  sum - num1;
      }
      else {
      cout << "\nNumber one is " << num1 << " and number two is " << num2;
      b = true;
      }

    j--;
    }

}







    }
    return 0;
    }

Okay so let's say you put in -13 for the sum (I'm just trying to clarify). It'll say

FIRST PART
num2 = -13 - 0
22/0 != -13
num1++
num2 = -13 -1;

num2 = -13 - 1
22 /1 != -14
num1++
num2 = -14 -2

num2 = -14 - 2
22/1 != -15

SECOND PART
Okay so let's say you put in -13 for the sum (I'm just trying to clarify). It'll say num2 = -13 - 0.
22 / 0 != -13
++

So it runs again
num2 = -13 +1
22/-1 = -12

so again
num2 = -13 +2
22/-2 = -11

So to me, the second part is fine. However, your program seems to get stuck on the first part. Maybe it's stuck on the first part? You can always add a cout statement to see if it's looping consistently.

I'd make it say something like
if(sum <0 || product <0)
{/*do the second part here*/
}

sorry for late response then o.O

Thats alright

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.