我将来自C Primer的代码复制为while循环的示例,并且它不输出任何内容.我正在使用g.
#include <iostream> int main() { int sum = 0,val = 1; // keep executing the while as long val is less than or equal to 10 while (val <= 10) { sum += val; // assigns sum+ val to sum\ ++val; // add 1 to val } std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; }
解决方法
sum += val; // assigns sum+ val to sum\
摆脱行尾的反斜杠.那是一个线条延续的角色.它导致下一行连接到此行;换句话说,val成为“指定求和的总和”评论的一部分.