Hello guys, its my first time posting here and I was hoping I could get some help. I've been stuck on this for the past hour or so and have moved things around enough to a point where I think its me not understanding syntax well enough to trouble shoot this correctly.
So here's my problem: I want the user to be able to input a number which will then be interpreted as an array. With this array I want to do a few things, but the function im stuck on is printing out each element in the array into a specified letter (if the value isn't defined then print out nothing). The way I've done it so far is:
Well, I'm not sure I understand the requirements.
If I'm getting this, an input of 323 should give an output of "e"?
There are a number of things I don't understand in the code - for example the function receives an array and an integer, both representing the same value? If they are the same, why not just pass an integer alone and let the function do the work?
One other point which may help - the array char num[] presumably contains ASCII characters? If so, your code if (num[i] == 0) should test for char '0' rather than integer 0.
However, rather than a series of if-else statements, you may find an array lookup to be simpler to code and easier to read.
1 2 3 4 5 6 7 8 9 10 11 12
void scramble(int values)
{
cout << values;
string result;
constchar encode[] = "a e i o u ";
do {
int digit = values % 10;
result += encode[digit];
} while (values/=10);
cout << " scrambled \"" << result << "\"\n";
}
I'm not really sure because you weren't too clear, but do you also want to scramble the elements in the array? By scramble, I mean switch the position of the elements and the order.
How does the encode array know which vowel to add to the result string?
I understand that the digit integer is the remainder left over after dividing by 10, but I don't understand how plugging it in would return the correct letter you want.
For example in the 323 input,
Wouldn't it make digit = 3.
How would the program know to add nothing to the string for encode[3]?
That doesn't seem to be defined in your array, so I'm quite confused how it works.
@Demineon
The important thing I had in mind when writing that code was that int digit could have any value from 0 to 9. That was the starting point. So I figured I needed an array with a length of ten elements.
The last element is the null terminator which we aren't interested in here.
encode[0] is 'a'
encode[1] is ' ' (a space)
encode[2] is 'e'
encode[3] is ' '
and so on.
How would the program know to add nothing to the string for encode[3]?
Well, it doesn't add nothing, it adds a space character which it finds in the array.