Hello all. I'm having problems figuring out how to get atof working in my program. I'm not sure exactly what I'm missing, and I was hoping someone could point me in the right direction. Thanks.

#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

static int top = -1;
static double stack[50];

void push(double d)
 {
  top++;
  stack[top]=d;
 }

double pop()
 {
  double p;
  p = stack[top];
  top--;
  return p;
 }

int main(int argc, char *argv[])
 {
  int i;
  double a, b, r;
  static double stack[50];
  static int top = -1;
  for(i=1; i<argc; i++)
   {
    if(argv[i][0]=='+')
     {
      b = pop();
      a = pop();
      r = a + b;
      push(r);
     }
    else if(argv[i][0]=='-')
     {
      b = pop();
      a = pop();
      r = a - b;
      push(r);
     }
    else if(argv[i][0]=='^')
     {
      b = pop();
      a = pop();
      r = pow(a,b);
     }
    else if(argv[i][0]=='*')
     {
      b = pop();
      a = pop();
      r = a * b;
     }
    else if(argv[i][0]=='/')
     {
      b = pop();
      a = pop();
      r = a / b;
     }
    else if(atof(argv[i][0]))
     {
      double num;
      num = atof(argv[i][0]);
      push(num);
     }
   }
  printf("%f\n",r);    
  return 0;
 }

Recommended Answers

All 2 Replies

atof() converts a string to a double, except in your case you are giving it not a string, but a single character.

How would I make it read the string input? The user input to the program is going to be something like: 2.0 3.0 +, which would result in 2.0+3.0=5.0, 5.0 being output as the final answer. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.