cstring conversion and calculation

This is a discussion on cstring conversion and calculation within the C++ Programming forums, part of the General Programming Boards category; Hi all. I am at the end of my rope. I have spent 3 days trying different ideas and searching ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    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. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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?
    Last edited by 7stud; 10-28-2005 at 03:42 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    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. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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?
    Last edited by 7stud; 10-28-2005 at 04:14 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    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. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote 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?
    Using addition.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Quote 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. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. Round conversion to 2 decimal places
    By Cero.Uno in forum C Programming
    Replies: 4
    Last Post: 02-17-2008, 02:39 AM
  4. Newbie Again
    By christianne in forum C Programming
    Replies: 14
    Last Post: 04-06-2006, 01:39 AM
  5. source of calculation error?
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 08-12-2004, 06:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21