Working with char arrays - Solving Array Problems with Rui's Help

  • Thread starter ruijorgemoren
  • Start date
  • Tags
    Arrays
In summary, The speaker is having issues with creating and using char arrays in their code to load and play wave files. They have a while loop with options to choose from, but their array is not working properly. They are receiving an error message when trying to assign a value to their array.
  • #1
ruijorgemoren
2
0
Hi there!

I'm having problems with creating/using char arrays.

My actual code is: (I reduced the options to make it smaller here..)
Code:
int option;
int i;
fs=11025;  %frequency

%load wave files into MATLAB vectors
[jose]=wavread('jose.wav');
[antonio]=wavread('antonio.wav');

[B]nomes=char();   % ?[/B]

option=0;
i=0;
while option~=12
    
    clc;
    disp('1-Jose');
    disp('2-Antonio');
    option=input('Quais os nomes que pretende reproduzir?');
   
    i=i+1;   %I do this to guarantee that we are in the first array position on the first option choosed
    
    switch option
        
        case 1
            nome=[jose];
            [B]nomes(i)=nome;    %  ?[/B]
            i=i+1;
    
        case 2
            nome=[antonio];    
            i=i+1;
        
        case 3
    
        %etc, etc...
        
    end;
    
    
  sound(nomes,fs);  
    
end;

ok, so basicly the funtion sound will play the wav's I want.
For example, if I want to play antonio.wav and then jose.wav I would do:
Code:
sound(antonio, jose, fs)
And this is when the array appears..
My idea is to have an array and every option that is choosed will create a new position in that array with the value, for example jose or antonio.
I'm not getting this to work with this code.. Array problems?

Help please..


Thanks in advance.
Rui.
 
Physics news on Phys.org
  • #2
ruijorgemoren said:
I'm having problems with creating/using char arrays.

I'm not getting this to work with this code.. Array problems?

We can't debug it if there isn't a bug...

How is it not working?
 
  • #3
Code:
Warning: Out of range or non-integer values truncated during conversion to character.
> In tp3 at 54
  In run at 62
? Error using ==> run
Subscripted assignment dimension mismatch.


My line 54 in tp3.m is:
Code:
nome=[jose];
 

FAQ: Working with char arrays - Solving Array Problems with Rui's Help

1. What is a char array and how is it used?

A char array is a data structure that stores a sequence of characters in a contiguous block of memory. It is often used to represent strings and can be manipulated using various string manipulation functions.

2. How do I declare and initialize a char array?

To declare a char array, you can use the syntax char array_name[size]; where "array_name" is the name you want to give the array and "size" is the number of characters you want to store. To initialize the array with specific characters, you can use curly braces and separate the characters with commas, like char array_name[size] = {'c', 'h', 'a', 'r'};

3. How can I access and modify individual elements in a char array?

You can access and modify individual elements in a char array using their index number. The first element in the array has an index of 0, and the last element has an index of size-1. For example, to access the first element of an array named "my_array", you can use my_array[0]. To modify an element, you can assign a new value to it, like my_array[2] = 't';.

4. How can I determine the length of a char array?

There is no built-in function to determine the length of a char array in C++, but you can use the strlen() function from the cstring library. This function takes a char array as its argument and returns the length of the string as an integer.

5. Can I convert a char array to a string?

Yes, you can convert a char array to a string using the std::string class from the string library. This class has a constructor that takes a char array as its argument and converts it to a string. Additionally, you can use this class's c_str() function to convert a string back to a char array.

Similar threads

Back
Top