Thread: Intiger to float

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Intiger to float

    I hava an programm, and it runs perfect, exept above input of 33000 it doesnt work anymore. This is because my input is an int so the solution is to make it an float, but when i do this the programm doesn't do what i want

    note: input = nInvoer
    Code:
    /* INCLUDES: */#include  <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    
    
    
    
    
    
    /* MAIN PROGRAM: */
    
    
    int main(void)
    {
       int nInvoer;
       int nIndex;
       int i;
       int nGetalgevonden;
       char chE12[12] = {10,12,15,18,22,27,33,39,47,56,68,82};
       char chAgain = 'y';
       while(chAgain=='y')
    	{
         	nGetalgevonden=2;
          printf("Voer een getal in\n") ;
          scanf("%d", &nInvoer);
    
    
          if (nInvoer <100)
          {
          	for( nIndex =0; nIndex<12; nIndex++)
       		{
          		if (nInvoer == chE12[nIndex])
             	{
           			nGetalgevonden=1;
             	}
          	}
           }
          else
          {
          	for (i=0; i<5; i++)
          	{
            	nInvoer=nInvoer/10;
            	for( nIndex =0; nIndex<12; nIndex++)
          			{
           				if (nInvoer == chE12[nIndex])
           				{
           					nGetalgevonden=1;
             			}
                	}
           }
          }
    
    
           if (nGetalgevonden==1)
           {
            printf("getal zit in de e12 reeks\n");
           }
           else
           {
            printf("getal zit niet in de e12 reeks\n");
           }
    
    
    
    
    
    
          printf("Do you want to run this program again? [y/n]:\n ");
          chAgain = getche();
    
    
    		}
    
    
          return(0);
    
    
       }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, first of all, if you're hitting the wall at 33000 you're probably working on a compiler with 16 bit integers and the best fix for that problem is to get a newer compiler with 32 bit integers... In cases like this I generally recommend Pelles C (Yes, I'm a link, click me!) which writes both 32 and 64 bit executables (depending on your OS) and is right up to date ... and free.

    Making your value into a float is going to mess you up pretty good since you are comparing with integers. Floats don't always land on exactly whole numbers so the "last bit ambiguity" problem will mess your comparisons up pretty good.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    Aigt, install a new compiler is not an option for me. This is an school assignment and we have to use borland C++ compiler. My teachers says use an float instead of an integer, but this doesn't work. There has to be an other solution than just download a new compiler. Thnx for the help btw:P
    Last edited by PvtVampire; 10-07-2011 at 04:29 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might try using a float but typecasting to int during comparisons...
    Code:
    float x;
    int list[100];
    
    if ((int)x == list[y])
    ...
    I'm not 100% shure that will solve the problem, but it's worth a try...


    But you will have to forgive my curiosity... why on Earth would any credible educator insist upon using an ancient compiler when there are far better and more up to date ones available for free?

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    Didn't solve the problem
    @ my teacher: i have no idea why we have to use an prehistoric compiler.
    Last edited by PvtVampire; 10-07-2011 at 04:40 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read your input as a character string (not an int), confirm it only contains digits, and process it accordingly.

    So an input of 33000 would be represented as "33000". Integer division by 10 would simply involve dropping the last character from the string. If you extract two digits at a time, you can extract an integer (which will be in the range [0,99]) and compare that value with elements of chE12.

    One side effect of doing that is you would not need to process cases of nInvoer less than 100 and other values as special cases. And the upper limit of values you can process will be limited only by the length of your character string.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    What about using a long int instead of an int?

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Problem found

    Aight, i found the problem. You can safely make int nInvoer to float nInvoer. The only thing to do is to change the scanf("%d"... into scanf("%f"... Thanx for the help:P

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Problem with that, as mentioned by CommonTater, is that there are integral values that a float (or a double) can't represent.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PvtVampire View Post
    my teacher: i have no idea why we have to use an prehistoric compiler.
    Does he?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. abs of float
    By taurus in forum C Programming
    Replies: 1
    Last Post: 10-10-2009, 09:13 AM
  2. (float) or (float*) wrt sizeOf()
    By jakemott in forum C Programming
    Replies: 2
    Last Post: 07-19-2009, 08:13 PM
  3. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  4. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM
  5. float
    By laasunde in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2002, 07:55 AM