Refactor Program that Outputs Binary Values of InputInput reading: two values (separated by whitespace) per lineValidation/error handling of user input valuesProgram that replicates itselfProgram that handles program optionsSort three input values by orderProgram that guesses your number using bitwise operationsBinary File Handling ProgramUnit conversion program into metre and stored inputFind whether two (or 'n') particular values are present in a binary treeProgram to take a number from 0 to 9 as input
Why is "das Weib" grammatically neuter?
Theorems like the Lovász Local Lemma?
Schematic conventions for different supply rails
Making a sword in the stone, in a medieval world without magic
Russian cases: A few examples, I'm really confused
Bash: What does "masking return values" mean?
My story is written in English, but is set in my home country. What language should I use for the dialogue?
Meaning of "SEVERA INDEOVI VAS" from 3rd Century slab
Is a lawful good "antagonist" effective?
Why is a Java array index expression evaluated before checking if the array reference expression is null?
How to explain that I do not want to visit a country due to personal safety concern?
Humanity loses the vast majority of its technology, information, and population in the year 2122. How long does it take to rebuild itself?
How do anti-virus programs start at Windows boot?
I need to drive a 7/16" nut but am unsure how to use the socket I bought for my screwdriver
Be in awe of my brilliance!
How to deal with a cynical class?
Do I need life insurance if I can cover my own funeral costs?
Employee lack of ownership
How to make healing in an exploration game interesting
Where is the 1/8 CR apprentice in Volo's Guide to Monsters?
How to answer questions about my characters?
Font with correct density?
What is IP squat space
Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?
Refactor Program that Outputs Binary Values of Input
Input reading: two values (separated by whitespace) per lineValidation/error handling of user input valuesProgram that replicates itselfProgram that handles program optionsSort three input values by orderProgram that guesses your number using bitwise operationsBinary File Handling ProgramUnit conversion program into metre and stored inputFind whether two (or 'n') particular values are present in a binary treeProgram to take a number from 0 to 9 as input
$begingroup$
I am looking to simplify my main() function. The nested if statements contain four lines of the same code, and I'm unsure of how to refactor and simplify it. I was attempting to use booleans, which seemed like a safe bet, but the logic gets skewed in the process.
#include <bits/stdc++.h>
#include <iostream>
#include <string>
int bits(unsigned long d)
int l;
if (d <= 255)
l = 8;
else if (d > 255 && d <= 65535)
l = 16;
else if (d > 65535 && d <= 4294967295U)
l = 32;
else
std::cout << "32 bits (4294967295) or smaller." << std::endl;
std::cout << "Bits..................... " << l << std::endl;
return l;
std::string convertToBinary(unsigned long decimal)
int l = bits(decimal);
int i, r;
std::string str;
// creates array
for (i = 0; i < l; i++)
r = decimal % 2;
decimal /= 2;
str += std::to_string(r);
// reverses array for binary value
reverse(str.begin(), str.end());
std::cout << "Binary Value............. " << str << std::endl;
return str;
int main(void)
std::string input;
std::cout << "------------ Input -----------" << std::endl;
std::cin >> input;
std::cout << "Input: " << input << std::endl;
std::cout << "Length: " << input.length() << std::endl;
int i, count = 0, j = 0;
int length = input.length();
int ascii;
unsigned long nums;
int numbers[33];
std::string binaries;
std::string chars;
for (i = 0; i < length; i++)
// if next input is digit
if (isdigit(input[i]))
numbers[j] = input[i];
// place digit in next decimal
count = (numbers[j] - '0') + count * 10;
// if next input is char
if (i == length - 1)
if (isdigit(input[length - 1]))
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// next number
j++;
// if next input is char
else
chars[i] = input[i];
ascii = (int) chars[i];
// if next input is digit
if (count != 0)
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// != end of letters
std::cout << "------------ " << chars[i] << " ------------" << std::endl;
std::cout << "ASCII.................... " << ascii << std::endl;
binaries += convertToBinary(ascii) + ' ';
std::cout << "n------- Binary Value of " << input << " -------" << std::endl;
std::cout << binaries << std::endl;
return 0;
The program takes an input, then outputs the corresponding number of bits, binary value, and if necessary, an ASCII value. If the user inputs "c357g98", then the program should get ASCII and binary values of "c", then retrieve the binary value of "357" (rather than "3", "5", then "7" individually), so any adjacent digits will be treated as a single int instead of separate integers. Each time I attempt to refactor, the program loses its ability to store the adjacent digits together as a single int.
In the first attempt at refactoring, I tried this, which takes the count and binary parameters:
void getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
Since the count is never returned, the input "c357g98" is treated as "c357g35798" and the numbers never reset.
So in my second attempt, I return the count variable as an int:
int getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
return c;
And replace the four lines of code with a reference to the function as:
count = getNumbers(count, binaries);
This method converts each value correctly, but does not store the final binary values of the digits, only the characters (since the reference to the string binaries was moved and not returned).
I'm coming to terms with the idea that my entire program may need heavily refactored in order for the logic to be simplified, but I was hoping to start with the messy main() function for now.
c++ c++11
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I am looking to simplify my main() function. The nested if statements contain four lines of the same code, and I'm unsure of how to refactor and simplify it. I was attempting to use booleans, which seemed like a safe bet, but the logic gets skewed in the process.
#include <bits/stdc++.h>
#include <iostream>
#include <string>
int bits(unsigned long d)
int l;
if (d <= 255)
l = 8;
else if (d > 255 && d <= 65535)
l = 16;
else if (d > 65535 && d <= 4294967295U)
l = 32;
else
std::cout << "32 bits (4294967295) or smaller." << std::endl;
std::cout << "Bits..................... " << l << std::endl;
return l;
std::string convertToBinary(unsigned long decimal)
int l = bits(decimal);
int i, r;
std::string str;
// creates array
for (i = 0; i < l; i++)
r = decimal % 2;
decimal /= 2;
str += std::to_string(r);
// reverses array for binary value
reverse(str.begin(), str.end());
std::cout << "Binary Value............. " << str << std::endl;
return str;
int main(void)
std::string input;
std::cout << "------------ Input -----------" << std::endl;
std::cin >> input;
std::cout << "Input: " << input << std::endl;
std::cout << "Length: " << input.length() << std::endl;
int i, count = 0, j = 0;
int length = input.length();
int ascii;
unsigned long nums;
int numbers[33];
std::string binaries;
std::string chars;
for (i = 0; i < length; i++)
// if next input is digit
if (isdigit(input[i]))
numbers[j] = input[i];
// place digit in next decimal
count = (numbers[j] - '0') + count * 10;
// if next input is char
if (i == length - 1)
if (isdigit(input[length - 1]))
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// next number
j++;
// if next input is char
else
chars[i] = input[i];
ascii = (int) chars[i];
// if next input is digit
if (count != 0)
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// != end of letters
std::cout << "------------ " << chars[i] << " ------------" << std::endl;
std::cout << "ASCII.................... " << ascii << std::endl;
binaries += convertToBinary(ascii) + ' ';
std::cout << "n------- Binary Value of " << input << " -------" << std::endl;
std::cout << binaries << std::endl;
return 0;
The program takes an input, then outputs the corresponding number of bits, binary value, and if necessary, an ASCII value. If the user inputs "c357g98", then the program should get ASCII and binary values of "c", then retrieve the binary value of "357" (rather than "3", "5", then "7" individually), so any adjacent digits will be treated as a single int instead of separate integers. Each time I attempt to refactor, the program loses its ability to store the adjacent digits together as a single int.
In the first attempt at refactoring, I tried this, which takes the count and binary parameters:
void getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
Since the count is never returned, the input "c357g98" is treated as "c357g35798" and the numbers never reset.
So in my second attempt, I return the count variable as an int:
int getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
return c;
And replace the four lines of code with a reference to the function as:
count = getNumbers(count, binaries);
This method converts each value correctly, but does not store the final binary values of the digits, only the characters (since the reference to the string binaries was moved and not returned).
I'm coming to terms with the idea that my entire program may need heavily refactored in order for the logic to be simplified, but I was hoping to start with the messy main() function for now.
c++ c++11
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
What have you attempted to do to refactor it? Assuming the four lines you are referring to are the ones beginning withnums = count;, why can't you put those four lines in a function that accepts references tonumsandcount?
$endgroup$
– Null
49 mins ago
$begingroup$
@Null I created a function that takescountandbinaries, then returns thecount. So the four lines are then replaced withcount = getNumbers(count, binaries);This method will skip the digits entirely, and when the program is due to print all final binary values at the end, only the characters are converted. In my second attempt, I changed the function tostring getNumbersand had thebinariesstring be returned instead, and the four lines of code in the nestedifstatements were replaced withbinaries += getNumbers(count, binaries);This method does not reset the counter.
$endgroup$
– J Pex
28 mins ago
$begingroup$
It's hard to follow that in a comment. Please edit your question with the attempted refactor code.
$endgroup$
– Null
17 mins ago
$begingroup$
@Null I apologize, I was unaware of the correct protocol for editing posts. I've edited the post now to include my attempts.
$endgroup$
– J Pex
1 min ago
add a comment |
$begingroup$
I am looking to simplify my main() function. The nested if statements contain four lines of the same code, and I'm unsure of how to refactor and simplify it. I was attempting to use booleans, which seemed like a safe bet, but the logic gets skewed in the process.
#include <bits/stdc++.h>
#include <iostream>
#include <string>
int bits(unsigned long d)
int l;
if (d <= 255)
l = 8;
else if (d > 255 && d <= 65535)
l = 16;
else if (d > 65535 && d <= 4294967295U)
l = 32;
else
std::cout << "32 bits (4294967295) or smaller." << std::endl;
std::cout << "Bits..................... " << l << std::endl;
return l;
std::string convertToBinary(unsigned long decimal)
int l = bits(decimal);
int i, r;
std::string str;
// creates array
for (i = 0; i < l; i++)
r = decimal % 2;
decimal /= 2;
str += std::to_string(r);
// reverses array for binary value
reverse(str.begin(), str.end());
std::cout << "Binary Value............. " << str << std::endl;
return str;
int main(void)
std::string input;
std::cout << "------------ Input -----------" << std::endl;
std::cin >> input;
std::cout << "Input: " << input << std::endl;
std::cout << "Length: " << input.length() << std::endl;
int i, count = 0, j = 0;
int length = input.length();
int ascii;
unsigned long nums;
int numbers[33];
std::string binaries;
std::string chars;
for (i = 0; i < length; i++)
// if next input is digit
if (isdigit(input[i]))
numbers[j] = input[i];
// place digit in next decimal
count = (numbers[j] - '0') + count * 10;
// if next input is char
if (i == length - 1)
if (isdigit(input[length - 1]))
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// next number
j++;
// if next input is char
else
chars[i] = input[i];
ascii = (int) chars[i];
// if next input is digit
if (count != 0)
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// != end of letters
std::cout << "------------ " << chars[i] << " ------------" << std::endl;
std::cout << "ASCII.................... " << ascii << std::endl;
binaries += convertToBinary(ascii) + ' ';
std::cout << "n------- Binary Value of " << input << " -------" << std::endl;
std::cout << binaries << std::endl;
return 0;
The program takes an input, then outputs the corresponding number of bits, binary value, and if necessary, an ASCII value. If the user inputs "c357g98", then the program should get ASCII and binary values of "c", then retrieve the binary value of "357" (rather than "3", "5", then "7" individually), so any adjacent digits will be treated as a single int instead of separate integers. Each time I attempt to refactor, the program loses its ability to store the adjacent digits together as a single int.
In the first attempt at refactoring, I tried this, which takes the count and binary parameters:
void getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
Since the count is never returned, the input "c357g98" is treated as "c357g35798" and the numbers never reset.
So in my second attempt, I return the count variable as an int:
int getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
return c;
And replace the four lines of code with a reference to the function as:
count = getNumbers(count, binaries);
This method converts each value correctly, but does not store the final binary values of the digits, only the characters (since the reference to the string binaries was moved and not returned).
I'm coming to terms with the idea that my entire program may need heavily refactored in order for the logic to be simplified, but I was hoping to start with the messy main() function for now.
c++ c++11
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I am looking to simplify my main() function. The nested if statements contain four lines of the same code, and I'm unsure of how to refactor and simplify it. I was attempting to use booleans, which seemed like a safe bet, but the logic gets skewed in the process.
#include <bits/stdc++.h>
#include <iostream>
#include <string>
int bits(unsigned long d)
int l;
if (d <= 255)
l = 8;
else if (d > 255 && d <= 65535)
l = 16;
else if (d > 65535 && d <= 4294967295U)
l = 32;
else
std::cout << "32 bits (4294967295) or smaller." << std::endl;
std::cout << "Bits..................... " << l << std::endl;
return l;
std::string convertToBinary(unsigned long decimal)
int l = bits(decimal);
int i, r;
std::string str;
// creates array
for (i = 0; i < l; i++)
r = decimal % 2;
decimal /= 2;
str += std::to_string(r);
// reverses array for binary value
reverse(str.begin(), str.end());
std::cout << "Binary Value............. " << str << std::endl;
return str;
int main(void)
std::string input;
std::cout << "------------ Input -----------" << std::endl;
std::cin >> input;
std::cout << "Input: " << input << std::endl;
std::cout << "Length: " << input.length() << std::endl;
int i, count = 0, j = 0;
int length = input.length();
int ascii;
unsigned long nums;
int numbers[33];
std::string binaries;
std::string chars;
for (i = 0; i < length; i++)
// if next input is digit
if (isdigit(input[i]))
numbers[j] = input[i];
// place digit in next decimal
count = (numbers[j] - '0') + count * 10;
// if next input is char
if (i == length - 1)
if (isdigit(input[length - 1]))
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// next number
j++;
// if next input is char
else
chars[i] = input[i];
ascii = (int) chars[i];
// if next input is digit
if (count != 0)
nums = count;
std::cout << "------------ " << nums << " ------------" << std::endl;
binaries += convertToBinary(nums) + ' ';
count = 0;
// != end of letters
std::cout << "------------ " << chars[i] << " ------------" << std::endl;
std::cout << "ASCII.................... " << ascii << std::endl;
binaries += convertToBinary(ascii) + ' ';
std::cout << "n------- Binary Value of " << input << " -------" << std::endl;
std::cout << binaries << std::endl;
return 0;
The program takes an input, then outputs the corresponding number of bits, binary value, and if necessary, an ASCII value. If the user inputs "c357g98", then the program should get ASCII and binary values of "c", then retrieve the binary value of "357" (rather than "3", "5", then "7" individually), so any adjacent digits will be treated as a single int instead of separate integers. Each time I attempt to refactor, the program loses its ability to store the adjacent digits together as a single int.
In the first attempt at refactoring, I tried this, which takes the count and binary parameters:
void getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
Since the count is never returned, the input "c357g98" is treated as "c357g35798" and the numbers never reset.
So in my second attempt, I return the count variable as an int:
int getNumbers(int c, std::string b)
std::cout << "------------ " << c << " ------------" << std::endl;
b += convertToBinary(c) + ' ';
c = 0;
return c;
And replace the four lines of code with a reference to the function as:
count = getNumbers(count, binaries);
This method converts each value correctly, but does not store the final binary values of the digits, only the characters (since the reference to the string binaries was moved and not returned).
I'm coming to terms with the idea that my entire program may need heavily refactored in order for the logic to be simplified, but I was hoping to start with the messy main() function for now.
c++ c++11
c++ c++11
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 mins ago
J Pex
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 56 mins ago
J PexJ Pex
11
11
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
J Pex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
What have you attempted to do to refactor it? Assuming the four lines you are referring to are the ones beginning withnums = count;, why can't you put those four lines in a function that accepts references tonumsandcount?
$endgroup$
– Null
49 mins ago
$begingroup$
@Null I created a function that takescountandbinaries, then returns thecount. So the four lines are then replaced withcount = getNumbers(count, binaries);This method will skip the digits entirely, and when the program is due to print all final binary values at the end, only the characters are converted. In my second attempt, I changed the function tostring getNumbersand had thebinariesstring be returned instead, and the four lines of code in the nestedifstatements were replaced withbinaries += getNumbers(count, binaries);This method does not reset the counter.
$endgroup$
– J Pex
28 mins ago
$begingroup$
It's hard to follow that in a comment. Please edit your question with the attempted refactor code.
$endgroup$
– Null
17 mins ago
$begingroup$
@Null I apologize, I was unaware of the correct protocol for editing posts. I've edited the post now to include my attempts.
$endgroup$
– J Pex
1 min ago
add a comment |
$begingroup$
What have you attempted to do to refactor it? Assuming the four lines you are referring to are the ones beginning withnums = count;, why can't you put those four lines in a function that accepts references tonumsandcount?
$endgroup$
– Null
49 mins ago
$begingroup$
@Null I created a function that takescountandbinaries, then returns thecount. So the four lines are then replaced withcount = getNumbers(count, binaries);This method will skip the digits entirely, and when the program is due to print all final binary values at the end, only the characters are converted. In my second attempt, I changed the function tostring getNumbersand had thebinariesstring be returned instead, and the four lines of code in the nestedifstatements were replaced withbinaries += getNumbers(count, binaries);This method does not reset the counter.
$endgroup$
– J Pex
28 mins ago
$begingroup$
It's hard to follow that in a comment. Please edit your question with the attempted refactor code.
$endgroup$
– Null
17 mins ago
$begingroup$
@Null I apologize, I was unaware of the correct protocol for editing posts. I've edited the post now to include my attempts.
$endgroup$
– J Pex
1 min ago
$begingroup$
What have you attempted to do to refactor it? Assuming the four lines you are referring to are the ones beginning with
nums = count;, why can't you put those four lines in a function that accepts references to nums and count?$endgroup$
– Null
49 mins ago
$begingroup$
What have you attempted to do to refactor it? Assuming the four lines you are referring to are the ones beginning with
nums = count;, why can't you put those four lines in a function that accepts references to nums and count?$endgroup$
– Null
49 mins ago
$begingroup$
@Null I created a function that takes
count and binaries, then returns the count. So the four lines are then replaced with count = getNumbers(count, binaries); This method will skip the digits entirely, and when the program is due to print all final binary values at the end, only the characters are converted. In my second attempt, I changed the function to string getNumbers and had the binaries string be returned instead, and the four lines of code in the nested ifstatements were replaced with binaries += getNumbers(count, binaries); This method does not reset the counter.$endgroup$
– J Pex
28 mins ago
$begingroup$
@Null I created a function that takes
count and binaries, then returns the count. So the four lines are then replaced with count = getNumbers(count, binaries); This method will skip the digits entirely, and when the program is due to print all final binary values at the end, only the characters are converted. In my second attempt, I changed the function to string getNumbers and had the binaries string be returned instead, and the four lines of code in the nested ifstatements were replaced with binaries += getNumbers(count, binaries); This method does not reset the counter.$endgroup$
– J Pex
28 mins ago
$begingroup$
It's hard to follow that in a comment. Please edit your question with the attempted refactor code.
$endgroup$
– Null
17 mins ago
$begingroup$
It's hard to follow that in a comment. Please edit your question with the attempted refactor code.
$endgroup$
– Null
17 mins ago
$begingroup$
@Null I apologize, I was unaware of the correct protocol for editing posts. I've edited the post now to include my attempts.
$endgroup$
– J Pex
1 min ago
$begingroup$
@Null I apologize, I was unaware of the correct protocol for editing posts. I've edited the post now to include my attempts.
$endgroup$
– J Pex
1 min ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
J Pex is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215455%2frefactor-program-that-outputs-binary-values-of-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
J Pex is a new contributor. Be nice, and check out our Code of Conduct.
J Pex is a new contributor. Be nice, and check out our Code of Conduct.
J Pex is a new contributor. Be nice, and check out our Code of Conduct.
J Pex is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215455%2frefactor-program-that-outputs-binary-values-of-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
What have you attempted to do to refactor it? Assuming the four lines you are referring to are the ones beginning with
nums = count;, why can't you put those four lines in a function that accepts references tonumsandcount?$endgroup$
– Null
49 mins ago
$begingroup$
@Null I created a function that takes
countandbinaries, then returns thecount. So the four lines are then replaced withcount = getNumbers(count, binaries);This method will skip the digits entirely, and when the program is due to print all final binary values at the end, only the characters are converted. In my second attempt, I changed the function tostring getNumbersand had thebinariesstring be returned instead, and the four lines of code in the nestedifstatements were replaced withbinaries += getNumbers(count, binaries);This method does not reset the counter.$endgroup$
– J Pex
28 mins ago
$begingroup$
It's hard to follow that in a comment. Please edit your question with the attempted refactor code.
$endgroup$
– Null
17 mins ago
$begingroup$
@Null I apologize, I was unaware of the correct protocol for editing posts. I've edited the post now to include my attempts.
$endgroup$
– J Pex
1 min ago