0

I am writing in c. I want to make a character moving on screen while running time .I am thinking about using printf() which can print the screen after a specified number of white spaces using %s.

I am saving this number in a variable how can I use %s with variable not constant value

The code is

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int it=0;
  int tr;
  printf("T \n");
  srand(time(NULL));
  while(it != 80 )
  {
   tr=rand()%3+1;
   switch(tr)
   {
       int tmove=it;
       case 1 :{
           if(80-it>1)
           {
               tmove+=1;
               it+=1;
           }
           else it=80;
          break;}
       case 2 :{
           if(80-it>2)
            {
                tmove=+2;
                it+=2;
            }
           else it=80;
          break;}
       case 3 :{
           if(80-it>3)
           {
               tmove+=3;
               it+=3;
           }
           else it=80;
          break;}
            default:break;
   }
   printf("%s","T");
 }
 }

I want to make T printed after number of whitecaps equal to move.

2
  • 1
    Any chance you can rephrase your question in a less-obfuscated manner? Commented Dec 20, 2014 at 13:57
  • I extended my answer since I noticed some improvements that can be made. Commented Dec 20, 2014 at 20:39

2 Answers 2

3

Use the length format specifier

printf("%*s%s\n", tmove, " ", "T");

Also, notice that each switch case can be written this way

if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else
    it = 80;

Hence you don't really need the switch you can just write

tr = rand() % 3 + 1;
if (80 - it > tr)
{
    tmove += tr;
    it    += tr;
}
else if (it > 3) /* for the default case of the switch */
    it = 80;

So the whole program transforms to

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s", tmove, " ", "T");
    }
    return 0;
}

And judging for the first printf in your code, and the fact that tmove is always increasing I would say that you also need a newline character in the second printf, the ouput looks interesting, what is it?

int main()
{
    int it = 0;
    int tr;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % 3 + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > 3)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}

one more tip, make the 3 a variable so you can modify it when you need to wihout having to change it everywhere

int main()
{
    int it = 0;
    int tr;
    int maximum = 3;

    printf("T\n");

    srand(time(NULL));
    while (it != 80 )
    {
        int tmove = it;
        tr = rand() % maximum + 1;
        if ((80 - it) > tr)
        {
            tmove += tr;
            it    += tr;
        }
        else if (it > maximum)
            it = 80;
        printf("%*s%s\n", tmove, " ", "T");
    }
    return 0;
}

this was one ouput for maximum = 3

T
  T
    T
      T
       T
         T
            T
             T
               T
                T
                 T
                  T
                   T
                     T
                      T
                         T
                            T
                              T
                               T
                                  T
                                     T
                                        T
                                         T
                                          T
                                           T
                                            T
                                              T
                                               T
                                                 T
                                                   T
                                                      T
                                                        T
                                                          T
                                                             T
                                                               T
                                                                 T
                                                                   T
                                                                    T
                                                                     T
                                                                      T
                                                                       T
                                                                        T
                                                                          T
                                                                            T
                                                                               T
                                                                               T
Sign up to request clarification or add additional context in comments.

2 Comments

Note: code never executes tmove=it, so tmove does not have a predictable value (Think of switch/case as a goto). Also tmove is out of scope if printf("%*s%s\n", tmove, " ", "T"); is to replace printf("%s","T");.
thanx a lot. i am trying trying to make T moves on screen while running ,i am thinking about clearing console each time in while loop -_- but it doesn't work
0

you can use the function clrscr() by adding #include <conio.h> and create a variable nb for making left shift after that you can print the name, in the variable name for instance,

printf("%*s",nb,name);

you can put all that in a loop and each time clear the screen, increment the nb variable and finally print the name you can see it moving

for those who are using linux

to use the clear screen you need to add #include <stdlib.h> and make system("clear screen")

1 Comment

Thanks for mentioning that ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.