I have another assignment for class that I am having some difficulties with. I seem to be able to write the program, except for the fact that it keeps looping over and over and I'm not quite sure how to stop this. Could anyone please help?? Thanks!

This is the assignment:
Write a program that asks the user for two integers; call
them num1 and num2. Make sure the number is between 1 and 9
(including 1 and 9). For each pair of numbers, print the
division of those two numbers. Note that this is integer
division, so there will be no fractional parts.
See the examples below.

EXAMPLE 1:
Please enter a number between 1 and 9: 3
Please enter another number between 1 and 9: 2
1 0
2 1
3 1
EXPLANATION:
Row 1: The first number is 1/1, the second number is 1/2
Row 2: The first number is 2/1, the second number is 2/2
Row 3: The first number is 3/1, the second number is 3/2


EXAMPLE 2:


Please enter a number between 1 and 9: 9
Please enter another number between 1 and 9: 9
1 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0
4 2 1 1 0 0 0 0 0
5 2 1 1 1 0 0 0 0
6 3 2 1 1 1 0 0 0
7 3 2 1 1 1 1 0 0
8 4 2 2 1 1 1 1 0
9 4 3 2 1 1 1 1 1
Press any key to continue . . .


EXAMPLE 3:
Please enter a number between 1 and 9: 10
That number is out of range.
Press any key to continue . . .

Here's what I have so far:

#include <iostream>
#include <string>


using namespace std;


int main()
{
int num1, num2;


cout << "Please enter a number between 1 and 9: ";
cin >> num1;
cout << "Please enter another number between 1 and 9: ";
cin >> num2;


if ((num1 <= 1) && (num1 >=9) || (num2 <= 1) && (num2 >= 9))
{
cout << "That number is out of range!" << endl;
}
else
{
while ((num1 >= 1) && (num1 <= 9) || (num2 >= 1) && (num2 <= 9))
{
for (int num3 = 1; num1 <= num1; num3++)
{
for (int num4 = 1; num4 <= num2; num4++)
{
cout << num3/num4 << '\t';
}
cout << endl;
}
}
}
}

Recommended Answers

All 12 Replies

i think you should have two statements instead one just one
example
instead of
if ((num1 <=1 && num1 >=9) || (num2 <=1 && num2 >=9))

do this
if (num1 < 1 && num1 > 9)

and

if (num2 < 1 && num2 > 9)

also you don't want to say <= or >= because that doesn't include 1 and 9...then assignment says to include 1 and 9

You made 2 simple mistakes....
•In the for loop, num1 will always = num1
• and your while loop will always be true because num 1 and num 2 never change... here is a fixed version

#include <iostream>
#include <string>

using namespace std;

int main()
{
  int num1, num2, num3, num4;

  cout << "Please enter a number between 1 and 9: ";
  cin >> num1;
  cout << "Please enter another number between 1 and 9: ";
  cin >> num2;

  if ((num1 <= 1) && (num1 >=9) || (num2 <= 1) && (num2 >= 9))
    {
      cout << "That number is out of range!" << endl;
    }
  else
    {
      do
        {
          for (num3 = 1; num3 <= num1; num3++)
            {
              for (num4 = 1; num4 <= num2; num4++)
                {
                  cout << num3/num4 << '\t';
                }
              cout << endl;
            }
        }
      while ((num3 <= 9) && (num4 <= 9));
    }
  system("PAUSE");
  return 0;
}

It is actualy better to write this as one if statement, it saves time for both you and the compiler. btw not to be mean, neither of the comments made help him with the infinite loop error

i think you should have two statements instead one just one
example
instead of
if ((num1 <=1 && num1 >=9) || (num2 <=1 && num2 >=9))

do this
if (num1 < 1 && num1 > 9)

and

if (num2 < 1 && num2 > 9)

also you don't want to say <= or >= because that doesn't include 1 and 9...then assignment says to include 1 and 9

okay thanks. . what you said dramatically changed my program for the better! but it still repeats the loops over and over. so now all i need it to do is break after the first one. ..i used what you said and edited my program to be:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num1, num2;

    cout << "Please enter a number between 1 and 9: ";
    cin >> num1;
    cout << "Please enter another number between 1 and 9: ";
    cin >> num2;

    if ((num1 < 1) && (num1 >9) || (num2 < 1) && (num2 > 9))
    {
        cout << "That number is out of range!" << endl;
    }
    else
    {
        while ((num1 > 1) && (num1 < 9) || (num2 > 1) && (num2 < 9))
        {
            for (int num3 = 1; num3 <= num1; num3++)
            {
                for (int num4 = 1; num4 <= num2; num4++)
                {
                    cout << num3/num4 << '\t';
                }
                cout << endl;
            }
        }
    }
}

any suggestions for breaking this??

you gotta think your logic here. "Any number that's out of the range of 1 to 9 is invalid."
(num1 <= 1) && (num1 >=9)
This test condition can never be true, b/c a number cannot be less than or equal to 1 and greater or equal to 9.
try to enter a 0 see what happens. Hope that will help

oh wow. yea what i had didnt make sense at all i guess, so thanks for that!!

any ideas on how to break this once the first loop goes through?

(num1 > 1) && (num1 < 9) || (num2 > 1) && (num2 < 9)
Another hint, this is an infinite loop condition. I'm not gonna give you the answer. I want you to think through your logic.

thanks! lol this took a ton of thinking but thanks a ton!!!
however, one more question.

if i enter a 0 or another number out of range in the first slot, it still lets me enter a second number, but tells me that it's out of range after the second has been answered. know how to get it to tell me this immediately after entering the first number?

here's my coding now:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num1, num2;

    cout << "Please enter a number between 1 and 9: ";
    cin >> num1;
    cout << "Please enter another number between 1 and 9: ";
    cin >> num2;

    if ((num1 < 1) || (num1 >9) || (num2 < 1) || (num2 > 9))
    {
        cout << "That number is out of range!" << endl;
    }
    else
    {
        if ((num1 >= 1) && (num1 <= 9) || (num2 >= 1) && (num2 <= 9))
        {
            for (int num3 = 1; num3 <= num1; num3++)
            {
                for (int num4 = 1; num4 <= num2; num4++)
                {
                    cout << num3/num4 << '\t';
                }
                cout << endl;
            }
        } 
    }
}

i tried breaking the first if statement in half, but i dont know if that should even logically work:

if ((num1 < 1) || (num1 >9))
{
    cout << "That number is out of range!" << endl;
}
else
{
    if ((num2 < 1) || (num2 > 9))
{
    cout << "That number is out of range!" << endl;
}

okay i solved this!!
by moving around the half of the if statement to be in between the two cout statements.
thanks for the help!!!

this is what you can do, ask the usr for the first #, check it. if it's good, go on. not good, ask again...until the input is good(hint, hint a do while loop)
then do the same for the second.
work on your logic in the checking condition.

Hey there. I'm doing the same problems. Here is what I have for the first assignment. I got it to work, but can't figure out how to make it break. Here is what I have:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int num1, num2 = 0, num3 = 0, num4 = 0;

    cout << "Please enter a number between 1 and 9: ";
    cin >> num1;

    if ((num1 >= 1) && (num1 <= 9))  
    {
        cout << "Please enter another number between 1 and 9: :";
        cin >> num2;
    }
    if ((num2 >= 1) && (num2 <= 9))
    {
        do
        {
            for (int num3 = 1; num3 <= num1; num3++)
            {
              for (int num4 = 1; num4 <= num2; num4++)
                {
                  cout << num3/num4 << '\t';
                }
                cout << endl;
            }
         }
        while ((num3 <= 9) && (num4 <= 9));
    }
    else
    {
        cout << "That number is out of range. " << endl;

    }
    system("PAUSE");
    return (0);
}

I got the program to work, how do you break it? Could you help me : )?

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.