Really stuck on a fairly simple function.

Write your question here.

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  int scramble(char num[], int values) {


int z= 0 ;
do{
        z++ ;
        values = values / 10 ;
    }while(values!=0) ;

    cout << num << " scrambled \"\ ";
    int i = 0 ;

   do{

        if(num[i] == 0){
            cout << "a" ;

        }
        if(num[i] == 2){
            cout << "e" ;

        }
         if(num[i] == 4){
            cout << "i" ;

        }
         if(num[i] == 6){
            cout << "o" ;

        }
         if(num[i] == 8){
            cout << "u" ;

        }
        i+=1 ;
    }while(i<z) ;
    cout << "\"\ " << endl ;
}


And here is how I call the function:
1
2
 
scramble(numb, value) ;


numb is the array inputted and value is the array value converted into an integer.

Let's say I compile the code and cin the value '323'. What couts for this function is "323 scrambled is " "".

Any help would be greatly appreciated!
Also, im a really big noob so any advice on cleaning up my code will also be greatly valued.
Last edited on
If num[i] is a char, the comparisons should look for '0', '2', etc. instead of 0, 2, etc.
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;
    const char encode[] = "a e i o u ";   
    do {
        int digit = values % 10;
        result += encode[digit];
    } while (values/=10);

    cout << " scrambled \"" << result << "\"\n";
}
Last edited on
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.

If so, this does it (example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <algorithm>
#include <array>
#include <random>
#include <chrono>

int main()
{
    std::array <int, 10> arr { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    for (int i : arr)
        std::cout << i << ' ' << std::flush;

    unsigned int seed =
    std::chrono::system_clock::now().time_since_epoch().count();

    std::shuffle(arr.begin(), arr.end(), std::default_random_engine(seed));

    std::cout << "\n\n\n";

    for (int i : arr)
        std::cout << i << ' ' << std::flush;

    std::cout << '\n';

    std::cin.get();

    return 0;
}
Last edited on
@Chervil that code worked!

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.

Last edited on
@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.

 
const char encode[] = "a e i o u ";

could be written more lengthily like this:
 
const char encode[11] = { 'a', ' ', 'e', ' ', 'i', ' ', 'o', ' ', 'u', ' ', '\0' };

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.

Hope this helps.
Topic archived. No new replies allowed.