How to display "Loading..." with having 3 dots animating using C++? - c++

You may have seen in many places "Loading..." where the 3 dots(or more), viz. "...", first appear one by one, then after displaying all the dots, they disapperar and once again appear one by one (so, total 2 times).
To elaborate:-
Stage 1:
Loading.
Stage 2:
Loading..
Stage 3:
Loading...
Then, it repeats second time! and then terminate!
So, for this I prepared a C++ program and its source code is:
#include <iostream.h>
#include <time.h>
#include<dos.h>
int main()
{
cout << "Loading";
cout.flush();
for (;;) {
for (int i = 0; i < 3; i++) {
cout << ".";
cout.flush();
sleep(1);
}
cout << "\b\b\b \b\b\b";
}
return 0;
}
This program is not terminating. It doesn't stops! How can I edit this to make this terminate?
Please post codes supported by Turbo C++ Compiler, as I am not too much aware of the ANSI C++!! :P
Gguys, please help me out? :)
Thanks, in advance! :)

Why not delete those points using a backspace?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
cout << "Loading";
cout.flush();
for (;;) {
for (int i = 0; i < 3; i++) {
cout << ".";
cout.flush();
sleep(1);
}
cout << "\b\b\b \b\b\b";
}
return 0;
}

I'd go for the following solution using "carriage return":
for (int j = 0; j < 3; j++) {
cout << "\rLoading \rLoading";
for (int i = 0; i < 3; i++) {
cout << ".";
sleep(300);
}
}
This overwrites the current line (with all dots) and repostions cursor at the end of "Loading".
BTW, the sleep value here is in milliseconds. You may have to change that if yours is in seconds.
Also, in your program you have nested loops both with running i - I doubt that this will work as intended...

Related

threaded for loop reaching values it's not supposed to reach

I'm messing around with multithreading in c++ and here is my code:
#include <iostream>
#include <vector>
#include <string>
#include <thread>
void read(int i);
bool isThreadEnabled;
std::thread threads[100];
int main()
{
isThreadEnabled = true; // I change this to compare the threaded vs non threaded method
if (isThreadEnabled)
{
for (int i = 0;i < 100;i++) //this for loop is what I'm confused about
{
threads[i] = std::thread(read,i);
}
for (int i = 0; i < 100; i++)
{
threads[i].join();
}
}
else
{
for (int i = 0; i < 100; i++)
{
read(i);
}
}
}
void read(int i)
{
int w = 0;
while (true) // wasting cpu cycles to actually see the difference between the threaded and non threaded
{
++w;
if (w == 100000000) break;
}
std::cout << i << std::endl;
}
in the for loop that uses threads the console prints values in a random order ex(5,40,26...) which is expected and totally fine since threads don't run in the same order as they were initiated...
but what confuses me is that the values printed are sometimes more than the maximum value that int i can reach (which is 100), values like 8000,2032,274... are also printed to the console even though i will never reach that number, I don't understand why ?
This line:
std::cout << i << std::endl;
is actually equivalent to
std::cout << i;
std::cout << std::endl;
And thus while thread safe (meaning there's no undefined behaviour), the order of execution is undefined. Given two threads the following execution is possible:
T20: std::cout << 20
T32: std::cout << 32
T20: std::cout << std::endl
T32: std::cout << std::endl
which results in 2032 in console (glued numbers) and an empty line.
The simplest (not necessarily the best) fix for that is to wrap this line with a shared mutex:
{
std::lock_guard lg { mutex };
std::cout << i << std::endl;
}
(the brackets for a separate scope are not needed if the std::cout << i << std::endl; is the last line in the function)

simple for loop finishes and fails the run

For some unknown reason this simple code runs, does what it's expected to do and then crashes the run. I am using NetBeans IDE, which overlapped my arrays before (tends to be buggy), so I was wondering if someone gets the same error - that would mean I certainly have to change the IDE environment.
#include <iostream>
using namespace std;
int main ()
{
int first[4][4];
for (int a = 0; a < 5; a++)
{
for (int b = 0; b < 5;b++)
{
cout << a << " " << b << " ";
if (first [a][b] != 0)
{
first[a][b] = 0;
}
cout << first[a][b] << " ";
}
cout << endl << endl << endl;
}
return 0;
};
here you are declearing a array with 4 indexes.In c/c++ index number starts at 0.
In your code you are saying :
int first[4][4];
that means indexs are : 0 1 2 3.Array length or total index are 4.
But in for loop you are saying
for (int a = 0; a < 5; a++) {
....
}
so you are trying to access index number 0 1 2 3 4 respectively.But remember you don't have index number 4.That is why it should give array index out of bound error.
Also at the end of main function you are using a semicolon.remove that
main () {
....
};
Hope this solves the problem.From next time Please try to provide details about the errors your IDE is giving you as it will be easier for the people who are giving answer.

Program to create a number of threads based on user input not working (cin)?

My apologies if this question looks simple. I'm still learning about threads. I already tried searching for a solution to this on here but didn't find any.
I'm trying to get my program to create a number of threads based on user input (ex: "cin >> 5" will create 5 threads) but it says the "i" in "threads myThreads[ i ]" needs to be a constant value. The code is below:
void exec(int n)
{
cout << "Thread " << n << endl;
}
int main()
{
int numThreads = 0;
// create threads
cin >> numThreads;
thread myThreads[numThreads]; // this part says myThreads "must be a constant value"
for (int i = 0; i < numThreads; i++)
{
myThreads[i] = thread(exec, i);
}
for (int i = 0; i < numThreads; i++)
{
myThreads[i].join();
}
cout << "Done!" << endl;
}
Any ideas as to how that section can be fixed? I've tried a few different ways but they haven't worked so far. Thank you very much.
There's no problem with multithreading. The problem is static array that you using as dynamic array.
Try something like this:
thread* myThreads = new thread[numThreads];
More about dynamic memory in C++:
http://www.cplusplus.com/doc/tutorial/dynamic/
UPD By James Adkison:
Do not forget to delete[] your array to avoid memory leaking.

how to print my name in next line in C++

I want to print my name ten times in new lines. Here is my code:
#include "stdafx.h"
#include<iostream>
using namespace std;
void main()
{
int i;
for (i = 1; i <= 10; ++i)
cout << "krishna " ;
getchar();
}
I need a break or "\n" after the first row of printing. How to get that ?
You should NOT use std::endl;
for (i = 1; i <= 10; ++i)
cout << "krishna " << endl;
Its indeed (after getting an comment) better to use '\n' (Windows only)
Look at this FAQ -> https://isocpp.org/wiki/faq/input-output#endl-vs-slash-n
You can use Both endl or '\n'.your code will look like this-
for (i=1;i<=10;i++)
cout<<"Krishna\n";

Calling function in C++

I'm trying to call a function with no return type but it doesn't seem to be getting called.
The code looks something like this(summarized):
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int ItemsInQuestion[4];
void GetQuestions(int NumQuests);
int main()
{
int NumberOfQuestions = 0;
srand((unsigned)time(NULL));
cout << "How many questions would you like?" << endl;
cin >> NumberOfQuestions;
cout << NumberOfQuestions << " questions will be asked.";
GetQuestions(NumberOfQuestions);
system ("PAUSE");
return 0;
}
void GetQuestions(int NumQuests)
{
for(int Questions=NumQuests; Questions>NumQuests; Questions++)
{
ItemsInQuestion[0]=(rand()%(263))+1;
ItemsInQuestion[1]=(rand()%(263))+1;
ItemsInQuestion[2]=(rand()%(263))+1;
ItemsInQuestion[3]=(rand()%(263))+1;
cout << ItemsInQuestion[0] << ' ' << ItemsInQuestion[1] << ' ' <<ItemsInQuestion[2] << ' ' << ItemsInQuestion[3];
}
}
The line that outputs the values in the array never comes up. What is causing this?
Because
int Questions=NumQuests;
and
Questions>NumQuests;
don't like each other.
You set Questions to NumQuests and then tell the loop to keep going as long as Questions is strictly greater than NumQuests, which it isn't to start off with.
Even if it was, you'd soon run into an overflow and undefined behavior.
This is not the way of using for-loops :
for ( __Initialization, __Condition, __Increment)
As Grigore pointed out in his answer, your initialization is wrong. As Questions=NumQuest, the statement Questions>NumQuests is false from the beginning, and your code is equivalent to
for ( ; 1<1 ; ) // I'm a lazy loop, I'm ugly and useless
There is an infinite number of way to do what you want :
// Direct : 0, 1, 2, .. NumQuest-1.
for (int Questions=0 ; Questions < NumQuests ; Questions++)
{ ... }
// Reverse : NumQuest, ..., 2, 1.
for (int Questions=NumQuests ; Questions > 0 ; Questions--)
{ ... }