You have quite a few problems. I'll address some of them, but the first thing I suggest for you to do is to
not try to write whole programs at once. Break the task down into smaller problems, ideally into problems you know how to solve directly. (Sometimes you'll need to do a bit of research, though. That's normal.)
The first thing your assignment asks you to do is to read in two integers and check that they are valid. Simplify it even further: Read in just the value of
number1
and verify that it's in range.
For example, if I wanted to read in an odd integer in the range 13-39 (inclusive) that might look like:
int odd_number;
std::cin >> odd_number;
if (odd_number < 13 || odd_number > 39)
{
std::cout << "Number must be in 13-39 range." << std::endl;
return 1;
}
if (odd_number % 2 == 0)
{
std::cout << "Number must be odd." << std::endl;
return 1;
}
I used
return 1;
because a nonzero return value from
main()
conventionally indicates that there was an error. As you can see, I don't like "using namespace std;" much.
Your code should do something similar for both
number1
and
number2
. Then print out the values you read in and validated (and return a 0 result for success). That's it. Don't even try anything else until that part is coded and tested.
Then you can move on to finding starting values for i and j; writing a loop that generates all pairs of (i, j) values from that starting point; computing the value of k for each of those pairs, and so on.
One thing you seem to be doing over and over again is treating the "=" operator as if it were a mathematical symbol. It's not. There are no equations in C++.
The statement
k = i * i + 2 * j;
is a
statement that will compute a value for the expression (i*i + 2*j) using the current values of the variables i and j; and then store the result into the variable named k. Unless you have loops or conditional statements like
if
or
switch
, statements are executed sequentially. Your assignment statement will run just once, long before you've even given values to the variables i and j. The results will be meaningless.
You need to put your statements in the order that you want them to run.
Most of your code isn't valid C++ syntax. As you write each statement, check it against the documentation you have and make sure it's right. One particular thing to watch out for is a semicolon after the condition in an
if
statement There should be nothing (except spaces and maybe comments) between the closing ) of the condition and the statement (or braced block) it controls. You have:
if (1 >= N1 <= 50 && 51 >= N2 <= 100);
{ ...
The ; at the end of the condition is an error, one that I've made many times. It's hard to spot and usually not a syntax error. C++ has quite a few pitfalls like this that can be frustrating--especially while you are learning. The best advice I can offer for these types of issues is to find out how to turn on all warnings (--Wall for GNU/clang compilers, /W4 for Visual C++) and get in the habit of making all warnings go away before trying to test.