C/C++常用输入方法整理

1 输入常用方法

1.1 基础输入

1.1.1 C++:iostream

cin:可嵌入while中实现变量的反复输入
1)【遇空格/Tab/回车结束】

#include <iostream>
using namespace std;

int main(){
   
	char ch;
	while(cin>>ch){
   
		cout << ch;
	}
	return 0;
}

//输入:qa zw sx edc
//输出:qazwsxedc

1.1.2 C:stdio.h

scanf:可嵌入while中实现变量的反复输入
1)【遇回车结束】

#include <stdio.h>

int main(){
   
	char ch;
	while(scanf("%c", &ch)){
   
		printf("%c\n", ch);
	}
	return 0;
}

//输入:qa zw sx edc
//输出:qa zw sx edc

1.2 关于字符

1.2.1 C++: cin.get()

所属库:iostream

常用1: cin.get();
仅接收一个字符,不接收空格/回车

#include <iostream>
using namespace std;

int main (){
   
	char ch;
	ch=cin.get();//或cin.get(ch);
	cout<<ch<<endl;
	return 0;
}

//输入:abcdefg
//输出:a

常用2: cin.get(变量名,数目);
接收一串字符,可接收空格, 但不接收回车

#include <iostream>
using namespace std;

int main (){
   
	char a[20];
	// 接收19个字符,末尾补\0
	cin.get(a,20);
	cout<<a<<endl;
}

//输入:qaz wsx 123
//输出:qaz wsx 123

//输入:abcdeabcdeabcdeabcdeabcde(输入25个字符)
//输出:abcdeabcdeabcdeabcd     (接收19个字符+1个'\0')

通用: cin.get(字符数组名,接收长度,结束符);

#include <iostream>
using namespace std;

int main(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值