Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > some last minute isues...

Reply
Thread Tools

some last minute isues...

 
 
felixnielsen@hotmail.com
Guest
Posts: n/a
 
      01-30-2006
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.

 
Reply With Quote
 
 
 
 
felixnielsen@hotmail.com
Guest
Posts: n/a
 
      01-30-2006
#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[i] < sqrt(test) && loop == true;
i++) {
if (test % P[i] == 0) {
loop = false;
}
else if (P[i+1] > sqrt(test)) {
loop = false;
P.push_back(test);
}
}
}
}
void write_primes() {
std:fstream primes("primes.txt",std::ios:ut | std::ios::app);
for (int i = prime_pointer; i < P.size(); i++) {
primes << P[i] << 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[i],-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();
}

 
Reply With Quote
 
 
 
 
Nitin Motgi
Guest
Posts: n/a
 
      01-30-2006
>
> Im getting a file named 'gmon.out' and i have no idea why.
>

I think you are compiling with -pg option if you are using cc.

-- Nitin Motgi

 
Reply With Quote
 
Neo
Guest
Posts: n/a
 
      01-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
news:(E-Mail Removed) ups.com:

> 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


> 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.
>


 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-31-2006
Neo wrote:
> 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<int>(expr) above.

> 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

 
Reply With Quote
 
felixnielsen@hotmail.com
Guest
Posts: n/a
 
      01-31-2006
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...

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-31-2006

(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
felixnielsen@hotmail.com
Guest
Posts: n/a
 
      01-31-2006
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

>std::cin.get();


 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-31-2006

(E-Mail Removed) wrote:
> 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
>
> >std::cin.get();


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

 
Reply With Quote
 
felixnielsen@hotmail.com
Guest
Posts: n/a
 
      01-31-2006
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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Some one suggest me the best last minute Resource for 70-290 Migel MCSE 6 03-06-2006 02:38 PM
Refresh every minute, on the minute. Denebola Java 3 02-27-2006 07:39 AM
Last minute security rollout for Windows 2000... Imhotep Computer Security 3 06-29-2005 02:04 PM
Reformatting... last minute tips? Simply Me Computer Support 19 11-22-2004 04:03 PM
Programming XML - last minute training offer Hazel Hopes XML 0 07-22-2004 01:15 PM



Advertisments