0

I need to have a for loop counter that should replace the number with a text when it's divisible by a set of numbers. I already have the loop and the code working, but the '\r' in my printf function does not work someone else's compiler.

Here is my code and outputs from different compilers so you can identify the issue better.

#include <stdio.h>

void hey(void)
{
    
    for (int i = 50; i <= 100; i++)
    {
        printf("%d", i);

        if (i % 5 == 0)
        {

            printf("\rHEY1 ");
        }
        if (i % 40 == 0)
        {

            printf("\rHEY2 ");
        }
        if (i % 40 == 0 && i % 5 == 0)
        {

            printf("\rHEY3 ");
        }

        printf("\n");
    }
}

int main(void)
{
    hey();
    return 0;
}

This's the result on my compiler, which is how I exactly want:

My output

and this one is how it appears on the online compiler that my teacher uses to mark:

Other output

For some reason, it doesn't remove the number to replace with 'Hey' in the other compiler. Instead, prints it on a new line.

How do I fix this? It should remove the number and print the letters instead of it as it's on the first screenshot. TIA.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 2
    I suspect that your teacher does not expect you to use `'\r'` and instead expects you to figure out how NOT to output the number in the first place and instead right away print the "HEY". That would be reliable in all environments. Please double check the assigment and confirm - or quote the assignment to clarify what exactly is requested. – Yunnosch Aug 16 '21 at 05:05
  • Yep. It does not say anything whether I should use '\r' or not, but that was the solution I found myself because it worked on my compiler. So I don't know if there's any other way to do it yet. – beginnercanadian444 Aug 16 '21 at 05:10
  • The shown output is not long engough to show case 80. What do you want there? – Yunnosch Aug 16 '21 at 05:11
  • I replaced the screenshots by typed text and removed parts of your example code which are not needed to understand the question. – Martin Rosenau Aug 16 '21 at 05:13
  • Since the number 80 is both divisible by 5 and 40, I want 'HEY3' there. Also that's what I got on my compiler, again. But it doesn't even work for 50 or 55 as shown. – beginnercanadian444 Aug 16 '21 at 05:13
  • What you did is a [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Be clear about what your *actual* problem is, and that your attempt with \r is an *attempt* and not the *actual problem*. – klutt Aug 16 '21 at 05:13
  • Maybe the way I titled the question is wrong. Yes, it doesn't supposed be done with \r, it just worked on mine and didn't work on another. – beginnercanadian444 Aug 16 '21 at 05:15
  • Are you learning if-else / control statements? – Aval Sarri Aug 16 '21 at 05:16
  • Printing and then deleting is not a good approach here. Instead, find the logic that only prints what you want. Hint: google "fizzbuzz", because this is the fizzbuzz problem. – klutt Aug 16 '21 at 05:16
  • @MartinRosenau Your replacement was well intended, but incorrect. It did not match the shown code or the ouput in the picture. I undid it. – Yunnosch Aug 16 '21 at 05:19
  • Closed it as a dup. The target is about C#, but since the logic is very simple it should be easy to translate to C. – klutt Aug 16 '21 at 05:20
  • I voted for re-opening the question because the the actual question ("Why does `\r` not work") is still not answered (neither here nor in the "duplicate question"). And there is one very common use case where the answer in "duplicate question" does not apply to: `printf("\rProgress is %d percent complete... ",percent);` In this case, `\r` is also used to replace a line in a loop! – Martin Rosenau Aug 16 '21 at 05:44
  • @Yunnosch Why did you revert my edit? As far as I know, screen shots of text output are not allowed on this web site. – Martin Rosenau Aug 16 '21 at 05:46
  • @MartinRosenau Did you read the comment I addressed at you? I undid because it was incorrect. The fact that other users cannot reliably reproduce text from screen shots is the reason for screen shot being unappreciated and for the recommendation not to try typing text from them to edit the question to improve. Your edit is an example, because you did not reproduce the output shown by OP. – Yunnosch Aug 16 '21 at 06:12

1 Answers1

0

I suspect that your teacher does not expect you to use '\r' and instead expects you to figure out how NOT to output the number in the first place and instead right away print the "HEY". That would be reliable in all environments.

Her is how you can do that. Note that I used else if and that I reordered the checks, in order to get what I am sure is what you want for case 80.
I also simplified the 80 condition, because the reordering allows that.
I chose the "HEY3" option for 80. You will never find "HEY2" AND "HEY3" in an output of this code, because anythign divisible by 40 is always also divisible by 5.


#include <stdio.h>

void hey(void)
{
    
    for (int i = 50; i <= 100; i++)
    {
        if (i % 40 == 0 /* && i % 5 == 0 */)
        {

            printf("HEY3 ");
        } else if (i % 5 == 0)
        {

            printf("HEY1 ");
        } /* else if (i % 40 == 0)
        {
            printf("HEY2 ");
        } */ else  
        {
            printf("%d", i);
        }

        printf("\n");
    }
}

int main(void)
{
    hey();
    return 0;
}

Output:

HEY1 
51
52
53
54
HEY1 
56
57
58
59
HEY1 
61
62
63
64
HEY1 
66
67
68
69
HEY1 
71
72
73
74
HEY1 
76
77
78
79
HEY3 
81
82
83
84
HEY1 
86
87
88
89
HEY1 
91
92
93
94
HEY1 
96
97
98
99
HEY1 
Yunnosch
  • 26,130
  • 9
  • 42
  • 54