# Thread: cstring conversion and calculation

1. ## cstring conversion and calculation

Hi all.

I am at the end of my rope. I have spent 3 days trying different ideas and searching this board and the internet for answers.

We have to build a calculator. I take in the data from a file, then store as a cstring. I'm lost from there.

We have to convert from char to a double. Get rid of the spaces. and then depending on what operation is in the string, do a calculation.

ex. 3 5 +

I'm not looking for code from you guys just a general outline of how you would handle the sequence. Do the values get stored in the same array after converting to double? Do I have to create another array or variables to store the data? When converting to a double won't the operater get thrown out?

At this point my head is swimming and I don't know which end is up. I would appreciate the guidence. And I thank you for your time in advance.

2. Hi,

Do the values get stored in the same array after converting to double?
A double cannot be stored in a char array. doubles can only be stored in an array of type double.

1) The first thing you need to do is access the first char in your cstring. How do you do that?

2) Then, how do you determine if it is a number(rather than an operator)? A series of if statements or a switch statement?

3) If it's a certain digit, then assign the corresponding double value to a variable named, say, num1.

4) If it's an operator, perform the operation on the previous num1, num2.

Start with single digits, then later determine how to handle numbers with two or more digits.

One question: since this is a C++ program, why are you reading data into a cstring and not a string?

3. Hi 7 and thanks for the reply.

I will be honest I'm still not sure I understand the difference between a string and a cstring. We are using cstrings because the assignment calls for that.

Can't an array be converted to a double? (this may explain some of my confusion)

I really appreciate your insight into the project. I especially like that the data has to be saved into a variable. That in my mind makes it easier to do what I need on it.

I'll try keeping it simple and then writting some code. I'll post again to see what you think.

Thanks.

4. We are using cstrings because the assignment calls for that.
Ok. With a lot of things you may want to do in a program, strings are just easier to deal with than cstrings, but they do the same things.

Can't an array be converted to a double? (this may explain some of my confusion)
No. Any array has a type, and it holds data of that type. Here is an example of a char array:
Code:
`char myArray[4] = {'a', 'b', '@', '+'};`
Now, how would converting that to a double array work? What double would 'a' be? What double would '@' be?

5. Your right. I can see that I have quite a few gaps in my knowledge of C++.

I keep getting stuck on issues that will come later.

Ok, lets say I figure out how to grap 2 single values from array and assign them to a variable. How would I handle a number like 3.1? I can assign a single value to a variable by reading it but how do you read index 0, 1, 2 and assign all 3 values to a varible?

6. Originally Posted by realjag
How would I handle a number like 3.1?
Treat the '.' as a modifier of the next value you are going to read. What do you have to do to 1, to make it .1?

I can assign a single value to a variable by reading it but how do you read index 0, 1, 2 and assign all 3 values to a varible?

7. Originally Posted by 7stud
What do you have to do to 1, to make it .1?
Yes. We have a page of data to covert then do an operation on.

Here's all of the data.

F
5.1 2.3 +
- 7 2.5 -
3 5 *
4 0 /
4 2 /
8.3 5.1 %
2 3 ^
e
40 S
f
45 s
60 c
e
30 t

an e or f tells us to set the output mode at fixed or scientific. An s,c or t stand for sine, cosine or tangent.

This project has got my stomach in knots. It's going to drive me crazy.

Thanks again for all of the help.

8. It just involves reading each character and determining a course of action. Start simple. Write a program that works on single digit integers. Then make modifications from there.