• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer Spacing Assignment.  RSS feed

 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on an assignment where I am to prompt a user to enter an integer (4567), output the integer as individual digits (4 5 6 7), and then find the sum of the digits (22). I've been able to tackle the first part. However, even if I enter a negative integer (-4567), it should still output positive digits (4 5 6 7). With the way I have it coded, if I enter the negative integer, I end up with (-4 -5 -6 -7). Cana somebody help me with this? I'm doing this one step at a time, so I have not attempted the find the sum yet.

 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard this post. I had an issue formatting the code on the site. I figured it out. I would have deleted this post, but don't see a "delete" option.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ellsworth wrote:Disregard this post. I had an issue formatting the code on the site. I figured it out. I would have deleted this post, but don't see a "delete" option.



First, thank you for telling us that it has been solved. And second, thank you for not deleting the topic. It is annoying for people to answer questions that has already been solved. And it is really annoying for people to answer questions that later gets deleted.

Telling us that the issue has been resolved, but not deleting the topic, was a good option here... telling us the solution (on what you figured out) would've probably been a better one.

Henry
 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Just to clarify, I have not solved the initial problem. The issue that I did solve was getting the code to appear correctly in my post. I had post a reply immediately follow my original post to apologize for the incorrectly formatted original post. When I corrected it, I couldn't delete the apology, so I just edited it to say disregard. The original question is still open. Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ellsworth wrote:
Just to clarify, I have not solved the initial problem. The issue that I did solve was getting the code to appear correctly in my post. I had post a reply immediately follow my original post to apologize for the incorrectly formatted original post. When I corrected it, I couldn't delete the apology, so I just edited it to say disregard. The original question is still open. Thanks.


Oh, sorry, I read it as "never mind" with the topic -- as it has been solved.


Anyway, your code isn't very efficient. It does some counter that works for positive numbers. Then it does some counter stuff in case the number is negative. Then it does the digit stuff for when the number is positive... then... I guess that you are missing the digit stuff for when the number is negative...

Seriously, I can't recommend that you do that. One option here is to make sure that the number is positive -- save the sign somewhere. And do the whole thing with positive digits. Another option is to convert the number to a string, to extract the character digits.

Henry
 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have loved to not even involve negative numbers since, being a beginner, I'd like to keep it simple. However, the instructions included an example that was negative.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ellsworth wrote:I would have loved to not even involve negative numbers since, being a beginner, I'd like to keep it simple. However, the instructions included an example that was negative.


I didn't say don't take negative input. I said take all input, and if it is negative, set a flag somewhere, and convert it to positive for processing. This way, your code only have to process positive numbers for the digits portion, making your processing simpler.

Henry
 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Ellsworth wrote:I would have loved to not even involve negative numbers since, being a beginner, I'd like to keep it simple. However, the instructions included an example that was negative.


I didn't say don't take negative input. I said take all input, and if it is negative, set a flag somewhere, and convert it to positive for processing. This way, your code only have to process positive numbers for the digits portion, making your processing simpler.

Henry


That is exactly what I want to do, but I don't know how.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ellsworth wrote:
That is exactly what I want to do, but I don't know how.


Have you learned "if" conditions yet?

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Ellsworth wrote:I'm working on an assignment...

John,

Please DontWriteLongLines. It makes your thread very hard to read. I've broken yours up this time, but for future reference, please remember:
80 characters max.

Thanks.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!