Thread: Conversion Type Question ?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Conversion Type Question ?

    I'm trying to run my program and have it convert the values in the command line to doubles.

    Test Run:

    ./prog fname -v 1.0 2.0 3.0 4.0

    But what I get is either extremely large values or the values after the decimal are truncated.

    I've tried atol(),strtol() and even strolf()

    Any suggestions would help.


    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
            const char *filename;
            FILE *file;
            double val0,val1,val2,val3;
    
      if(argv[2][0] == '-' && argv[2][1] == 'v' && argc == 7)
      {
            filename = argv[1];
            file = fopen(filename, "r");
            val0 = atof(argv[3],sizeof(argv[3]));
            val1 = atof(argv[4],sizeof(argv[4]));
            val2 = atof(argv[5],sizeof(argv[5]));
            val3 = atof(argv[6],sizeof(argv[6]));
    
            printf(">>>>>>>>>>>\n");
            printf("%f %f %f %f\n",val0,val1,val2,val3);
            printf(">>>>>>>>>>>\n");
      }
    
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    1. Use the correct header file

    Code:
    #include <stdlib.h>
    2. atof() has one argument, not two

    Code:
    val0 = atof(argv[3]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM