@Ketsuekiame: Thank you! I went through and changed the names so they all fit one style, and I also made the other changes you recommended.

@tinstaafl: Hmm, I didn't even think about doing it that way. As far as I can tell `output += ToBinary((int)input[i]);` gets rid of my need for the ASCII conversion for loop while concatenating each 8 bit output into a single output, and `(int)input[i]` is what leverages the implicit cast, right? Thank you!

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;
int decValues = {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 ...

Oh wow, thank you! Haha, the bad part is that I knew it just hid it, but I just couldn't seem to find the real problem. I didn't even think about [s + 1] going out of bounds. Also, thanks for the efficiency improvement, I didn't think about that either.

Steadily working my way up to more complicated projects really gets me excited about what's to come after I have a better understanding of the language.

Hey guys, I was looking over my code and decided to try something to fix it and it worked. I changed `int* pArray = new int [count];` to `int pArray[count]`. Does anyone know why that would cause such a big error? It also stopped the string sorting from causing a Segmentation Fault.

Hey guys, I've been working on a little project of mine, and part of it requires sorting. I'm using bubble sort and before I put the algorithm in the code, the program ran fine.

``````void ranking(){
int count = getcount("all");  // get total members and save value to count
int* pArray = new int [count];
string names[count];          // why can't I dynamically allocate memory to string arrays?

// Get member names for point checking and sorting
string name;
ifstream members("members.txt");
for(int i = 0; i < count; i++){
members >> names[i];
}
members.close();

// Get total points for each name and save to corresponding pArray element so names[i] has pArray[i] points
for(int i = 0; i < count; i++)
pArray[i] = totalpoints(names[i]);

// Bubble Sort because it's simple and I don't need speed
bool flag = true;
for(int i = 0; (i < count) && (flag == true); i++){
flag = false;
for(int s = 0; s < count; s++){
if(pArray[s] < pArray[s + 1]){
// Sorts the names and total points keeping them in the same element number so names[i] has pArray[i] points
int temp = 0;
//string stemp;
temp = pArray[s];
pArray[s] = pArray[s + 1];
pArray[s + 1] = temp;
// stemp = names[s];
// names[s] = names[s + 1];   Causes Segmentation Fualt (core dumped)
// names[s + 1] = stemp;
flag = true;
}
}
}

// Display for debug purposes
for(int i = 0; i < count; i++)
cout << pArray[i] << endl;

}``````

The ...

Back to the whole size - 1 thing. Ever since I changed it to size, I've had extra characters appearing at the end of my hashed strings(ex. hello = igopt, the '' appears as an 'ACK' surrounded by black)

Edit: nevermind, I made the mistake of also changing the 'size - 1' in the for loops, which added an extra character.

After I changed `int ascii[size]` to int `*ascii = new int[size]` the program started crashing after I typed what I wanted it to encrypt.
Segmentation fault (core dumped)
Process returned 139 (0x8B)

Coming back to previous discussion, in declaration of ascii, its index will run from 0 to 3 and not from 0 to 4 as far as I am concerned. Because according to me what const expression within square brackets indicate is the size of array that is 4 bytes.

Oh wow, I feel like an idiot. For some reason I was thinking the decleration told what number it went to. I'm very sorry about that. You're definitely right. But the program still runs perfectly which now seems impossible since it should be leaving off a character. Any idea why it still works?

Oh okay, I've changed it. Thank you for your explanation and patience in dealing with me, I really appreciate it! :)

I'm sorry, but you're wrong.
Let string x ={'1', '2', '3', '4', '5'}.
x.length() = 5
If ascii[x.length() - 1], then it would declare ascii. As you said above.
However, that means it has 5 elements because it starts from 0.
ascii[0,1,2,3,4]. I'm not mixing up anything, this is right.
Haha, I hate to say it but I don't know what you mean by

Moreover there is abig problem with your code at line 12 which I earlier skipped.
It should be int *ascii = new int[size]
You should use new for dynamic allocation of memory. Though you can get away with it due to compiler optimisation, it is not advised in standards.

Do you think you could explain? I would be very grateful.

@nchy13: I had thought about the problem with the ASCII values, but I couldn't think of a solution, modulus never even crossed my mind :/
You are, however, wrong about "It should be of size 'size' not 'size-1' and its index will run from 0 to size-1." The length of the string is a countable number, so because arrays start at 0, the size needs to be length - 1.('char' is 4 characters so an array for that would need to go from 0 to 3.
I've also made the other two changes you reccomended. I really don't know much about the string library, so thanks for that. I'll look into it.
Thank you :)

Hey guys, as the title says, this is my first encryption program. Being extremely simple, I was just looking for advice on ways I could improve it. I just recently got back into programming(I tried to learn before but unfortunately lost interest while I was still learning the basics) and would appreciate any advice and/or criticism you could offer. Thanks.

``````#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// encrypts clear text string
int encrypt(string x){

int size = x.length();
int ascii[size - 1];
int change = 1;
string hash;

// converts the chars of string x into its ascii value
for (int i = 0; i <= size - 1; i++)
ascii[i] = x[i];

// changing each ascii value by +change(dependent on number of times it loops)
for (int i = 0; i <= size - 1; i++) {
ascii[i]= ascii[i] + change;
change++;
}

// converts the new ascii values into a hashed string
for (int i = 0; i <= size - 1; i++)
hash[i] = ascii[i];

ofstream crypt;
crypt.open("hash.txt",ofstream::app);

// displays hash on screen and saves it to hash.txt
for (int i = 0; i <= size - 1; i++) {
cout << hash[i];
crypt << hash[i];
}

crypt << endl;
crypt.close();

cout << endl;

return 0;
}

// decrypts hashed string
int decrypt(string y){

int ysize = y.length();
int yascii[ysize - 1];
int change = 1;
string clear;

for (int i = 0; i <= ysize - 1; i++) ...``````

Thank you so much!! I didn't know that :/ I should probably read more.

I'm sorry, but that's not the problem :/ I just checked my code in the file, and everything matched perfectly. I seem to have made an error typing it into the forum(should have just ^c and ^v). Any other help would be greatly appreciated.

-It won't let me edit the original post to fix that error :/

Hey guys, I just started learning HTML/Javascript yesterday and I just wanted to mess around a bit. I'm working on a few things dealing with user input but I've come accross a little snag. I want it to take in 3 inputs, do an operation, and then output the answer. Equation: (x + y) / z = a. To test it, I input the same number for x, y, and z because that's supposed to give me a = 2. The problem is, it doesn't. Depending on the number input for all three variables, the output changes.(e.g. x,y,z = 10 a = 101; x,y,z = 2 a = 11) I've no idea what's wrong, but it's probably some stupid mistake on my part.

Here's the code:

``````<!DOCTYPE html>
<html>
<title>Test</title>
<body>
<form name="TESTING">
<table border="1" width="600" height="200" cellpadding="10" cellspacing="3">
<tr>
<th colspan="2">
<h1>TESTING</h1>
</th>
<td>
<input type="number" name="lev" id="input1" onchange="calculate()"/>
<input type="number" name="AT" id="input2" onchange="calculate()"/>
<input type="number" name="DE" id="input3" onchange="calculate()"/>
</td>
<td>
<td>
<input type="number" name="ANS" id="output">
</td>
</td>
</tr>
</table>
</form>
<script>
function calculate() {
var USERINPUT1 = document.TESTING.Lev.value,
USERINPUT2 = document.TESTING.AT.value,
USERINPUT3 = document.TESTING.DE.value,
RESULT = (USERINPUT2 + USERINPUT3) / USERINPUT1;
document.TESTING.ANS.value = RESULT;
}
</script>
</body>
</html>``````

Hey guys, I just started learning HTML/Javascript yesterday and I just wanted to mess around a bit. I'm working on a few things dealing with user input but I've come accross a little snag. I want it to take in 3 inputs, do an operation, and then output the answer. Equation: (x + y) / z = a. To test it, I input the same number for x, y, and z because that's supposed to give me a = 2. The problem is, it doesn't. Depending on the number input for all three variables, the output changes.(e.g. x,y,z = 10 a = 101; x,y,z = 2 a = 11) I've no idea what's wrong, but it's probably some stupid mistake on my part.

Here's the code:

``````<!DOCTYPE html>
<html>
<title>Test</title>
<body>
<form name="TESTING">
<table border="1" width="600" height="200" cellpadding="10" cellspacing="3">
<tr>
<th colspan="2">
<h1>TESTING</h1>
</th>
<td>

<input type="number" name="lev" id="input1" onchange="calculate()"/>

<input type="number" name="AT" id="input2" onchange="calculate()"/>

<input type="number" name="DE" id="input3" onchange="calculate()"/>
</td>
<td>
<td>
<input type="number" name="ANS" id="output">
</td>
</td>
</tr>
</table>
</form>
<script>
function calculate() {
var USERINPUT1 = document.TESTING.Lev.value,
USERINPUT2 = document.TESTING.AT.value,
USERINPUT3 = document.TESTING.DE.value,
RESULT = (USERINPUT2 + USERINPUT3) / USERINPUT1;
document.TESTING.ANS.value = RESULT;
}
</script>
</body>
</html>``````

Thank you. Wow do I feel like an idiot, I can't believe it was that easy.

I'm writing a program that will keep track of every Anime/Manga I watch/read(I'm an avid fan). I'm trying to make a database in order to keep track of all of the titles of the Anime/Manga. My problem is that when i try to add a new title to the existing list, it overwrites the entries that are already there. Here is the code for my add function.

``````int Add(void)
{
outfile.open("database.txt");

cout << "What is the title of the Anime/Manga that you wish to add?" << endl;
cout << "::>";
cin >> Title;

outfile << Title << endl;

outfile.close();
}``````

As I said it overwrites what was already in the file. Here is the function that reads the content of the file in case that might help.

``````int List(void)
{
int count = 0;

infile.open("database.txt");

//Check for errors
if(infile.fail())
{
cerr << "Could not open Database file.";
exit(1);
}

while(!infile.eof())
{
infile >> Title;
cout << Title << endl;
count++;
}

//Display number of entries
cout << count << " entries found." << endl;

infile.close();
}``````

I have infile and outfile declared globally if that matters. Title is also global.

Hi. I am 14 years old, currently a freshman in high school. I love anything and everything about computers. I recently took up programming(C++, C#) and i love it.
When i'm not on a computer i like to hang out with my friends, go to the movies, play baseball, and listen to music. A lot of people tease me about it, but i love classical music. My favorite is Bach. I also listen to almost anyothe type of music, except metal.