Hey guys, this is my solution to a simple problem and I was looking for some feedback on my code. Please post any criticism you may have about it.
//Test project for string to binary conversion//
#include <iostream>
#include <string>
using namespace std;
void binConvert(string ConvertMe){
int ArraySize = ConvertMe.length();
int* asciiArray = new int [ArraySize];
int binValues[8];
int decValues[8] = {128,64,32,16,8,4,2,1};
// Convert each char of ConvertMe into its ascii value
for(int i = 0; i < ArraySize; i++){
asciiArray[i] = ConvertMe[i];
}
// Convert asciiArray values from decimal to binary and output to screen
for(int i = 0; i < ArraySize; i++){
// convert each ascii value into its binary 8 bit equivalent
for (int a = 0; a < 8; a++){
if (asciiArray[i] >= decValues[a]){
binValues[a] = 1;
}
else if (asciiArray[i] < decValues[a]){
binValues[a] = 0;
}
if (binValues[a] == 1){
asciiArray[i] = asciiArray[i] - decValues[a];
}
}
for (int a = 0; a < 8; a++)
cout << binValues[a];
}
}
int main(){
string ConvertMe;
cout << "What do you want to convert: ";
getline(cin, ConvertMe);
binConvert(ConvertMe);
}
I do have a question about my use of for loops in lines 20-36. Is it an accepted practice for me to nest the two for loops inside the first, or is something like that generally frowned upon? If it's frowned upon, what's another way I could accomplish the same thing?
Also, I didn't see anything about it in the rules, would it be okay for me to post my projects like this regularly as I get better? Writing code and receiving criticism(good or bad) on it seems to help me learn faster.