c++中用scanf输入string的问题

在学习C++时遇到使用scanf输入string导致的问题,由于scanf不支持C++的string类型,尝试通过resize扩展字符串长度导致比较错误。解决方案包括使用C++标准输入、指定输入分隔符或使用临时char数组进行输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做OJ的相关题,由于OJ系统检测内存和算法时间对Java很不利,因此转行学习C++,目前尚处于初学阶段,遇到08年上海交大的一题,题目不再复述,先贴代码吧。


#include <iostream>
#include <string>
#include <stdio.h>
#define ISYEAR(x) (x%100!=0&&x%4==0)||(x%400==0)?1:0
using namespace std;
int dayOfMonth[13][2]={
		0,0,
		31,31,
		28,29,
		31,31,
		30,30,
		31,31,
		30,30,
		31,31,
		31,31,
		30,30,
		31,31,
		30,30,
		31,31
};
struct Date{
	int year;
	int month;
	int day;
	void nextDay(){
		day++;
		if(day>dayOfMonth[month][ISYEAR(year)]){
			month++;
			day=1;
			if(month>12){
				year++;
				month=1;
			}
		}
	}
};
string monthName[13]={
	"","January","February","March","April","May","June","July","August","September",
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值