scanf函数详细解释:
EOF的意义及用法(while(scanf("%d",&n) != EOF))
https://blog.csdn.net/henu1710252658/article/details/83040281
在while中使用cin>>a 为条件,注意事项
https://blog.csdn.net/qiqi123i/article/details/52185599
scanf简单示例:
#include "stdio.h"
int main(void)
{
int a;
int b;
int c;
printf("Give me the value of a,b,c seperated with whitespaces:\n");
//每次读入三个十进制数,赋值到a,b,c;若数量不够,则不会读入;每个输入数之间以空格相间隔,空格可连续多个
while(scanf("%d%d%d",&a,&b,&c) != EOF)
{
printf("a=%d,b=%d,c=%d \r\n",a,b,c);
}
printf("end test! \n\r");
return 0;
}
输出:
cin >> 示例
#include<iostream>
using namespace std;
int main()
{
int currVal = 0 , val = 0;
if(cin >> currVal)
{
int cnt=1;//统计次数
while(cin >> val)
{
if(currVal == val)
{
cnt++;
}
else{
cout << currVal <<" occurs "<<cnt<<" times "<<endl;
currVal = val;
cnt=1;//重新计算另一个
}
} //while循环结束
cout << currVal <<" occurs "<<cnt<<" times "<<endl;//打印最后一个数
}//if循环结束
return 0;
}