while (i < j) {
Since j
starts at zero, this loop is never going to begin unless you enter a negative value for i
.
And, if you do enter a negative value, you're going to see a lot of output :-)
In other words, it should be >
rather than <
.
You probably want to get rid of the \n
in your scanf
format string as well, it serves no useful purpose here. In fact, it's harmful since it causes the scanner to keep reading whitespace until it finds a non-whitespace character, which will then be set up as the next character to read.
That means you will not see the second prompt until after you have entered something non-whitespace, such as with the following transcript:
pax> ./testprog
Enter what number you to extend
123
3
How many?
123123123pax>
And, just as an aside, you really should check things that can fail with adverse consequences. For example, if you enter a non-numeric value when using scanf
with a %d
format specifier, the variable will not be updated - it will hold whatever (arbitrary, since it's an automatic storage object with no initialisation) value it had beforehand.
You can check scanf
because it returns the number of items successfully scanned, something like:
if (scanf("%d\n", &t) != 1) {
fprintf("\nERROR: I don't know what to make of that.\n");
exit(1);
}
Here's how I would have written this, with those checks and some other minor things:
#include <stdio.h>
int main(void) {
int number, copies;
printf("Enter the number you want to extend: ");
if (scanf("%d", &number) != 1) {
fprintf(stderr, "*** ERROR: Invalid numeric entry.\n");
return 1;
}
printf("How many copies? ");
if (scanf("%d", &copies) != 1) {
fprintf(stderr, "*** ERROR: Invalid numeric entry.\n");
return 1;
}
while (copies-- > 0) {
printf("%d", number);
}
putchar('\n');
return 0;
}
I'd tend to go for something a little more robust for production-quality, but that code above should be fine for basic input functionality.