kamarja
asked on
Urgent!!: Convert a char to a double.
Hello Experts,
I was wondering if there is a built-in function that I could use to convert a 'char' to
a 'double' without loosing the initial values. Yes! It's HomeWork. I just need some pointers. This program takes in a postscript expression and changes it back to infix. I am getting stuck because the data was inputed as 'char' and I need it to be 'double'. I tried 'strtod' but it didn't work.
Newbie Code below .....hehe
-------------------------- ---------- ----
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX = 8;
double array[MAX];
bool isempty(int);
int pop(void);
int push(double x);
bool isfull(int num);
bool isnumber(char num);
int validate(char p1, int, int);
void evaluate(char postfix[]);
int packStack(char getData[], int length);
/************************* ********** ********** ********** ********** ********** ********** ********
** FUNCTION MAIN : ........
************************** ********** ********** ********** ********** ********** ********** ********/
int main()
{
int num = -1;
int tos = -1;
double answer = 0;
char array2[20];
char * p1 = NULL;
p1 = array2;
cout<<"Enter your expression"<<endl;
cin.getline(array2,20,'\n' );
int length = strlen(array2);
for(int j = 0; j < length; j++)
{
array[j] = array2[j];
if(array2[j] == ' ')
continue;
cout<<"HERE: "<<array2[j]<<endl;
}
answer = packStack(array2,length);
cout<<"THE ANSWER: "<<answer<<endl;
return 0;
}
/************************* ********** ********** ********** ********** ********** ********** ********
FUNCTION DECLARATIONS : PUSH, POP, ISEMPTY, ISFULL, TOS, VALIDATE, SP........
************************** ********** ********** ********** ********** ********** ********** ********/
bool isempty(int num)
{
if(num == -1)
return true;
else
return false;
}
bool isfull(int num)
{
int max = 10;
if(num == max - 1)
return true;
else
return false;
}
int pop(void)
{
int num = 5;
if(isempty(num) == 1)
{
cout<<"ERROR: Stack UnderFlow\n";
}
else
{
array[num--];
}
return array[num];
}
int push(double x)
{
int num = 5;
char *p1;
char mim = array[0];
double test;
//test = (strtod(mim,&p1));
if(isfull(num) == 1)
{
cout<<"ERROR: Stack OverFlow\n";
}
else
{
array[num++] = x;
}
return array[num];
}
bool isnumber(char num)
{
return isdigit(num);
}
int validate(char p1, int ops, int nums)
{
char mul = '*';
char div = '/';
char min = '-';
char pows = '$';
char plus = '+';
if(p1 == mul)
return 6;
if(p1 == 'min')
return 5;
if(p1 == div)
return 4;
if(p1 == plus)
return 3;
if(p1 == pows)
return 2;
else
return 1;
return 0;
}
int packStack(char getData[], int length)
{
char *numbers = new char [length];
char *operators = new char[length];
char *infix = new char[length];
char *postfix = new char[length];
int opcount = 0;
int numcount = 0;
for(int j = 0; j < length; j++)
{
postfix[j] = getData[j];
if(isnumber(getData[j])==1 )
{
numbers[j] = getData[j];
numcount++;
}
if(isnumber(getData[j])==0 )
{
operators[j] = getData[j];
opcount++;
cout<<endl;
}
}
char temp;
for(int k = 0; k < length; k++)
{
temp = getData[k];
int ret = 0;
ret = validate(temp,opcount,numc ount);
if (ret == 6)
{
cout<<"Multiply";
double temp2 = 0;
temp2 = pop();
push(pop() * temp2);
}
if(ret == 3)
{
cout<<"ADD"<<endl;
int temp2 = pop();
push(pop() + temp2);
}
}
return 0;
}
I was wondering if there is a built-in function that I could use to convert a 'char' to
a 'double' without loosing the initial values. Yes! It's HomeWork. I just need some pointers. This program takes in a postscript expression and changes it back to infix. I am getting stuck because the data was inputed as 'char' and I need it to be 'double'. I tried 'strtod' but it didn't work.
Newbie Code below .....hehe
--------------------------
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX = 8;
double array[MAX];
bool isempty(int);
int pop(void);
int push(double x);
bool isfull(int num);
bool isnumber(char num);
int validate(char p1, int, int);
void evaluate(char postfix[]);
int packStack(char getData[], int length);
/*************************
** FUNCTION MAIN : ........
**************************
int main()
{
int num = -1;
int tos = -1;
double answer = 0;
char array2[20];
char * p1 = NULL;
p1 = array2;
cout<<"Enter your expression"<<endl;
cin.getline(array2,20,'\n'
int length = strlen(array2);
for(int j = 0; j < length; j++)
{
array[j] = array2[j];
if(array2[j] == ' ')
continue;
cout<<"HERE: "<<array2[j]<<endl;
}
answer = packStack(array2,length);
cout<<"THE ANSWER: "<<answer<<endl;
return 0;
}
/*************************
FUNCTION DECLARATIONS : PUSH, POP, ISEMPTY, ISFULL, TOS, VALIDATE, SP........
**************************
bool isempty(int num)
{
if(num == -1)
return true;
else
return false;
}
bool isfull(int num)
{
int max = 10;
if(num == max - 1)
return true;
else
return false;
}
int pop(void)
{
int num = 5;
if(isempty(num) == 1)
{
cout<<"ERROR: Stack UnderFlow\n";
}
else
{
array[num--];
}
return array[num];
}
int push(double x)
{
int num = 5;
char *p1;
char mim = array[0];
double test;
//test = (strtod(mim,&p1));
if(isfull(num) == 1)
{
cout<<"ERROR: Stack OverFlow\n";
}
else
{
array[num++] = x;
}
return array[num];
}
bool isnumber(char num)
{
return isdigit(num);
}
int validate(char p1, int ops, int nums)
{
char mul = '*';
char div = '/';
char min = '-';
char pows = '$';
char plus = '+';
if(p1 == mul)
return 6;
if(p1 == 'min')
return 5;
if(p1 == div)
return 4;
if(p1 == plus)
return 3;
if(p1 == pows)
return 2;
else
return 1;
return 0;
}
int packStack(char getData[], int length)
{
char *numbers = new char [length];
char *operators = new char[length];
char *infix = new char[length];
char *postfix = new char[length];
int opcount = 0;
int numcount = 0;
for(int j = 0; j < length; j++)
{
postfix[j] = getData[j];
if(isnumber(getData[j])==1
{
numbers[j] = getData[j];
numcount++;
}
if(isnumber(getData[j])==0
{
operators[j] = getData[j];
opcount++;
cout<<endl;
}
}
char temp;
for(int k = 0; k < length; k++)
{
temp = getData[k];
int ret = 0;
ret = validate(temp,opcount,numc
if (ret == 6)
{
cout<<"Multiply";
double temp2 = 0;
temp2 = pop();
push(pop() * temp2);
}
if(ret == 3)
{
cout<<"ADD"<<endl;
int temp2 = pop();
push(pop() + temp2);
}
}
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char str[] = "3.141592";
double result;
result = atof(str);
In case str is not numeric function returns 0 (not error trhown).
double result;
result = atof(str);
In case str is not numeric function returns 0 (not error trhown).
ASKER
OK...I got rid of the errors. Now I am getting an error that says: The instruction at "0x004224a1" reference memory at "0x000000020".
The memory could not be "read". Any idea what's going on here.
this is what I have now:
========================== ====
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int num = 0;
const int MAX = 20;
double array[MAX];
char array2[MAX];
bool isempty(int);
int pop(void);
int push(double x);
bool isfull(int num);
bool isnumber(char num);
int validate(char p1);
void evaluate(char postfix[]);
int packStack(char getData[], int length);
/************************* ********** ********** ********** ********** ********** ********** ********
** FUNCTION MAIN : ........
************************** ********** ********** ********** ********** ********** ********** ********/
int main()
{
double answer = 0;
char inputstring[MAX];
int ret = 0;
//tempstring = (char *) malloc(sizeof(char)*20);
cout<<"Enter your expression"<<endl;
cin.getline(inputstring,20 ,'\n');
int length = strlen(inputstring) - 1;
cout<<"Size of Length :"<<length<<endl;
char * convert;
for(int j = 0; j < length; j++)
{
array2[j] = inputstring[j];
if(inputstring[j] == ' ') // delete all white space for nice format for output
// in infix format.
continue;
}
char * temp;
for(int h = 0; h < length; h++)
{
if(isnumber(array2[h])==0)
{
temp = (char *) inputstring[h];
double x = strtod(temp,& convert);
cout<<"Conerverion :"<<x<<endl;
push(x);
}
else
{
char z = inputstring[h];
int ret = (validate(z));
double a = 0;
double b = 0;
double c = 0;
b = pop();
a = pop();
if(ret == 6)
{
b = pop();
a = pop();
c = a * b;
}
if(ret == 5)
{
b = pop();
a = pop();
c = a - b;
}
if(ret == 4)
{
b = pop();
a = pop();
c = a / b;
}
if(ret == 3)
{
b = pop();
a = pop();
c = a + b;
}
if(ret == 2)
{
b = pop();
a = pop();
c = pow(a,b);
}
if(ret == 1)
{
cout<<"ERROR: Invalid expression:"<<endl;
}
}
}
//cout<<"THE ANSWER: "<<pop()<<endl;
return 0;
}
/************************* ********** ********** ********** ********** ********** ********** ********
FUNCTION DECLARATIONS : PUSH, POP, ISEMPTY, ISFULL, TOS, VALIDATE, SP........
************************** ********** ********** ********** ********** ********** ********** ********/
bool isempty(int num)
{
if(num == -1)
return true;
else
return false;
}
bool isfull(int num)
{
int max = 10;
if(num == max - 1)
return true;
else
return false;
}
int pop(void)
{
int num = 5;
if(isempty(num) == 1)
{
cout<<"ERROR: Stack UnderFlow\n";
}
else
{
array[num--];
}
return array[num];
}
int push(double x)
{
if(isfull(num) == 1)
{
cout<<"ERROR: Stack OverFlow\n";
}
else
{
array[num++] = x;
}
return array[num];
}
bool isnumber(char num)
{
return isdigit(num);
}
int validate(char p1)
{
char mul = '*';
char div = '/';
char min = '-';
char pows = '$';
char plus = '+';
if(p1 == mul)
return 6;
if(p1 == 'min')
return 5;
if(p1 == div)
return 4;
if(p1 == plus)
return 3;
if(p1 == pows)
return 2;
else
return 1;
return 0;
}
int packStack(char getData[], int length)
{
char *numbers = new char [length];
char *operators = new char[length];
char *infix = new char[length];
char *postfix = new char[length];
int opcount = 0;
int numcount = 0;
for(int j = 0; j < length; j++)
{
postfix[j] = getData[j];
if(isnumber(getData[j])==1 )
{
numbers[j] = getData[j];
numcount++;
}
if(isnumber(getData[j])==0 )
{
operators[j] = getData[j];
opcount++;
cout<<endl;
}
}
delete [] infix;
delete [] postfix;
delete [] operators;
delete [] numbers;
return 0;
}
The memory could not be "read". Any idea what's going on here.
this is what I have now:
==========================
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int num = 0;
const int MAX = 20;
double array[MAX];
char array2[MAX];
bool isempty(int);
int pop(void);
int push(double x);
bool isfull(int num);
bool isnumber(char num);
int validate(char p1);
void evaluate(char postfix[]);
int packStack(char getData[], int length);
/*************************
** FUNCTION MAIN : ........
**************************
int main()
{
double answer = 0;
char inputstring[MAX];
int ret = 0;
//tempstring = (char *) malloc(sizeof(char)*20);
cout<<"Enter your expression"<<endl;
cin.getline(inputstring,20
int length = strlen(inputstring) - 1;
cout<<"Size of Length :"<<length<<endl;
char * convert;
for(int j = 0; j < length; j++)
{
array2[j] = inputstring[j];
if(inputstring[j] == ' ') // delete all white space for nice format for output
// in infix format.
continue;
}
char * temp;
for(int h = 0; h < length; h++)
{
if(isnumber(array2[h])==0)
{
temp = (char *) inputstring[h];
double x = strtod(temp,& convert);
cout<<"Conerverion :"<<x<<endl;
push(x);
}
else
{
char z = inputstring[h];
int ret = (validate(z));
double a = 0;
double b = 0;
double c = 0;
b = pop();
a = pop();
if(ret == 6)
{
b = pop();
a = pop();
c = a * b;
}
if(ret == 5)
{
b = pop();
a = pop();
c = a - b;
}
if(ret == 4)
{
b = pop();
a = pop();
c = a / b;
}
if(ret == 3)
{
b = pop();
a = pop();
c = a + b;
}
if(ret == 2)
{
b = pop();
a = pop();
c = pow(a,b);
}
if(ret == 1)
{
cout<<"ERROR: Invalid expression:"<<endl;
}
}
}
//cout<<"THE ANSWER: "<<pop()<<endl;
return 0;
}
/*************************
FUNCTION DECLARATIONS : PUSH, POP, ISEMPTY, ISFULL, TOS, VALIDATE, SP........
**************************
bool isempty(int num)
{
if(num == -1)
return true;
else
return false;
}
bool isfull(int num)
{
int max = 10;
if(num == max - 1)
return true;
else
return false;
}
int pop(void)
{
int num = 5;
if(isempty(num) == 1)
{
cout<<"ERROR: Stack UnderFlow\n";
}
else
{
array[num--];
}
return array[num];
}
int push(double x)
{
if(isfull(num) == 1)
{
cout<<"ERROR: Stack OverFlow\n";
}
else
{
array[num++] = x;
}
return array[num];
}
bool isnumber(char num)
{
return isdigit(num);
}
int validate(char p1)
{
char mul = '*';
char div = '/';
char min = '-';
char pows = '$';
char plus = '+';
if(p1 == mul)
return 6;
if(p1 == 'min')
return 5;
if(p1 == div)
return 4;
if(p1 == plus)
return 3;
if(p1 == pows)
return 2;
else
return 1;
return 0;
}
int packStack(char getData[], int length)
{
char *numbers = new char [length];
char *operators = new char[length];
char *infix = new char[length];
char *postfix = new char[length];
int opcount = 0;
int numcount = 0;
for(int j = 0; j < length; j++)
{
postfix[j] = getData[j];
if(isnumber(getData[j])==1
{
numbers[j] = getData[j];
numcount++;
}
if(isnumber(getData[j])==0
{
operators[j] = getData[j];
opcount++;
cout<<endl;
}
}
delete [] infix;
delete [] postfix;
delete [] operators;
delete [] numbers;
return 0;
}
ASKER
Salte and Exceter,
My original problem was resolved. I spit the points between both of you, 125 each. I am still having issues per my question above but thanks for helping me out with the conversion. Many thanks to all the other Experts as well.
As to the problem I am have now, it only occurs during runtime, right after I enter the expression. Any ideas?
My original problem was resolved. I spit the points between both of you, 125 each. I am still having issues per my question above but thanks for helping me out with the conversion. Many thanks to all the other Experts as well.
As to the problem I am have now, it only occurs during runtime, right after I enter the expression. Any ideas?
char * temp;
for(int h = 0; h < length; h++)
{
if(isnumber(array2[h])==0)
{
temp = (char *) inputstring[h];
double x = strtod(temp,& convert);
cout<<"Conerverion :"<<x<<endl;
push(x);
}
That section is your problem, or at least the beginning of it. Without lookinig too deeply, isnumber is accepting the decimal as a number, which strtod can't handle, if you look at the following link, you will see that you have to have digits either before or after the decimal, but here you have neither. I'll leave you to suss out the remaining issues (if they exist).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_strtod.2c_.wcstod.asp
for(int h = 0; h < length; h++)
{
if(isnumber(array2[h])==0)
{
temp = (char *) inputstring[h];
double x = strtod(temp,& convert);
cout<<"Conerverion :"<<x<<endl;
push(x);
}
That section is your problem, or at least the beginning of it. Without lookinig too deeply, isnumber is accepting the decimal as a number, which strtod can't handle, if you look at the following link, you will see that you have to have digits either before or after the decimal, but here you have neither. I'll leave you to suss out the remaining issues (if they exist).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_strtod.2c_.wcstod.asp
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Split points between Salte & Exceter
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
Split points between Salte & Exceter
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
ASKER
Hi Tinchos,
I don't see Salte's name here, only Exceter's. How do I give him the points?
Ian
I don't see Salte's name here, only Exceter's. How do I give him the points?
Ian
As in your post
>>Salte and Exceter,
>>My original problem was resolved. I spit the points between both of you, 125 each. I am still having issues per my >>question above but thanks for helping me out with the conversion. Many thanks to all the other Experts as well.
>>Salte and Exceter,
>>My original problem was resolved. I spit the points between both of you, 125 each. I am still having issues per my >>question above but thanks for helping me out with the conversion. Many thanks to all the other Experts as well.
ASKER
Tinchos,
You obviously misunderstood my question. What I meant was that I allotted 125 points for Exceter but I didn't see Salte's name. So I was unable to give him the "well deserved" points. "I am not saying that Salte didn't help!" Please give me the link where I can see both "Salte's and Exceter's" comments so that I can split the points between both of them, because I can not find the original post for the question "Since you guy's gave the website a facelift ;-)." Got it?
Thanks
Ian
You obviously misunderstood my question. What I meant was that I allotted 125 points for Exceter but I didn't see Salte's name. So I was unable to give him the "well deserved" points. "I am not saying that Salte didn't help!" Please give me the link where I can see both "Salte's and Exceter's" comments so that I can split the points between both of them, because I can not find the original post for the question "Since you guy's gave the website a facelift ;-)." Got it?
Thanks
Ian
If you want to give salte some points
give exceter the points in these question
and create a new one with the points you want to award him
whose title is : Points for Salte
and in the body put a reference to this question
something like
Points for Salte because of
https://www.experts-exchange.com/questions/20635286/Urgent-Convert-a-char-to-a-double.html
give exceter the points in these question
and create a new one with the points you want to award him
whose title is : Points for Salte
and in the body put a reference to this question
something like
Points for Salte because of
https://www.experts-exchange.com/questions/20635286/Urgent-Convert-a-char-to-a-double.html
Hey kamarja
Just to make it clear.
You assigned to exceter the points of this question
you posted a question so that assign salte some points
and you posted a question so that you assign some more points to exceter.
Just to let you know that you're awarding points 2 times 2 exceter. Not sure if that's what you meant.
Cheers
Tincho
Just to make it clear.
You assigned to exceter the points of this question
you posted a question so that assign salte some points
and you posted a question so that you assign some more points to exceter.
Just to let you know that you're awarding points 2 times 2 exceter. Not sure if that's what you meant.
Cheers
Tincho
>> Just to let you know that you're awarding points 2 times 2 exceter. Not sure if that's what you meant.
I won't accept more points for help I havn't provided.
I won't accept more points for help I havn't provided.
ASKER
OOPs. I gave Exceter the points but when I checked my points, it seemed as if it didn't go through. Can you
check it out for me because that's not what I meant to do :-). I had 930 points and I gave them 175 each.
Now I have 580 points left. It seems correct.
Cheers!
Ian
check it out for me because that's not what I meant to do :-). I had 930 points and I gave them 175 each.
Now I have 580 points left. It seems correct.
Cheers!
Ian
ASKER
Sorry for the confusion.
Ian
Ian
>> I gave Exceter the points but when I checked my points, it seemed as if it didn't go through.
That's because the points are taken from your account when you post the question, not when you accept an answer.
That's because the points are taken from your account when you post the question, not when you accept an answer.
syntax is same as scanf, so it's easy to use.
Here is an example:
char str[] = "3.141592";
double result;
sscanf(str,"%lf,&result); //It's will convert the string to double same way scanf does.
Remeber you can also use sprint, to convert double back to string (or any type that scanf and printf supports);