Hello everyone! I'm fairly new to C++ and am having trouble with a "nested loops" problem.

Here is the problem: USING A WHILE LOOP, get integers from the user. Then, USING A FOR LOOP, calculate a 'times table' of that number and the numbers between 1 and 20. When the user enters a zero, end the program. Run the program to see the expected output.

This is the code I've come up with so far...Even though it's "successful," it doesn't even run in my compiler. When entered into the program my professor has created for assignment submissions, it still does a times table for zero, even though I've used the while loop. I've tried collecting the data before the while loop (putting cin >> num just above while (num !=0), and that created an infinite loop.

Please help me with whatever I'm doing wrong! Thanks!

#include <iostream>
using namespace std;
int main ()

{

int num;


while (num != 0)
{
	cin >> num;
cout << "times table for " << num << ": " << endl;


for (int x = 1; x <= 17; x++)
{
cout << (num * x) << "\t";
}

cout << endl;
}
}

Recommended Answers

All 8 Replies

1) The reason why it doesn't run on your compiler but runs on the prof's compiler (or test thing) is that the value of "num" is not initialized. At line 7, it creates an int called "num" with no initial value, so the value of num could be anything when the while loop is entered. It just happens to be that your compiler makes "num" zero to start with and thus, your program stops right away. Your prof's compiler just doesn't do that (which is the "correct" behavior for a compiler... yours is weird). To fix it, just give a dummy non-zero initial value for "num" (like -1 or 231431 or whatever) before you enter the while loop.

2) The reason why it doesn't stop immediately when 0 is entered is because the condition of the while loop is only evaluated when the execution comes back to the start of the loop. So, when you enter 0, it will finish executing the loop until line 22 and then come back to like 10, evaluate the condition, and skip to the end of the loop (line 23). If you don't want that last time table to get printed, you should rearrange your while loop such that the condition is evaluated right after the input of "num".. I'll let you figure out how to do that.

3) A minor omission as well, you need to "return 0;" from the main function. Just insert "return 0;" between line 22 and 23.

Thank you so much for your (very prompt!) help. I am still having trouble though...

#include <iostream>
using namespace std;
int main ()

{

int num = -1;
cin >> num;

while (num != 0)

{

cout << "times table for " << num << ": " << endl;


for (int x = 1; x <= 17; x++)
{
cout << (num * x) << "\t";

}

cout << endl;
}
return 0;
}

This evaluates the condition right after num is entered right? But when I do that, it creates an infinite loop. I feel like I'm just overlooking something really silly but I can't figure it out! Thanks

You should change this to a "priming read" arrangement:

int someNum = 0;                            //declare and initialize input variable

cout << "Please enter a non-zero value: ";  //priming prompt
cin >> someNum;                             //priming read

while (someNum != 0) {                      //begin while loop
  for (int i = 0; i < 20; ++i) {            //begin for loop
    cout << someNum;                        //output statement
  }                                         //end for
  cout << "Please enter a non-zero value: ";//secondary prompt
  cin >> someNum;                           //secondary read
}                                           //end while

The problem is that we have to submit these codes into a program the professor has created for assignment submissions -- unfortunately, they have to match exactly what he has as the answer, word-for-word. For this problem we aren't supposed to actually ask the user for a nonzero number. I tried using your format with doing the primary and secondary read (as shown in the code below)....in my compiler, it created a times table like it should but in my professor's program, it created an infinite loop! We've also never learned this primary/secondary read thing so maybe that's why it doesn't work in his program? What gives?

{

int num = 1;

while (num != 0)

{
cin >> num;
cout << "times table for " << num << ": " << endl;


for (int x = 1; x <= 17; x++)
{
cout << (num * x) << "\t";

}

cout << endl;
cin >> num;
}
return 0;
}

This program is based on an "indeterminate" loop. An indeterminate loop is an infinite loop that only terminates under a certain condition. That's the whole point, that's how an "indeterminate" loop is supposed to work.

>>The problem is that we have to submit these codes into a program the professor has created for assignment submissions -- unfortunately, they have to match exactly what he has as the answer, word-for-word.
The output has to match, or the actual code? Either way, it's a really stupid way to grade a programming assignment. It stifles creativity and exploration, two essentials of learning programming.

Also, something else that I've noticed:

for (int x = 1; x <= 17; x++)

Why are you only going to 17? Aren't you required to go to 20?

>>The problem is that we have to submit these codes into a program the professor has created for assignment submissions -- unfortunately, they have to match exactly what he has as the answer, word-for-word.
The output has to match, or the actual code? Either way, it's a really stupid way to grade a programming assignment. It stifles creativity and exploration, two essentials of learning programming.

I guess both the output and the code have to match....therefore, if I have for example, cout << "Please enter a non-zero value: "; I would get the assignment incorrect because he doesn't want us to actually ask for the value (but rather to just assume that the user knows to input so many numbers). I agree that it's a dumb way to grade...it has definitely caused me more frustration finding tedious mistakes than helping to learn C++ programming as a whole!

Also, as far is it creating an "indeterminate" loop until a certain condition is met...in this problem that condition is "0" so why am I not meeting that condition when I have the while (num != 0) at the start of the loop?

I'm sorry, I'm very beginner at this! I feel like I was just missing or overlooking something minor in my original code, but now I'm totally confused..

for (int x = 1; x <= 17; x++)for (int x = 1; x <= 17; x++)
Why are you only going to 17? Aren't you required to go to 20?

Sorry, I did see that...we have a number of these problems to complete, all with different input numbers..must have just gotten copied & pasted from my attempt at a 17-value problem previously.

Sorry, I did see that...we have a number of these problems to complete, all with different input numbers..must have just gotten copied & pasted from my attempt at a 17-value problem previously.

Here's an excellent opportunity for a lesson. Don't use "magic numbers". You are much better off using a pre-defined constant. If the requirements change, it reduces the chances of missing one of 50 spots in your code where the number needs to change. If you use the constant, you only need to change it one (1) time, where the constant is defined. All the rest will update automagically.

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.