# help simplifying code



## ste2425 (Jan 25, 2010)

ive wrote this little bit of code, the task is you select a midi status byte and it will tell you what channel number its on. But the problem is its rather a large bit of code for something as small as whats asked for. is there any way of doing this different then using a switch and still getting the same answer?
 heres what ive done


> void Midi(int note)
> {
> switch (note)
> {
> ...


----------



## Kreij (Jan 25, 2010)

```
if (note > 0 && note < 17)
    cout << "Channel " + (note - 1).ToString();
else
    Error on Input
```

That is C#, I think C++ converts integers to string automatically, but it's been awhile.


----------



## ste2425 (Jan 25, 2010)

Kreij said:


> ```
> if (note > 0 && note < 17)
> cout << "Channel " + (note - 1).ToString();
> else
> ...



ok i understand whats going on there, were using c++ at uni is there much difference would that work i i just put it in? nd whats 





> .ToString()


 all about


----------



## Deleted member 3 (Jan 25, 2010)

It converts whatever you use it on to a string. Usually needed with collections and the likes, if this was vb.net it wouldn't be needed in this case. Not sure about other languages, I doubt it.


----------



## Kreij (Jan 25, 2010)

ToString() is a method in C# to cast another type (int, decimal, object, etc.) to a string.


----------



## ste2425 (Jan 26, 2010)

rather then starting a new thread ive got another question. Is there any way to make c++ store letters as well as numbers? coz weve done floats an arrays an classes an things but thats all for numbers and different decimal placings if that makes sense but if there was a variable where you can input letters as well as numbers it would make some task much easier, is there any sort of function for this? Im really gettin into programing and picking it up real quick cheers guys n gals.


----------



## DirectorC (Jan 26, 2010)

I'm no pro at this but my guess is if you want to have numbers & letters together in the same value it would have to be a character string.

Not sure how much good it would do, you'd have to write an algorithm to then go through that character string and pick out all the independent values, splitting the numbers from the letters.


----------



## ste2425 (Jan 26, 2010)

right ok thanks, im really new to this so dont no everything but i want to no more so theres no form of variable where you can type a word and it will automaticly save that word?


----------



## human_error (Jan 26, 2010)

ste2425 said:


> rather then starting a new thread ive got another question. Is there any way to make c++ store letters as well as numbers? coz weve done floats an arrays an classes an things but thats all for numbers and different decimal placings if that makes sense but if there was a variable where you can input letters as well as numbers it would make some task much easier, is there any sort of function for this? Im really gettin into programing and picking it up real quick cheers guys n gals.



Well personally i'd use a string and have code dissect the string to get the different values (as long as it was a fixed length). Since you've used arrays already why not use an array storing a different character/number in a different position, that way you can update and retrieve individual values easily. For storing multiple numbers of number/letter combos you can make a 2D array, which is an array of arrays.


----------



## Kreij (Jan 26, 2010)

@ ste, here's a little tip. Take a few moments and learn all of the different variable types that are available to you in C++ (e.g int, double, char, boolean, etc.) and get a comfortable working knowledge of them. You are going to be using them constantly in every program you write.

For a little practice, you could write a program that creates and manipulates all of the types.
The output of the program will then allow you to see if the type manipulation performs as you expected.


----------



## ste2425 (Jan 26, 2010)

Kreij said:


> @ ste, here's a little tip. Take a few moments and learn all of the different variable types that are available to you in C++ (e.g int, double, char, boolean, etc.) and get a comfortable working knowledge of them. You are going to be using them constantly in every program you write.
> 
> For a little practice, you could write a program that creates and manipulates all of the types.
> The output of the program will then allow you to see if the type manipulation performs as you expected.



ok thanks yes your right i should research for my self then come for help if i cannot find the anser 
thanks for time guys


----------



## ste2425 (Feb 12, 2010)

ive worked it out i think, arrays can be put into arrays right? so all i need to do is put all my arrays with notes in different cells on a array, then just call each cell of the array in the loop. Just have the array name then in the[] bit have a variable where the user inputs the cell number containing the array?


----------



## Clement (Feb 12, 2010)

DirectorC said:


> I'm no pro at this but my guess is if you want to have numbers & letters together in the same value it would have to be a character string.
> 
> Not sure how much good it would do, you'd have to write an algorithm to then go through that character string and pick out all the independent values, splitting the numbers from the letters.



It might be simpler to use a structure/class and separate these two as an integer and a string/char array. Not sure what exact environment/compiler you are using.

When you are writing the code to access the the integer, simply convert the int to ASCII. Your code will be cleaner.

I find it much simpler to store/access characters as ints. But thats just me.

Writing code to analyze these two together, IMHO, would be overkill.

On the other hand you would learn A LOT


----------



## Clement (Feb 12, 2010)

ste2425 said:


> ive worked it out i think, arrays can be put into arrays right? so all i need to do is put all my arrays with notes in different cells on a array, then just call each cell of the array in the loop. Just have the array name then in the[] bit have a variable where the user inputs the cell number containing the array?



Multi dimensional array, yes. Array within an array, that's not really what it is but if that works for you by all means call it that!

If you go this route, learn to use pointers with arrays.


----------



## ste2425 (Feb 14, 2010)

the multi dementional arrays works a treat, ive just one other question if you dont mind spareing your time. 
with normal arrays you can quickly assign the values to each cell like this:


> int myArray [3] {0,1,2};


but with multi dimentional arrays i only no of the long way to assign values:


> int myArray [3] [3];
> myArray [0] [0] =60;
> myArray [1] [0] =61;
> myArray [2] [0] =62;
> and so on


is there a quicker way to do this like with the normal arrays?


----------



## Disparia (Feb 15, 2010)

In PHP we can do,


```
$TehArray = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));
```

After a little Googling... seems that you can do the same in C++ as well.


```
int TehArray[3][3] = { {1, 2, 3} , {4, 5, 6}, {7, 8, 9} };
```


----------



## Clement (Feb 15, 2010)

ste2425 said:


> the multi dementional arrays works a treat, ive just one other question if you dont mind spareing your time.
> with normal arrays you can quickly assign the values to each cell like this:
> 
> but with multi dimentional arrays i only no of the long way to assign values:
> ...



Don't forget, you can use loops to simplify the process further.

Instead of changing these values manually, if you ever have to, you may want to look into loading them from a config file? Not sure if your notes will ever change but its just a suggestion.

A simple menu system would make this application much more accessible to your friends  (Convert, Display, Edit, etc. notes)

Gotta love PHP .


----------



## ste2425 (Feb 15, 2010)

aye i do have loops to play the notes and use variables to access the data in the array. so it seems quite easy but i no this stuff is rather basic but im learnin it quick, and enjoying it too.


----------



## Clement (Feb 15, 2010)

ste2425 said:


> aye i do have loops to play the notes and use variables to access the data in the array. so it seems quite easy but i no this stuff is rather basic but im learnin it quick, and enjoying it too.



As you learn more you can modify this code if you choose, or create new code.

If its fun by all means, Enjoy!!!!


----------



## ste2425 (Feb 15, 2010)

Clement said:


> As you learn more you can modify this code if you choose, or create new code.
> 
> If its fun by all means, Enjoy!!!!



that much is true


----------



## ste2425 (Feb 15, 2010)

yes finally i got it done!!!! jesus it was hard but fun, my first proper full program. Its nothing spectacular as oposed to what you guys can do but for my first program i like it. I feel a strange sense og achievement now ive done it. Had a few bugs one was if you selected it to play a scale with 12 notes then a scale with 8 it would get to the eight then try to carry on and throw up a midi error, but that was fixed with a simple loop. Another error was if you selected it to accend when playing a scale then select another scale to decend straight after it would hang on one note and just keep playing it. But fixed that too
so again thanks for the help, my program probably wouldnt be half as good without it.


----------



## Clement (Feb 15, 2010)

ste2425 said:


> yes finally i got it done!!!! jesus it was hard but fun, my first proper full program. Its nothing spectacular as oposed to what you guys can do but for my first program i like it. I feel a strange sense og achievement now ive done it. Had a few bugs one was if you selected it to play a scale with 12 notes then a scale with 8 it would get to the eight then try to carry on and throw up a midi error, but that was fixed with a simple loop. Another error was if you selected it to accend when playing a scale then select another scale to decend straight after it would hang on one note and just keep playing it. But fixed that too
> so again thanks for the help, my program probably wouldnt be half as good without it.



Congratulations!!!! 

BTW my first complete (bug free ) program ever, was 'Hello World' in C, yours is far more complex than that!


----------

