Output to console while my thread is not finished.

I have a basic is a prime number program and I want to be able to output something to console so it doesn't look stuck. Can you guys give me some resources of this I can't seem to find it on Google?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* C++ Program to implement Sieve of Eratosthenes
*/

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <future>
#include <windows.h>

enum PrimeResult { IsPrime, IsNotPrime, UnableToCalculate };

// isPrime based on Sieve of Eratosthenes' Pseudocode @ https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
PrimeResult isPrime(const long long int n) {
	// The index of the array represents the number and the value of that index represents if the number is prime.
	// By default all numbers are assumed to be prime.
	std::vector<bool> v(static_cast<unsigned int>(n), true);
	// One is not a prime number and needs to be hard coded to be false.
	v[0] = false;
	// Translation of Sieve of Eratosthenes' Pseudocode
	try
	{
		for (long long int i = 2; i <= n; i++)
			for (long long int j = i * i; j <= n; j += i)
				v[static_cast<unsigned int>(j - 1)] = false;
	}
	catch (...)
	{
		return UnableToCalculate;
	}
	// If the end of the array is true then Sieve of Eratosthenes determined that it is a prime else it's not.
	return v.back() ? IsPrime : IsNotPrime;
}

// C++ doesn't natively provide an is_number function or method...
bool is_number(const std::string& s)
{
	return !s.empty() && std::find_if(s.begin(), s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
}

int main() {
	std::string NumberInput;

	do
	{
		std::cout << "***isPrime Program***\nEnter a non numeric character or a negative number to exit the application.\nEnter the number you wish to verify is a prime number: ";
		std::cin >> NumberInput;
		if (is_number(NumberInput)) {
			auto future = std::async(isPrime, std::atoi(NumberInput.c_str()));
			std::cout << "Calculation in progress";
			while (future.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
				std::cout << ".";
				Sleep(1000);
			}
			std::cout << "\nDone!\n";
			switch (future.get())
			{
			case IsPrime:
				std::cout << "The number " << NumberInput << " is a prime number . . .\n";
				system("pause");
				std::cout << std::endl;
				break;
			case IsNotPrime:
				std::cout << "The number " << NumberInput << " is not a prime number . . .\n";
				system("pause");
				std::cout << std::endl;
				break;
			case UnableToCalculate:
				std::cout << "The number " << NumberInput << " is just to big for this simple application to calculate . . .\n";
				system("pause");
				std::cout << std::endl;
				break;
			default:
				break;
			}
		}			
		else
			break;
	} while (is_number(NumberInput));
	
	std::cout << "The program will now exit . . .\n";
	system("pause");
}
Last edited on
Where in this code do you want the console to be saying things? What in this code takes so long that you need to reassure people it's still working? That's where you need to put some output.

I would guess that the time is being taken in here:

for (long long int i = 2; i <= n; i++)
			for (long long int j = i * i; j <= n; j += i)
				v[static_cast<unsigned int>(j - 1)] = false;


in which case you don't need threads.


1
2
3
4
5
6
7
8
for (long long int i = 2; i <= n; i++)
{
    std::cout << "Still working... \n";
        for (long long int j = i * i; j <= n; j += i)
         {
				v[static_cast<unsigned int>(j - 1)] = false;
         }
}
would probably do it
Last edited on
@Repeater, I wish I would have read this earlier. I've managed to do it with threading tho.

1
2
3
4
5
6
7
auto future = std::async(isPrime, std::atoi(NumberInput.c_str()));
std::cout << "Calculation in progress";
while (future.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
        std::cout << ".";
	Sleep(1000);
}
std::cout << "\nDone!\n";
Last edited on
printing just makes it take that much longer.
but if you insist, print i in the loop, so you can see how far along it has gotten. After a few pages of the same text, its hard to tell...
Topic archived. No new replies allowed.