Help with project

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
#include <Windows.h>
#include <iostream>
#include <fstream>
using namespace std;

ofstream outf;

DWORD WINAPI Prime(LPVOID Input)
	{
		int a = reinterpret_cast<int>(Input);

		for (int i=0;i<=a;i++)
		{
			if(i % 2 != 0 || i % 3 != 0 || i % 5 != 0 )
			{
				outf << i <<"\n";
			}
		}

			return (DWORD)Input;
	}



int main(int argc, char* argv[ ])
{


	DWORD ThreadId;
	HANDLE ThreadHandle;
	int Input;
	/* perform some basic error checking */
	cout<<"Enter value"<<endl;
	cin>>Input;

outf.open("Prime.txt");
	ThreadHandle = CreateThread(NULL,0,Prime,&Input,0,&ThreadId); // returns the thread identifier
	if (ThreadHandle != NULL) 
		{
			// now wait for the thread to finish
			WaitForSingleObject(ThreadHandle, INFINITE);
			// close the thread handle
			CloseHandle(ThreadHandle);
		}


outf.close();
return 0;
}




I want to have a separate thread running the function prime that will take in a number that the user will enter and file output to a .txt file all the prime numbers < = to it. I dont see any real compile errors the the issue is the when i execute the .exe file that creates the txt file; inside the txt is a long list of numbers in order. I also that the input number wasnt taken into account or something please help thanks
I dont see any real compile errors
You shouldn't have any compile time errors or warnings.

issue is the when i execute the .exe file that creates the txt file; inside the txt is a long list of numbers in order.
I'd expect to see all the numbers from zero to the number you specified that are not divisible by 2,3 or 5.

You really should put braces around those comparisons, the precedence isn't always what you expect.

I don't quite see the problem here. I will say that there's a bit more to calculating a prime number than you've implemented.
i didnt receive any real compiler errors my concern is the output that is being sent to the file, im not sure wat to change in the code for it to stop when its supposed to stop. for some reason it shoots out numbers 1 to 100000+ when i input like for example 100
closed account (DSLq5Di1)
Looks like you are assigning the address of Input to a rather than the value.
1
2
int a = reinterpret_cast<int>(Input);
int a = *(static_cast<int*>(Input));
thanks that did the trick ^
Topic archived. No new replies allowed.