home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1686
Discussions: 188,402, Posts: 2,243,609, Members: 232,632
Old April 28th, 2005, 09:47 PM   Digg it!   #1 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
c++ strings

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.
__________________

lost-and-found is offline   Reply With Quote
Old April 28th, 2005, 09:59 PM     #2 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
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

originel is offline   Reply With Quote
Old April 28th, 2005, 10:03 PM     #3 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
input would be something like: "20 3 42"

lost-and-found is offline   Reply With Quote
Old April 28th, 2005, 10:05 PM     #4 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
ok cool...that's what i was assuming in my explanation
originel is offline   Reply With Quote
Old April 29th, 2005, 01:06 AM     #5 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,959
Send a message via AIM to lost-and-found
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 .
lost-and-found is offline   Reply With Quote
Old April 29th, 2005, 07:57 AM     #6 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
Hehe, you say time for "a" girlfriend...not "the" or "my"...hmm...
originel is offline   Reply With Quote
Old 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
jessen is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter out Non-Binary Strings tgxiii Webmastering and Programming 0 October 10th, 2004 05:55 PM
php, partial strings cracked Webmastering and Programming 1 February 14th, 2004 08:04 PM
how to use cursors with strings srinivas_mca2k Webmastering and Programming 12 September 16th, 2003 02:21 AM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Misery Loves Company... (2144)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Futronix has water features? (0)
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:35 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28