Easy do-while loop help!

Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number.

I have no idea what I'm doing. Any help would be appreciated. I only can edit line 14 by the way!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <stdio.h>

int main(void) {
   int countLimit = 0;
   int printVal = 0;

   // Get user input
   scanf("%d", &countLimit);

   printVal = 0;
   do {
      printf("%d ", printVal);
      printVal = printVal + 1;
   } while ( /* Your solution goes here  */ );
   printf("\n");

   return 0;
}
@kuroakuma97

Well, I would say you need it to keep repeating while printVal does NOT equal countLimit.
@whitenite1
So I did that, but it didn't work. However, I added a + 1 to the end and it did. Any idea why?
@kuroakuma97

output 0 to countLimit

Because what you said here, I figured you wanted the value of printVal to be one under, or to, instead of equaling countLimit. You can leave the +1 so that they are the same, or you could write it as..
} while ( printVal <= countLimit);

Which means, 'less than equals'. Looks better, IMHO, than the +1

As an aside, You should also have something printed to screen before the scanf, telling the user what is expected. Like printf("Type in a number, please..\n");
Last edited on
Hi,

When using scanf, make use of the value it returns, so you can tell whether it worked or not ......

It won't make any difference for this small program, but it really is a good idea to check that things have worked.

Maybe you can ask your teacher.

Good Luck :+)
Topic archived. No new replies allowed.