I am asking you to accidentally convert the C language char array

Asked 2 years ago, Updated 2 years ago, 29 views

For example, when I have 123.123123 as a char array, I try to convert it by mistake. If you know the method or function, please let me know! Thank you.

c

2022-09-22 21:51

1 Answers

Refer to the following two functions.

Note - stod(...)

// stod example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("365.24 29.53");
  std::string::size_type sz;     // alias of size_t

  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz));
  std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
  return 0;

Note - atof(...)

/* atof example: sine calculator */
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atof */
#include <math.h>       /* sin */

int main ()
{
  double n,m;
  double pi=3.1415926535;
  char buffer[256];
  printf ("Enter degrees: ");
  fgets (buffer,256,stdin);
  n = atof (buffer);
  m = sin (n*pi/180);
  printf ("The sine of %f degrees is %f\n" , n, m);
  return 0;
}


2022-09-22 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.