set precision?

This is a discussion on set precision? within the C++ Programming forums, part of the General Programming Boards category; hey all, i am making a program to test if a number is prime (as some of you already know) ...

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    set precision?

    hey all, i am making a program to test if a number is prime (as some of you already know) and i got it working, except there is only one thing, and that is it doesnt work with numbers over 9 digets long, i tried putting
    Code:
    cin >>n >>setprecision (20);
    which i saw someone do for a cout once, but it didnt work. is there any way to make it support more digets? here is my code:
    Code:
    #include <iostream>
    #include <cmath>
    #include <string>
    int main()
    {
      int i = 2;                            //the first number it tests
      int n;
      int b;
      while(1)
      {
        cout <<"Enter a number to test.\n";
        cin >>n;
        b = (n % i);                        //be is the remainder of their number and 2
        if(n == 2 || n == 3)
        {
          cout <<n <<" is prime.\n\n";
          system("Pause");
          return 0;
        }
        if(b == 0)                          //if there is no remainder...
        {
          cout <<n <<" is not prime. It is divisible by 2.\n\n"; //...its not prime
        }
        else
        {
          for(i = 3; i < sqrt(n); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
          {
            b = (n % i);                     //same as before, except instead of 2 its the next odd value
            if(b == 0)                       //if no remainder...
            {
              cout <<n <<" is not prime. It is divisible by "<< i <<".\n\n";  //...then its not prime
              system("Pause");
              return 0;
            }   //end if
            else                             //if its not yet known, it just repeats the loop
            {
            }   //end else
          }     //end for
            cout <<n <<" is prime.\n\n";         //once i > .5n (at which point its redundent) it declares it prime
        }     //end else
      }         //end while
    }           //end main
    a minor side note, if the number is divisible by 3, it says its divisible by 2, not a big deal but i couldnt find how to fix that.
    also how do you use goto? or something like that, because after it finds a number not prime it has to quit, i would rather use something like goto then have to implament a loop.
    thank you for your time.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    the use of goto is not recommended. it produces sloppy code most of the time. just use a loop.

    I don't think setprecision will work with cin. you have to do it with cout.

    edit: this should do...
    Code:
    std::cout << std::setprecision(10) << s << std::endl;
    where s is a double with 9 decimal places.
    Last edited by alpha; 03-27-2003 at 10:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Replies: 7
    Last Post: 08-19-2007, 09:10 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 11:05 AM
  5. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21