-4

So I want this in an easier way. Is there any method for it ? Tried to find something similar here but unfortunately found nothing :(

cout << "1";
Sleep(500);
system("cls");
cout << "2";
Sleep(500);
system("cls");
cout << "3";
Sleep(500);
system("cls");
cout << "4";
Sleep(500);
system("cls");
cout << "5";
Sleep(500);
system("cls");
cout << "6"; //goes until 100
Intercase
  • 1
  • 3
  • Do you want to clear the entire terminal window or a single line? Do you develop for Linux or Windows or both? – moooeeeep Oct 05 '16 at 08:46
  • Why not use a loop? – CinCout Oct 05 '16 at 08:46
  • 1
    Somth like a loop? Are you trying to learn C++ by trial and error? That won't work. – Baum mit Augen Oct 05 '16 at 08:47
  • So the loadscreen is right at the beginning so clearing the entire terminal is ok I suppose. Sorry for my half knowledge since I am just a beginner. Bloody amateur as you will. I am on Windows. If you could show me how to delete a single line I really would appreciate. – Intercase Oct 05 '16 at 08:50

2 Answers2

2

How about a loop?

for (int i = 1; i <= 100; i++)
{
    std::cout << i;
    Sleep(500);
    system("cls");
}

Also, it's considered bad practice to use use namespace xxx;, so you should prefix the cout function with its namespace.

AntoineB
  • 4,535
  • 5
  • 28
  • 61
  • Why is it that bad ? Some people say that but no one seems to teach me why it is not good of a choice. I'll try that loop. – Intercase Oct 05 '16 at 08:49
  • Because if you have the same function in two different namespaces and you `use namespace` both of them, you'll get a conflict. And it's also better to see immediately from what namespace your function is coming, instead of having to look at the top of the file. You don't see the issue with such a simple code, but it's a good habit to take right now. – AntoineB Oct 05 '16 at 08:51
  • @Intercase [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/q/1452721/1025391) – moooeeeep Oct 05 '16 at 08:52
  • @Intercase *"Some people say that but no one seems to teach me why it is not good of a choice."* Did you try putting "C++ using namespace std bad practice" into your favorite search engine? – Baum mit Augen Oct 05 '16 at 08:53
  • Well, ok. Definitely my bad. Thanks for your help. – Intercase Oct 05 '16 at 08:55
  • use namespace xxx is fine in a cpp file, but very bad in a header... the point being that the Cpp file is not going to impact anything else, and the header file can. – UKMonkey Oct 05 '16 at 10:43
0

Well, you could put it in a loop:

for (int i = 0; i < 100; i++)
{
    if (i != 0)
    {
        Sleep(500);
        system(cls);
    }
    std::cout << i+1 << std::flush;  // Need flush to force immediate update
}

Note: I have seen a suggested edit to make the loop run from 1 to <= 100. I usually avoid such loops. Although it avoids the +1 in the output, it makes it that much harder to reason about number of times the loops run.

  • Not a good use of control structures inside a loop. Why not `std::cout << "1"; for (int i ...)` and then skipping the only-used-once if-compound? – Sebastian Mach Oct 05 '16 at 09:00
  • @SebastianMach: Because the used repeatedly code may be more complicated than this, and DRY. Note that your suggested change introduces a bug - you forgot the flush (but C++ doesn't *really* have a good way to do this; every option is a compromise). – Martin Bonner supports Monica Oct 05 '16 at 09:05
  • Increasing complexity and decreasing readability for the sake of a simple print statement is neither DRY nor KISS, but rather premature pessimization. – Sebastian Mach Oct 05 '16 at 09:14
  • It doesn't increase complexity if that is the idiom which is always used. We will have to agree to disagree. – Martin Bonner supports Monica Oct 05 '16 at 09:21
  • Never heard of that kind of argumentation. Does using a car not burn fuel because using a car is what has always been done? Does dying not kill because dying is what has always been done to become dead? // Furthermore, I don't know any circle where it's done like that. Because simply put, it's not DRY, not KISS and prematurely sub-optimal. – Sebastian Mach Oct 05 '16 at 09:24
  • Always using the same idiom makes things simpler because you don't have to work out what that code is doing, you can recognize that it is the same as any other time the idiom is used. This is same reason that I *very* much prefer to end my loop with `<`, not `<=`. You keep saying it isn't DRY, but I don't see any repetition in my code. As to KISS, I have tried to explain why I think it is, but you obviously don't agree. – Martin Bonner supports Monica Oct 05 '16 at 09:36
  • You could simplify this to `for (int i=0; i<100; std::cout << '\r' << ++i << std::flush) Sleep(500);` – moooeeeep Oct 05 '16 at 09:58
  • @moooeeeep That works ... but it's a lot harder to see it's right; and as an idiom, it doesn't generalize to more complicated case. – Martin Bonner supports Monica Oct 05 '16 at 10:01
  • @MartinBonner: But _most_ don't "always use the same idiom" you describe. Also, I've seen huge loop-bodies that became largely unreadable because of one-stop-if-statements like yours. They were DRY with your definition of DRY, but seriously, DRY is not the same as _readable_, _debuggable_, _maintainable_. Give me any 100-liner, and I will transform it into an unmaintainable pile of crap by your definition of DRY. If your definition of DRY is the only measurement, then any large software maintained by you will have a readability and performance problem. // But as a quick fix, let's disagree. – Sebastian Mach Oct 06 '16 at 05:36