Thread: confusion with integers (newbie question)

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    confusion with integers (newbie question)

    hey, im 15 years old and just starting out learning C. my question is this:
    when i use "atoi" to change a character variable to an integer its works fine. however when i use it to change a character variable into a float variable it still comes out as an integer and i cannot place a decimal. can someone help me figure out how to make this work. thanks.

    here is a small code of mine to show you what i mean.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      float num1;
      float num2;
      float ans;
      char cnum1[20];
      char cnum2[20];
      
      printf(" addition program.\n");
      printf("input number:");
      gets(cnum1);
      printf("inut number:");
      gets(cnum2);
      num1=atoi(cnum1);
      num2=atoi(cnum2);
      ans=num1+num2;
      printf("%f+%f=%f", num1, num2, ans);
      system("PAUSE");	
      return 0;
    }

    &#91;code]&#91;/code]tagged by Salem
    ~matt~

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Use atof if you want floats.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    An integer is not the same as a float in C, use atof.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    atoi = array to integer

    Of course the result is an integer . As was mentioned, use atof instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    thanks ALOT. that was driving me crazy. now i can continue on my C learning adventure!
    ~matt~

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    could be wrong here, but I'm not. atof() is used to go from ASCII to a double, not a float. Like I said, I could be wrong, but I'm almost certain that's the way atof works. Check out your reference books guys.
    don't be a two-bit user

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    atof() converts a string to a double. Isn't double a float just with more precision?
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by damonbrinkley
    atof() converts a string to a double. Isn't double a float just with more precision?
    The point is, a double is larger than a float. Your compiler should warn you about possible loss of data. Example:

    char c;
    int x;

    x = 1000000;
    c = x; /* Data loss. */

    The same holds true for double->float conversion.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  5. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM