some last minute isues...

Discussion in 'C++' started by felixnielsen@hotmail.com, Jan 30, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    #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();
    }
     
    , Jan 30, 2006
    #2
    1. Advertising

  3. Nitin Motgi Guest

    >
    > 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
     
    Nitin Motgi, Jan 30, 2006
    #3
  4. Neo Guest

    wrote in
    news::

    > 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.
    >
     
    Neo, Jan 31, 2006
    #4
  5. Luke Meyers Guest

    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
     
    Luke Meyers, Jan 31, 2006
    #5
  6. Guest

    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, 2006
    #6
  7. Gavin Deane Guest

    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
     
    Gavin Deane, Jan 31, 2006
    #7
  8. Guest

    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, 2006
    #8
  9. Gavin Deane Guest

    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
     
    Gavin Deane, Jan 31, 2006
    #9
  10. Guest

    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, 2006
    #10
  11. Nitin Motgi Guest

    Gavin Deane wrote:
    > 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
     
    Nitin Motgi, Jan 31, 2006
    #11
  12. Guest

    I have tryed everything, it just wont compile with the line
    "buffer.cin.flush "
     
    , Jan 31, 2006
    #12
  13. Marcus Kwok Guest

    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
     
    Marcus Kwok, Jan 31, 2006
    #13
  14. Nitin Motgi Guest


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

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

    -- Nitin Motgi
     
    Nitin Motgi, Jan 31, 2006
    #14
  15. In message <dronq2$7k5$>, Marcus Kwok
    <> writes
    > 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
     
    Richard Herring, Feb 3, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJhZCBSb2JlcnRz?=
    Replies:
    1
    Views:
    384
    =?Utf-8?B?UGF1bA==?=
    Aug 23, 2004
  2. Denebola
    Replies:
    3
    Views:
    983
    Denebola
    Feb 27, 2006
  3. Hazel Hopes
    Replies:
    0
    Views:
    339
    Hazel Hopes
    Jul 22, 2004
  4. Petr Jakes

    do "some action" once a minute

    Petr Jakes, May 9, 2006, in forum: Python
    Replies:
    10
    Views:
    496
    James Stroud
    May 10, 2006
  5. kbutterly
    Replies:
    1
    Views:
    361
    Jon Paal
    Feb 11, 2007
Loading...

Share This Page