Jump to content

C++ program

_kR15n

Multiply the original number by ten and simply add the new number.

So in this case you have 2, multiple it by 10 to get 20 and then add the input (1) to get 21.

Now the user fills in the number 3. Multiply the 21 by ten to get 210, add the 3 to get 213.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Minibois said:

Multiply the original number by ten and simply add the new number.

So in this case you have 2, multiple it by 10 to get 20 and then add the input (1) to get 21.

Now the user fills in the number 3. Multiply the 21 by ten to get 210, add the 3 to get 213.

and if the input is two digits then i multiply by 100 and add the input right?

Link to comment
Share on other sites

Link to post
Share on other sites

No, you still multiply it by 10.

 

You have 25. You want to add 3 to the end ... 25 x 10 + 3 = 253

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, _kR15n said:

and if the input is two digits then i multiply by 100 and add the input right?

Yep. You could even make that sort of an 'if statement' thing.

Power it by the base number by (10 ^ digits of user input).

 

So if the user inputs 124, that's 3 digits, 10³ = 1000

 

(I may not have called the power of thing correctly, math is not really my thing)

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, if you add more than 1 number at the end , then you'll have to add one number at a time, or multiply the original number by 10/100/1000 and so on, as many zeros as the number of digits.

 

You can determine the number of digits by repeatedly dividing the number by 10 and checking if rest is 0

 

// append 15 to 1234  => 123415

int basenumber = 1234;

int number = 15;

 

int counter = 1;

int temporary = 0;

temporary = number;

while (temporary >10) {

  counter = counter + 1;

  temporary = temporary / 10;

}

while (counter > 0) {

 basenumber = basenumber * 10;

 counter = counter - 1;

}

basenumber = basenumber + number;

 

... something like this ... i'm writing it directly in the forum here, so it may have errors.

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, Minibois said:

Yep. You could even make that sort of an 'if statement' thing.

Power it by the base number by (10 ^ digits of user input).

 

So if the user inputs 124, that's 3 digits, 10³ = 1000

 

(I may not have called the power of thing correctly, math is not really my thing)

 

Could you explain why this error appears?

 

Screenshot_20190616-020219.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

C++ allows you to type numbers in your source code text using various bases, not just decimal.

 

If the number has a 0 at the beginning, it's assumed you're typing it in Octal, base 8 , so you can only type numbers between 0 and 7 after the first 0 ... ex  040  is translated into 32, because the value 32 is 40 in octal, base 8.

 

You can also type number in base 16, hexadecimal, by placing "0x" before the number  ... ex  0x64  is 100 in decimal.

 

Some compilers also allow binary, where you can type a byte value (0..255 or -127..127) by saying which bits are on or of, placing "0b" before the sequence of bits ... ex  0b10101010 is 170 in decimal.

 

 

In your particular code, simple solution would be to change *100  to *10  and just say +[number]

 

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, mariushm said:

C++ allows you to type numbers in your source code text using various bases, not just decimal.

 

If the number has a 0 at the beginning, it's assumed you're typing it in Octal, base 8 , so you can only type numbers between 0 and 7 after the first 0 ... ex  040  is translated into 32, because the value 32 is 40 in octal, base 8.

 

You can also type number in base 16, hexadecimal, by placing "0x" before the number  ... ex  0x64  is 100 in decimal.

 

Some compilers also allow binary, where you can type a byte value (0..255 or -127..127) by saying which bits are on or of, placing "0b" before the sequence of bits ... ex  0b10101010 is 170 in decimal.

 

 

In your particular code, simple solution would be to change *100  to *10  and just say +[number]

 

i can not do that because i have more than 10 cases

for which i need more than 10 digits

Link to comment
Share on other sites

Link to post
Share on other sites

Yes you can, you just have to not start a number with 0 when writing it in your editor .... figure out how to rewrite that so you won't have 01 , 02 and so on...

if you don't HAVE to use case, don't use it ... use if  for example ... if (letter == 'c') {  instructions }   - for your particular problem it would be better as you don't have to keep tying break all the time, it's more condensed and easier to follow with your eyes.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I see a lot of wrong here, both in the OP's code and in many of the replies, and OP has not fully clarified their requirements, either. For example, if the pre-existing number was 99, what should happen if user inputs 001 -- should it become 99001 or 991? If the former, none of the suggestions here work!

 

As for those case-clauses there, instead of a billion clauses like that, instead just do:

check that InputCharacter is actually a letter and lowercase (see strcspn() and tolower())

d = d * 100 + (inputCharacter - 'a' + 1)

 

Why? Well, 'a' - 'a' would be zero, so it'd become d = d * 100 + (0 + 1) or, in other words, d = d * 100 + 1, just like in OP's code. If InputCharacter was 'b', doing 'b' - 'a' would result in a 1, so again, this exact same statement would become d = d * 100 + ('b' - 'a' + 1), or d = d * 100 + (1 + 1), or, again, d = d * 100 + 2. As you see, you can replace ALL of those case - clauses with one, single line of code and completely remove the whole switch() - statement -- a whole lot more efficient and readable.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, Dat Guy said:

About formatting numbers with leading zeroes:

There's a stream modifier for that.

 

https://en.cppreference.com/w/cpp/io/manip/setfill

That's for output, not input, so I don't see how that's relevant.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×