I tried to make a c++ prime finder using the % to check, but my code became a big mess and too long so anyone could like help me true steam or msn to help me correct mistakes etc. ? Cause the code became like 100 lines or something and didn't even work. I won't ask you too spoon feed me Il do everything myself only sometimes I need some general pointers.
Why get help through steam/msn when you can get help here? Post the code and people will eventually help out.
I didn't want to post it cause It didn't do anything any when I tried commenting I figured out that I had no Idea of what im doing anymore xD. Il start from scratch and post some code here.
Why don't you just post in the [url=http://www.facepunch.com/showthread.php?t=560020]resurrected prime finder thread[/url]? I'm sure there is some info you can use, and there is no sense having two of the same thread laying around.
Feels kinda strange to post multiple questions in someone else his post, but oke.
[editline]09:40PM[/editline]
This already fails for some reason :
[cpp]
#include <iostream>
#include <new>
int main()
{
FILE * PrimeFile;
int Primes;
int * PrimeMemory;
std::cout << "How much numbers do you want to find ?\n";
std::cout << "Input : ";
std::cin >> Primes;
PrimeMemory = new (nothrow) int [Primes];
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
std::cout << "==========================================\n";
std::cin.get();
// Do some code here
}
return 0;
}
[/cpp]
error C2065: 'nothrow' : undeclared identifier
fatal error C1060: compiler is out of heap space
I included new so why won't it get nothrow ?
It's std::nothrow.
-snip-
[QUOTE=quincy18;16723198]Thats already in the code[/QUOTE]
No it's not?
Why would you not want to throw exceptions in this case? nothrow is part of the std namespace. Every time you use nothrow, you need to spell it out as "std::nothrow" or else put "using namespace std;" at the top of your code.
-snip-
You should probably edit or snip yours for clarification as well.
Since we are talking here anyway :
[cpp]
#include <iostream>
#include <new>
int main()
{
FILE * PrimeFile;
int Primes;
int PrimesFound = 0;
int * PrimeMemory;
bool PrimeNotFound = true;
std::cout << "How much numbers do you want to find ?\n";
std::cout << "Input : ";
std::cin >> Primes;
PrimeMemory = new (std::nothrow) int [Primes];
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
PrimeMemory[0] = 2;
for (int n=3; PrimesFound < Primes; n+=2) // Loops true uneven numbers till all prime numbera have been found
{
for (int a=0; a<=Primes ;a++) // Loops trough the prime number array
{
if (PrimeMemory[a] != 0) // If the next slot isn't zero.
{
if(n % PrimeMemory[a] == 0) // If there isn't a remainder of the division break the loop
{
break;
}
} else {
PrimeMemory[a] = n; // Assign the prime number to the array.
std::cout << n << "\n";
break; // Break the loop
}
}
PrimesFound++; // Whe found a prime !
}
}
return 0;
}
[/cpp]
Its not printing any numbers :S
Am I missing something ?
[QUOTE=quincy18;16724076]Since we are talking here anyway :
[cpp]
#include <iostream>
#include <new>
int main()
{
FILE * PrimeFile;
int Primes;
int PrimesFound = 0;
int * PrimeMemory;
bool PrimeNotFound = true;
std::cout << "How much numbers do you want to find ?\n";
std::cout << "Input : ";
std::cin >> Primes;
PrimeMemory = new (std::nothrow) int [Primes];
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
PrimeMemory[0] = 2;
for (int n=3; PrimesFound < Primes; n+=2) // Loops true uneven numbers till all prime numbera have been found
{
for (int a=0; a<=Primes ;a++) // Loops trough the prime number array
{
if (PrimeMemory[a] != 0) // If the next slot isn't zero.
{
if(n % PrimeMemory[a] == 0) // If there isn't a remainder of the division break the loop
{
break;
}
} else {
PrimeMemory[a] = n; // Assign the prime number to the array.
std::cout << n << "\n";
break; // Break the loop
}
}
PrimesFound++; // Whe found a prime !
}
}
return 0;
}
[/cpp]
Its not printing any numbers :S
Am I missing something ?[/QUOTE]
debug it
How you mean debug it ?
[QUOTE=quincy18;16734207]How you mean debug it ?[/QUOTE]
you serious?
He's writing a prime number finder. Do you honestly think he's going to be an experienced debugger?
What IDE do you use? VC++? Code::Blocks?
Ok VC++...
If you press F10 it will go through your program step by step. Press F10 to go to the next step, repeat, see what it's doing. By this you should be able to tell why its not working.
thanks, found the problem :
if (PrimeMemory[a] != 0)
I wanted to check if the next array value wasn't containing a prime number, how should I do that ? (I thought dynamic memory automatically assigned zero values)
Damm the debugger is nice :D
got it working by using : if (PrimeMemory[a] > 0)
bit hacky though ?
Make all values zero with memset.
[cpp]memset( PrimeMemory, 0, Primes )[/cpp]
Put that at the beginning. (After "PrimeMemory = new ...")
after this right ?
[cpp]
PrimeMemory = new (std::nothrow) int [Primes];
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
[/cpp]
[cpp]#include <iostream>
#include <new>
int main()
{
FILE * PrimeFile;
int Primes;
int PrimesFound = 0;
int * PrimeMemory;
bool PrimeNotFound = true;
std::cout << "How much numbers do you want to find ?\n";
std::cout << "Input : ";
std::cin >> Primes;
PrimeMemory = new (std::nothrow) int [Primes];
memset( PrimeMemory, 0, Primes );
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
PrimeMemory[0] = 2;
for (int n=3; PrimesFound < Primes; n+=2) // Loops true uneven numbers till all prime numbera have been found
{
for (int a=0; a<=Primes ;a++) // Loops trough the prime number array
{
if (PrimeMemory[a] != 0) // If the next slot isn't zero.
{
if(n % PrimeMemory[a] == 0) // If there isn't a remainder of the division break the loop
{
break;
}
} else {
PrimeMemory[a] = n; // Assign the prime number to the array.
std::cout << n << "\n";
break; // Break the loop
}
}
PrimesFound++; // Whe found a prime !
}
}
return 0;
}[/cpp]
[cpp]
memset( PrimeMemory, 0, Primes );
if (PrimeMemory == 0)
[/cpp]
Isn't it best to set it after we made sure there is enough memory ?
And if I input 1000000 it will allocate 4 mb of memory ?
Yes, about 3,8 MB. Here's the final code, you were right about the allocation thing, sorry :v:
[cpp]#include <iostream>
#include <new>
int main()
{
FILE * PrimeFile;
int Primes;
int PrimesFound = 0;
int * PrimeMemory;
bool PrimeNotFound = true;
std::cout << "How much numbers do you want to find ?\n";
std::cout << "Input : ";
std::cin >> Primes;
PrimeMemory = new (std::nothrow) int [Primes];
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
memset( PrimeMemory, 0, Primes );
PrimeMemory[0] = 2;
for (int n=3; PrimesFound < Primes; n+=2) // Loops true uneven numbers till all prime numbera have been found
{
for (int a=0; a<=Primes ;a++) // Loops trough the prime number array
{
if (PrimeMemory[a] != 0) // If the next slot isn't zero.
{
if(n % PrimeMemory[a] == 0) // If there isn't a remainder of the division break the loop
{
break;
}
} else {
PrimeMemory[a] = n; // Assign the prime number to the array.
std::cout << n << "\n";
break; // Break the loop
}
}
PrimesFound++; // Whe found a prime !
}
}
return 0;
}[/cpp]
Thanks everyone for the quick responses.
Lol, last question :
This epicly failed by writing this inside primes.txt when I typed 100 :
% ) + / 5 ; = C G I O S Y a
And some more strange numbers fp can't display.
[cpp]
#include <iostream>
#include <stdio.h>
#include <new>
int main()
{
FILE * PrimeFile;
int Primes;
int PrimesFound = 0;
int * PrimeMemory;
bool PrimeNotFound = true;
std::cout << "How much numbers do you want to find ?\n";
std::cout << "Input : ";
std::cin >> Primes;
PrimeMemory = new (std::nothrow) int [Primes];
if (PrimeMemory == 0)
{
std::cout << "Could not allocate enough memory for your primes !\n";
} else {
PrimeFile = fopen ("Primes.txt","w");
memset( PrimeMemory, 0, Primes );
PrimeMemory[0] = 2;
for (int n=3; PrimesFound < Primes; n+=2) // Loops true uneven numbers till all prime numbera have been found
{
for (int a=0; a<=Primes ;a++) // Loops trough the prime number array
{
if (PrimeMemory[a] != 0) // If the next slot isn't zero.
{
if(n % PrimeMemory[a] == 0) // If there isn't a remainder of the division break the loop
{
break;
}
} else {
PrimeMemory[a] = n; // Assign the prime number to the array.
fputc( n, PrimeFile);
fputc( ' ', PrimeFile);
break; // Break the loop
}
}
PrimesFound++; // Whe found a prime !
}
}
fclose(PrimeFile);
delete[] PrimeMemory;
return 0;
}
[/cpp]
[QUOTE=Overv;16734746]Make all values zero with memset.
[cpp]memset( PrimeMemory, 0, Primes )[/cpp]
Put that at the beginning. (After "PrimeMemory = new ...")[/QUOTE]
memset works on chars:
[cpp]memset(PrimeMemory,0,Primes*sizeof(int))[/cpp]
Edit: Wait, shit. That'll only work on unsigned ints, not on signed ones.
Uhm Anyone on the file problem ?
Write them as chars instead of integers into the file.
How would I do that ?
You're not writing formatted input, you are writing the numbers are raw binary into the file, which is great for computers but not human readable. Looks like you've gone the C route with using file pointers and fopen rather than fstreams, but okay.
Instead of fputc, you should use fprintf to print formatted numbers into the text file. Like this
[code]
fprintf(PrimeFile, "%d ", n);
[/code]
This function will print the stuff in the quotations into the file specified. It also uses number delimiters to print formatted numeric information. The %d means to format and print the number in n as a decimal number.
Because all text is just a long line of characters, you need to convert numerics in your code into these sequences of alphanumerics in order to make them human-readable. The fprintf function does that automatically.
[editline]11:34AM[/editline]
What you've got going on now is that you are essentially printing out the numbers in a base-256 numbering system which is known as ascii. '+' in base 10 is actually 43. '/' is 47. In fact, I can just go through the ascii tables and map the character to it's decimal value
'+' = 43
'/' = 47
'5' = 53
';' = 59
'=' = 61
'C' = 67
'G' = 71
and so on.
[QUOTE=Cathbadh;16776539]Looks like you've gone the C route with using file pointers and fopen rather than fstreams, but okay.
[/QUOTE]
Uhm how should I do it then, like this : [url]http://www.cplusplus.com/doc/tutorial/files/[/url] (Found this a few minutes ago)
Sorry, you need to Log In to post a reply to this thread.