程序
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
int i;
int sum = 0;
// 请输入一段整数,并用空格隔开,然后会输出结果
cout << "Please enter a string of positive numbers separated by Spaces: " << endl;
while (cin >> i)
{
sum += i;
while (cin.peek() == ' ')
{
cin.get();
}
if (cin.peek() == '\n')
{
break;
}
}
cout << "The calculation results: " << sum << endl;
system("pause");
return 0;
}
C++ 基本的输入输出
C++ 的 I/O 发生在流中,流是字节序列。如果字节流是从设备(如键盘、磁盘驱动器、网络连接等)流向内存,这叫做输入操作。如果字节流是从内存流向设备(如显示屏、打印机、磁盘驱动器、网络连接等),这叫做输出操作。
I/O 库头文件
下列的头文件在 C++ 编程中很重要。
头文件 | 函数和描述 |
---|---|
<iostream> | 该文件定义了 cin、cout、cerr 和 clog 对象,分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流。 |
<iomanip> | 该文件通过所谓的参数化的流操纵器(比如 setw 和 setprecision),来声明对执行标准化 I/O 有用的服务。 |
<fstream> | 该文件为用户控制的文件处理声明服务。我们将在文件和流的相关章节讨论它的细节。 |
cin.get()
可以使用 cin.get()
或者 cin.get(var)
,读取一个字符
也可以用
istream& get(char* s, streamsize n)
istream& get(char* s, size_t n, streamsize delim)
二者的区别是前者默认以换行符结束,后者可指定行结束符。n 表示目标空间的大小。
来读取一行
cin.getline()
和 cin.ignore()
从标准输入设备键盘读取一串字符串,并以指定的结束符结束。
istream& getline(char* s, streamsize count); //默认以换行符结束
istream& getline(char* s, streamsize count, char delim);
例子:
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
char buf[20];
cin.ignore(7);
cin.getline(buf, 10);
cout << buf << endl;
system("pause");
return 0;
}
hello world! //输入
orld! //输出
请按任意键继续. . .
cin.peek()
cin.peek()的返回值是一个char型的字符,其返回值是指针指向的当前字符,但它只是观测
类似于检查当前字符
例子:
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
char p;
cout << "Enter a paragraph of text: \r\n";
while (cin.peek() != '\n')
{
p = cin.get();
cout << p;
}
cout << endl;
system("pause");
return 0;
}
Enter a paragraph of text:
aosdifj ojadsfoi sadfsafs hqhqeh qeh h q h q //输入
aosdifj ojadsfoi sadfsafs hqhqeh qeh h q h q //输出
cin.gcount()
和 cin.read()
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
const int Size = 50;
char buf[Size];
cout << "Enter a paragraph of text: ";
cin.read(buf, 20);
cout << "String The number of collected characters is: " << cin.gcount() << endl;
cout << "Enter the following text information: ";
cout.write(buf, 20);
cout << endl;
system("pause");
return 0;
}
// 输入
Enter a paragraph of text: 12345678901234567890123
// 输出
String The number of collected characters is: 20
Enter the following text information: 12345678901234567890
cout.flush()
用于清空当前的输出缓冲区,将缓冲区中的内容显示出来
cin >> i
cin 可以连续从键盘读取想要的数据,以空格、tab 或换行作为分隔符。实例如下。
#include <iostream>
using namespace std;
int main() {
char a;
int b;
float c;
string
cin>>a>>b>>c;
cout<<a<<" "<<b<<" "<<c<<" "<<endl;
return 0;
}
//输入
a
11
5.56
//输出
a 11 5.56
cout.precision()
例子:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double result = sqrt(3.0);
cout << "If we take the square root of 3 and keep 0 to 9 decimal places, we get the following result :\n" << endl;
for (int i = 0; i <= 9; i++)
{
cout.precision(i);
cout << result << endl;
}
cout << "The current output precision is: " << cout.precision() << endl;
system("pause");
return 0;
}
输出:
If we take the square root of 3 and keep 0 to 9 decimal places, we get the following result :
2
2
1.7
1.73
1.732
1.7321
1.73205
1.732051
1.7320508
1.73205081
The current output precision is: 9