473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

some last minute isues...

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.

Jan 30 '06 #1
14 1556
#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::ofstream primes("primes.txt",std::ios::out | 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();
}

Jan 30 '06 #2
>
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

Jan 31 '06 #3
Neo
fe**********@hotmail.com wrote in
news:11*********************@o13g2000cwo.googlegro 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.


Jan 31 '06 #4
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

Jan 31 '06 #5
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...

Jan 31 '06 #6

felixniel...@hotmail.com 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

Jan 31 '06 #7
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();


Jan 31 '06 #8

fe**********@hotmail.com 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

Jan 31 '06 #9
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...

Jan 31 '06 #10

Gavin Deane wrote:
fe**********@hotmail.com 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();


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

-- Nitin Motgi

Jan 31 '06 #11
I have tryed everything, it just wont compile with the line
"buffer.cin.flush "

Jan 31 '06 #12
fe**********@hotmail.com wrote:
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.

--
Marcus Kwok
Jan 31 '06 #13
After cin.get flush the input buffer.cin.flush should do it.

I am sorry I meant std::cin.flush()

-- Nitin Motgi

Feb 1 '06 #14
In message <dr**********@news-int2.gatech.edu>, Marcus Kwok
<ri******@gehennom.net> writes
fe**********@hotmail.com wrote:
I have tryed everything, it just wont compile with the line
"buffer.cin.flush "
Try std::cin.flush(),


Which won't help. flush() is only defined for ostreams.

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.


--
Richard Herring
Feb 3 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neeper | last post by:
Hi, I've got a transactions table with a tstamp field (datetime type) and I need help creating a SELECT query to retrieve only last transactions that occured within the last 5 minutes. The...
3
by: t2581 | last post by:
Hi , I run restore with rollforward In job in output, last commited transaction time less then backup image time Is it normal ? RESTORE DATABASE PRICE_V8 FROM "E:\BACKUP" TAKEN AT...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
4
by: Shabs | last post by:
Hi, We are having this problem here on our project where we store all firstnames, lastnames in the oracle database in uppercase, when we retrieve the data to be used in web application and...
7
by: Petr Jakes | last post by:
I would like to do "some action" once a minute. My code (below) works, I just wonder if there is some more pythonic approach or some "trick" how to do it differently. minutes=time.localtime()...
2
by: ayman723 | last post by:
hi; I have this code in my book and when I copied it into my compiler into two files, one is header and the other is .cpp. it gives me an error, I'm sure I copied it right but can't figure out the...
9
by: mazen.s.m | last post by:
I have been trying to find a way to get the Domain and UserName of the user that last modified a file. I know how to get the owner of the file via WMI or Win32, but I can't find a way to get the...
4
by: MLH | last post by:
Would like to extract list of database objects in A97 database and each of their last-modified dates. What's the best way to proceed? And, is the last-modified date a property I can read in a...
7
by: Richard Cranium | last post by:
Trying to implement a simple rrd. Code below. When calling rrd_write, 8 successful times in a row, the 9th one causes this to happen: Program received signal EXC_BAD_ACCESS, Could not access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.