C: How to read char data type as double type?

  • zevan4grk
  • Born
  • Born
  • zevan4grk
  • Posts: 1

Post 3+ Months Ago

Code: [ Select ]
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define MAX_VECTORS 4
#define SIZE 100


int main( void )
{

   double **vectors = NULL;
   int *lengths = NULL;
   vectors = (double **) malloc (MAX_VECTORS * sizeof(double *));
   lengths = (int *) malloc (MAX_VECTORS * sizeof( int ));
   char input[SIZE];
   int precision = 2; //defualt precision.
   char spec;
   double *elements = NULL;
   elements = (double *) malloc (MAX_VECTORS * sizeof(double *));
   int length;

  
   while( strcmp(input, "bye") != 0){ // Repeats calc until the user enters bye.

   printf("calc>\n");
   scanf("\n");
   gets(input);
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #define MAX_VECTORS 4
  6. #define SIZE 100
  7. int main( void )
  8. {
  9.    double **vectors = NULL;
  10.    int *lengths = NULL;
  11.    vectors = (double **) malloc (MAX_VECTORS * sizeof(double *));
  12.    lengths = (int *) malloc (MAX_VECTORS * sizeof( int ));
  13.    char input[SIZE];
  14.    int precision = 2; //defualt precision.
  15.    char spec;
  16.    double *elements = NULL;
  17.    elements = (double *) malloc (MAX_VECTORS * sizeof(double *));
  18.    int length;
  19.   
  20.    while( strcmp(input, "bye") != 0){ // Repeats calc until the user enters bye.
  21.    printf("calc>\n");
  22.    scanf("\n");
  23.    gets(input);


Say the input is A 3 5 4 2, I want to read A to be a specifier, 3 to be the length of an array, as int, and then the elements of the array as double data type(3 elements).
I was able to get the length and the specifier by using:

Code: [ Select ]
 sscanf(input,"%c\n%d\n", &spec, &length); 


how can I get the elements as double data type?

Thank you.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • alias
  • Born
  • Born
  • alias
  • Posts: 1

Post 3+ Months Ago

you may try the "atof" function.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 92 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.