April 28th, 2005, 09:47 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
hey guys, I have an assignment for c++ lab.
I have the user input numbers into one string, and my assignment is to add those numbers and print out the result.
For the life of me I cannot seem to get it, any pointers would be greatly appreciated. |
| |
April 28th, 2005, 09:59 PM
|
#2 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
are they just single digit numbers or can they have more than 1 digit?
i assume that the numbers are delimited (i.e. a space, comma, or something separates them), correct?
could you give an example input?
My thoughts (assuming multiple digit delimited) would be to basically go through character by character starting at the end and working forward because powers increase from right to left, not left to right. Basically this would be in one for loop. You would check to see if it was a number and if it was a number you would add it to a "running number" which keeps track of the current number through all of its digits. To add it you would first multiply by the power (i.e. if it's in the tens place multiply by 10, etc). It's easy to figure out what power it is because we are working right to left and you just have to keep a powers variable and multiply it by 10 with each loop and reset it to 1 everytime you find a delimiter. You can also add the finished number to the total sum whenever you encounter a delimiter.
That's how i would do it anyways.
EDIT: oh yeah, you would also have to convert from ascii to binary by subtracting 60 from the number |
| |
April 28th, 2005, 10:03 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
input would be something like: "20 3 42" |
| |
April 28th, 2005, 10:05 PM
|
#4 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
ok cool...that's what i was assuming in my explanation |
| |
April 29th, 2005, 01:06 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Illinois
Posts: 2,959
|
I DID IT!!! I wrote my RPN (reverse polish notation) calculator...took me a while to figure out what to do. Thanks for teh help originel. I followed your instructions and used a stack to store the seperate digits from each number  . Now, after 8 hours time for a girlfriend  . |
| |
April 29th, 2005, 07:57 AM
|
#6 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
Hehe, you say time for "a" girlfriend...not "the" or "my"...hmm...  |
| |
April 29th, 2005, 09:05 AM
|
#7 (permalink)
| | Junior Member
Join Date: Apr 2005
Posts: 5
|
Just declare two or three variables and then use the function for e.g,
cin>> to assign the user input value to them ... Then add them as follows as z=a+b+c; an then display the sum Z as using the function cout>> .. Contact me for any doubt |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |