c
是雙倍的,因此您不能使用模運算符%
。
改爲使用fmod()。
所以改變這樣的:
c %= 3
這樣:
c = fmod(c, 3);
正如斯拉瓦提到的,你也可以使用一個int
代替,就像這樣:
int c = 5; // for example
c %= 3
這不會要求ire使用fmod()
。瞭解模塊運營商%
適用於int
s很重要。
由於πάνταρέι提到的,也有這樣的:Can't use modulus on doubles?
作爲一個側面說明維克多,你有這麼多的變數,但其中大部分尚未使用,或初始化。您是否編譯了所有啓用的警告?這裏是我得到的編譯你的原代碼(有評論說,產生錯誤的行)時:
C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp
main.cpp:9:5: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:8: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:11: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:14: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:17: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:20: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:10:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
x += 5;
^
main.cpp:7:16: note: initialize the variable 'x' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:11:5: warning: variable 'y' is uninitialized when used here [-Wuninitialized]
y -= 2;
^
main.cpp:7:19: note: initialize the variable 'y' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:12:5: warning: variable 'z' is uninitialized when used here [-Wuninitialized]
z *= 10;
^
main.cpp:7:22: note: initialize the variable 'z' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:13:5: warning: variable 'a' is uninitialized when used here [-Wuninitialized]
a /= b;
^
main.cpp:7:25: note: initialize the variable 'a' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:13:10: warning: variable 'b' is uninitialized when used here [-Wuninitialized]
a /= b;
^
main.cpp:7:13: note: initialize the variable 'b' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
11 warnings generated.
你申請一些 –
你的問題是與 –
C爲100這裏,但沒有其他人被設置。 –