-4

I wanted to write a program where you enter an integer and then the program asks how many times you want to repeat that number, then you enter the amount of how many times it gets repeated. Then it repeats the integer as your given amount. Can you tell me what's wrong in this code?

My code is:

#include <stdio.h>
int main(){
int i,t,j=0;
printf("Enter what number you to extend\n");
scanf("%d\n", &t);
printf("How many?\n");
scanf("%d", &i);
while (i < j) {
printf("%d", t);
j+= 1; }
return 0;
} 
TechGeek49
  • 476
  • 1
  • 7
  • 24
Acp Siam
  • 17
  • 6
  • 3
    Lets take a step back, the first thing you need to do is work out why C# and C are ***not*** the same thing – TheGeneral Oct 19 '20 at 02:16
  • Please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please learn how to [edit] your questions to improve them. – Some programmer dude Oct 19 '20 at 02:21
  • 4
    The edit you just made, was it to fix a typo in the original program, or was it a response to [the answer by paxdiablo](https://stackoverflow.com/a/64420296/440558)? If it was as a response to the answer, then don't do that! That makes the question (and any posted answers) worthless. Remember that this site isn't only for you right now, but also for future viewers who might have a similar (or even the exact same) problem. – Some programmer dude Oct 19 '20 at 02:24

1 Answers1

2
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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I fixed the thing inside while loop. But the code isn't working yet. will you pleasw try compiling it? – Acp Siam Oct 19 '20 at 02:24
  • Ha! Fixed it man! You're a genius. I removed the \n in my scanf format string and it's fixed now. Thanks buddy. I appreciate your help. – Acp Siam Oct 19 '20 at 02:30
  • I think the normal approach is to send through a good bottle of Cab Merlot or Pinot :-) – paxdiablo Oct 19 '20 at 02:48
  • 2
    As discussed extensively in [What is the effect of trailing white space in a `scanf()` format string?](https://stackoverflow.com/q/19499060/15168). the trailing newline in a `scanf()` format is actively harmful. – Jonathan Leffler Oct 19 '20 at 03:16