some last minute isues...

F

felixnielsen

In function `void print_calc()':
[Warning] converting to ìnt' from `double'

Thats the message i get when compiling, the problem should lie within
these 3 lines:
(using bloodshed dev-c++)

p_pointer = P.size();
periode_size = pow(P[e+1],2)-pow(P[e],2);
prob -= prob*pow(P[e],-1);


In this little piece of code i have the problem that it ignoret the
cin.get() endless im writing it twice, how can that be?

std::cout << "some text" <<std::endl;
std::cout << std::endl;
std::cin.get();
std::cout << " Press enter to return to menu..." << std::endl;
std::cin.get();


Im getting a file named 'gmon.out' and i have no idea why.


The program im working on is writing some stuff to af file once in a
while and its importaint that the process is completed, eg. halfway
through when the user kills the program, doesnt work. Is there a way to
solve that problem?

Thanks in advance...

NB.
Im posting the code for the complete program in a new threath, its
pretty messy and long, however some might find it interesting.
 
F

felixnielsen

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <iterator>
#include <string>
std::vector<unsigned long long> P(0);
int prime_pointer = 0;
void find_primes() {
int p_size = P.size();
bool loop;
int i;
for (unsigned long long test = P.back() + 2; P.size() == p_size;
test += 2) {
for (loop = true, i = 0; P < sqrt(test) && loop == true;
i++) {
if (test % P == 0) {
loop = false;
}
else if (P[i+1] > sqrt(test)) {
loop = false;
P.push_back(test);
}
}
}
}
void write_primes() {
std::eek:fstream primes("primes.txt",std::ios::eek:ut | std::ios::app);
for (int i = prime_pointer; i < P.size(); i++) {
primes << P << std::endl;
}
prime_pointer = P.size();
}
void read_primes() {
std::ifstream primes("primes.txt");
if (!primes && P.size() == 0) {
P.push_back(2);
P.push_back(3);
}
else if (P.size() == 0) {
copy(std::istream_iterator<__int64>(primes),
std::istream_iterator<__int64>(), back_inserter(P));
prime_pointer = P.size();
}
}
void print_calc() {
std::cout << " Reading primes from file... ";
read_primes();
int e = 0;
double prob = 1;
if (P.size() > 2) {
while (pow(P[e],2) < P.back()) {
e++;
}
for (int i = 0; i < e; i++) {
prob -= prob*pow(P,-1);
}
}
std::cout << "Done!" << std::endl;
int periode_size;
int p_pointer;
system("cls");
for (bool loop = true; loop == true; e++) {
p_pointer = P.size();
periode_size = pow(P[e+1],2)-pow(P[e],2);
prob -= prob*pow(P[e],-1);
std::cout << "
-----------------------------------------------------------------------------
" << std::endl;
std::cout << " Testing interval: " << P[e] << "^2 -> " <<
P[e+1] << "^2 - 1" << std::endl;
std::cout << " Estimated number of primes: " <<
floor(periode_size*prob) << std::endl;
while (P.back() < pow(P[e+1],2)) {
find_primes();
}
P.pop_back();
std::cout << " Primes found: " << P.size()-p_pointer <<
std::endl;
std::cout << " Uncertainty: " <<
((floor(periode_size*prob)-(P.size()-p_pointer))/periode_size)*100 <<
"%" << std::endl;
write_primes();
}
}
void documentation () {
system("cls");
std::cout << " Primesstat documentation" << std::endl;
std::cout << std::endl;
std::cout << " About" << std::endl;
std::cout << std::endl;
std::cout << " Some time ago i got an idea that, if it worked,
could estimate" << std::endl;
std::cout << " how many primesnumbers could be found in a given
interval." << std::endl;
std::cout << " In order to test if it worked as well as i thought,
i wrote" << std::endl;
std::cout << " this little piece of software." << std::endl;
std::cout << " In my oppinion it works fine and now i want to share
it with" << std::endl;
std::cout << " the world." << std::endl;
std::cout << std::endl;
std::cout << " The math" << std::endl;
std::cout << std::endl;
std::cout << " An interval allways follow the rule:" << std::endl;
std::cout << " An interval allways follow the rule:" << std::endl;
std::cout << " P(n)^2 -> P(n+1)^2 - 1" << std::endl;
std::cout << " eg. 2~2 -> 3~2 - 1" << std::endl;
std::cout << " NB. This interval is the size of 5, not 4." <<
std::endl;
std::cout << std::endl;
std::cout << " The maximum number of primes an interval can contain
is tricky" << std::endl;
std::cout << " but can be explained with a couple of examples." <<
std::endl;
std::cout << " 2^2 -> 3^2 - 1 = (3^2 - 2^2) * (2^-1)" << std::endl;
std::cout << " 3^2 -> 5^2 - 1 = (5^2 - 3^2) * (2^-1 - (2^-1 *
3^-1))" << std::endl;
std::cout << " 5^2 -> 7^2 - 1 = (7^2 - 5^2) * ((2^-1 - (2^-1 *
3^-1)) - (->)" << std::endl;
std::cout << " ((2^-1 - (2^-1 * 3^-1)) * 5^-1))" << std::endl;
std::cout << " The result should be round down to the nearest
integer." << std::endl;
std::cout << " It is somewhat tricky, but it works." << std::endl;
std::cout << std::endl;
std::cout << " The uncertainty in % is simply found:" << std::endl;
std::cout << " ((estimated number of primes - actual number of
primes) / (->)" << std::endl;
std::cout << " periode size) * 100" << std::endl;
std::cout << std::endl;
std::cout << " Troubleshooting" << std::endl;
std::cout << std::endl;
std::cout << " The program will automaticly write the primes found
to the" << std::endl;
std::cout << " file 'primes.txt', this file will grow rapitly as
more and" << std::endl;
std::cout << " more primes are found, otherwise the program will
have to" << std::endl;
std::cout << " start all over every time it is restarted. If it is
a problem" << std::endl;
std::cout << " having such a big file lying around, it can without
any" << std::endl;
std::cout << " problems be deleted." << std::endl;
std::cout << std::endl;
std::cout << " Another problem is that the program writes to the
file every" << std::endl;
std::cout << " time it is done testing an interval, therefor, if
you want to" << std::endl;
std::cout << " quit the program, do so right after it has started
testing a" << std::endl;
std::cout << " new interval." <<std::endl;
std::cout << std::endl;
std::cin.get();
std::cout << " Press enter to return to menu..." << std::endl;
std::cin.get();
}
void menu() {
std::string choice;
system("cls");
std::cout <<
"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄ¿"
<< std::endl;
std::cout << "³1. Start testing³2. View documentation³3. Quit³"
<< std::endl;
std::cout <<
"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÙ"
<< std::endl;
std::cout << std::endl;
std::cout << " Make a choice: ";
std::cin >> choice;
if (choice == "1") {
print_calc();
}
else if (choice == "2") {
documentation();
menu();
}
else if (choice == "3") {
//quit();
}
else {
std::cout << " Invalid choice!" << std::endl;
std::cout << " Make a new choice: ";
std::cin >> choice;
}
}
int main() {
menu();
}
 
N

Neo

(e-mail address removed) wrote in

In function `void print_calc()':
[Warning] converting to ìnt' from `double'

Thats the message i get when compiling, the problem should lie within
these 3 lines:
(using bloodshed dev-c++)

p_pointer = P.size();
periode_size = pow(P[e+1],2)-pow(P[e],2);
prob -= prob*pow(P[e],-1);

Here period size is type 'int' pow(...) returns float (or double)
Thus the warning.

To remove it.. do
periode_size = (int) pow(P[e+1],2)-pow(P[e],2);

that shud solve the warning
 
L

Luke Meyers

Neo said:
To remove it.. do
periode_size = (int) pow(P[e+1],2)-pow(P[e],2);

No, don't do that. If you must cast (it should be avoided if possible,
through designs that don't rely heavily on casting), don't use the old
C-style casts like this. New-style casts are superior in every way.
Use static_cast said:
that shud solve the warning

Should. Remember, warnings are not annoyances, they are hints to help
us improve our code. It's a good idea not to get into the mindset of
"how do I get rid of the warnings/how do I shut the compiler up;"
instead, prefer "how is the compiler trying to help me today?"

Luke
 
F

felixnielsen

Well, thanks to all, the gmon thing worked, and im confident that the
warning stuff will too, however i find the:
static_cast<int>(expr)
somewhat weird, but ill try my way around and get back to u if i cant
get it to work...
 
G

Gavin Deane

Well, thanks to all, the gmon thing worked, and im confident that the
warning stuff will too, however i find the:
static_cast<int>(expr)
somewhat weird, but ill try my way around and get back to u if i cant
get it to work...

It's meant to be weird. It's meant to be big and obvious and to leap
out at you. That way, on those rare occasions when you absoultely must
cast, which amounts to overruling the compiler, you (or the next person
to read your code) can easily identify those places where you decided
you know better than the compiler and give the code there the scrutiny
it requires.

The C++ style casts are also more restricted in what they can do so are
safer.

Gavin Deane
 
F

felixnielsen

k, it worked, thanks.
now only one think remains, that i actually though would be the first i
got an answer to:
In this little piece of code i have the problem that it ignoret the
cin.get() endless im writing it twice, how can that be?
std::cout << "some text" <<std::endl;
std::cout << std::endl;
std::cin.get();
std::cout << " Press enter to return to menu..." << std::endl;

Its not a problem but it is damn enoying not to know what is going on
;-)
 
G

Gavin Deane

k, it worked, thanks.
now only one think remains, that i actually though would be the first i
got an answer to:



Its not a problem but it is damn enoying not to know what is going on
;-)

When I run this program

#include <iostream>

int main()
{
std::cout << "some text" <<std::endl;
std::cout << std::endl;
std::cin.get();
std::cout << " Press enter to return to menu..." << std::endl;
std::cin.get();
}

both calls to cin.get() wait for me to enter a single character.

What are you entering at the first pause? If, at the first pause, I
press, for example, 'a' then enter, that is two characters sent to cin.
The first call to get removes the 'a' but the '\n' from the enter key
is still in the stream and is read immediately by the second call to
get() so there is no apparent pause.

Gavin Deane
 
F

felixnielsen

ok, the program runs, get to a point where the text:
Press enter to return to menu...
is printet on screen and it pauses...
i press enter once, not twice, and it is no longer paused...

how ever, if i remove the cin.get() before:
std::cout << " Press enter to return to menu..." << std::endl;
it doesnt pause...
 
N

Nitin Motgi

Gavin said:
When I run this program

#include <iostream>

int main()
{
std::cout << "some text" <<std::endl;
std::cout << std::endl;
std::cin.get();
std::cout << " Press enter to return to menu..." << std::endl;
std::cin.get();

After cin.get flush the input buffer.cin.flush should do it.

-- Nitin Motgi
 
M

Marcus Kwok

I have tryed everything, it just wont compile with the line
"buffer.cin.flush "

Try std::cin.flush(), and also please quote what you are replying to.
If you are using Google, click on the "Show Options" then use the
"Reply" link there; don't use the "Reply" link at the bottom of the
message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top