[html]
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1,v2;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;
return 0;
}
輸出:
[html]
pateo@pateo-B86N53X:~/work/study$ g++ -o main main.cc
pateo@pateo-B86N53X:~/work/study$ ls
main main.cc
pateo@pateo-B86N53X:~/work/study$ ./main
Enter two numbers:
1
2
The sum of 1 and 2 is 3
pateo@pateo-B86N53X:~/work/study$
[html]
pateo@pateo-B86N53X:~/work/study$ ./main
Enter two numbers:
4 6
The sum of 4 and 6 is 10
pateo@pateo-B86N53X:~/work/study$
endl 說明:操作符,將它寫入輸出流時,具有輸出換行的效果,並刷新與設備相關聯的緩沖區,通過刷新緩沖區,用戶可立即看到寫入到流中的輸出
其他說明:c++ 也有前++和後++,相應的for循環等基礎語法是一樣的
[html]
#include <iostream>
int main()
{
int sum = 0, value;
while(std::cin >> value)
sum += value;
std::cout << "Sum is " << sum << std::endl;
return 0;
}
停止動作:兩次ctr+d
輸出:
[html] www.2cto.com
pateo@pateo-B86N53X:~/work/study$ ./main
1 2 3 Sum is 6
pateo@pateo-B86N53X:~/work/study$