Algebra program problem
May 11, 2013 at 11:12pm UTC
hello,
I am making a program where you input two numbers and outputs two numbers which
are combined number1 and timed number two
for example
input: 7 12
output:3 4
the problem is when your iput is a positive and then a negative
for example
input: 7 -12
you get no answer. this is the only combination that doesn't work.
-- -+ ++ works.
can somewone plz tell me whats wrong with the code
and sorry if the code is sloppy i am a beginner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
while (true )
{
bool loop=true ;
int n1;
int n2;
cin >> n1 >> n2;
int n3=-100;
int n4=-100;
for (bool loop=true ;loop=true ; n3++)
{
for (; n4<30; n4++)
{
if (n3+n4==n1 && n3*n4==n2)
{
const char * plusSignN3 = "" ;
const char * plusSignN4 = "" ;
if (n3>0)
{
plusSignN3="+" ;
}
if (n3>0)
{
plusSignN4="+" ;
}
cout << n3 << " " << n4 << endl;
loop=false ;
}
}
n4=0;
}
cin >> n1;
}
}
May 12, 2013 at 12:24am UTC
I think you need better checks. I believe this program tries to find two numbers of which the sum equals the first number and the product equals the second number. I have tried to make it so that if there is no solution, we get back to input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
int main()
{
using namespace std;
while (true )
{
int n1(0), n2(0);
cin >> n1 >> n2;
int n3(-100), n4(-100);
for (bool loop = true ; (n3 < n1 || n3 < n2) && loop; ++n3) // You used assignment, you should use == to check for equality.
{
for (; n4 < n1 || n4 < n2; ++n4)
{
if ((n3 + n4) == n1 && (n3 * n4) == n2) // http://en.cppreference.com/w/cpp/language/operator_precedence
{
const char *plusSignN3 = "" ;
const char *plusSignN4 = "" ;
if (n3 > 0)
{
plusSignN3 = "+" ;
plusSignN4 = "+" ;
}
cout << n3 << " " << n4 << endl;
loop = false ;
break ;
}
}
n4=0;
}
//cin >> n1; // Why are you asking for n1 when you're gonna ask it again?
}
}
Topic archived. No new replies allowed.