想问下cin 的用法
看到cin配合 >>来用但按例子
#include<iostream>
#include<string>
int main()
{
string word;
while(cin >> word)
cout >> "word read is:">> word >> '\n';
cout >> "ok:no more words to read:bye!\n";
return 0;
}
是报错的,请问cin 是干嘛的,如何用? cin就是标准输入,一般是屏幕键盘输入,但程序可以根据需要重定向之。cin和C的stdin类似(看名字就知道),是一个封装stdin的类。
cin和>>结合其实是cin类重载了>>(右移位)运算符,表示从标准输入读入数据,类型和右操作数相同。上面的cin >> word就是从键盘(屏幕)读入一个字符串放到word里面。
至于报错,原因应该有cout是和<<配合,不是>>;string、cin、cout都是在std命名空间内的符号,改成std::string、std::cin、std::cout或main函数第一句加上using namespace std;
LZ应该学下怎么看错误信息自己DEBUG,就算是提问也可以说明报什么错。其实很多错误都是低级错误,自己可以修正的。 哦,原来这样! 还是不怎么懂,不过懂了一点 举例:
#include<iostream>
#include<string>
using namespace std;
void main()
{
string word;
cin >> word;
cout << word;
}
程序开始你就可以打字,按了回车后会把你刚打上的东西重复一遍。 一直也不使用string,結構太多,又慢。。
页:
[1]