prime numbers code compile error

This is a discussion on prime numbers code compile error within the C Programming forums, part of the General Programming Boards category; hi i'm new to c and i've had trouble with the following code i got from a tutorial. i get ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    7

    prime numbers code compile error

    hi i'm new to c and i've had trouble with the following code i got from a tutorial. i get 'warning assignment to int from double' when trying to compile. if anyone could help as i'd like to know what i've done wrong or if its the compiler.

    Code:
    int main(void)  //code to find the next prime number
    {
       int startingpoint, last, i, isprime, candidate;
       
         startingpoint=19;  //find the next prime after 19 in this case
        
          if(startingpoint < 2)
             {
                candidate= 2;
             }
          else if(startingpoint ==2)
             {
               candidate =3;
             }
          else
             {
             candidate=startingpoint;
             
                      if( candidate %2 ==0)
                      candidate--;
                      
                               do
                               {
                                   isprime= true;
                                   candidate +=2;
                                   last = sqrt(candidate);
                               
                              for(i=3; (i<= last) && isprime; i+=2)
                              {
                                  if((candidate %i)== 0)
                                     isprime= false;
                                 }
                             }while( ! isprime);
                         }
                        
     printf("the next prime after %d is %d. happy?\n", startingpoint, candidate);
                         
                                  std::cin.get();
                             
                                  return 0;
                             }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >last = sqrt(candidate);
    sqrt returns a double and last is an int. Because this is assigning a wider value to a narrower type, the implementation is required to warn you.

    And C++ code goes in the C++ forum.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    thank you for your help. if this includes c++ code then i'm sorry but i've been learning c and this program came from a c tutorial. though obviously not a very good one

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >this program came from a c tutorial
    >>std::cin.get();
    That's a pretty sad C tutorial, maybe you should just get a good book.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    perhaps you could help me further by telling me what equivilent code i could use from the c language to replace this 'std::cin.get()'.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well you could have a look at this for an idea of what is going on with your code, and how you might change it. Also, you need to include at least two header files for some of the functions you use in your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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