# C++ Note ###### tags: `JUCE` ## 2021.6.15 ### 1. ```Switch Case```的```block``` : #### 問題 : 下面的```code```會有問題 : - 因為 : 兩個```case```是在同一個```block```的,所以對```complie```而言,```num```重複宣告。 - 回憶 : 對```C++```而言```case```後面必須加入break,不然會一直執行下去,代表```case```是同個```block```層內相接的```statement```。 ```c= void function Example(int type){ switch(type){ case 1 : int num = 1 ; break ; case 2 : int num = 2 ; break; } } ``` #### 改善 : 增加```block```的區塊 - 將```case```區塊使用```{}```包住,形成不同的宣告區域。 - 也可以把需要宣告的變數移出去```case```外,在```switch```開始的地方宣告。 ```c= void function Example(int type){ switch(type){ // int num ; case 1 :{ int num = 1 ; // num =1 ; break ; } case 2 :{ int num = 2 ; // num =2 ; break; } } } ``` ### 2. ```float```的```mod``` : - 問題 : ```C++```的```mod operator```不支援浮點數運算。 ```c= float a = 1.2 ; float b = 3.4 ; a%b; // Complie Error ``` - 改善 : 使用```<cmath>```的```fmod```函式 ```c= #include<cmath> ... float a = 1.2 ; float b = 3.4 ; a%b; // Complie pass ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up