在这个C++问题中如何把string类型的数据转化成int类型的数据?

llcnllcn 2003-05-09 08:38:22
#include <iostream>
#include <string>
using namespace std;
void TimeToSeconds(string);

int main()
{
cout<<"please enter the time(hh:mm:ss):"<<endl;
string Time;
cin>>Time;
int realTime=TimeToSeconds(Time);
cout<<"you enter the time is "<<realTime<<" seconds in all"<<endl;
}
void assert();
int TimeToSeconds(string Time)
{
string Hour=Time.substr(0,Time.find(":"));
Time=Time.substr(Time.find(":")+1);
string Minute=Time.substr(0,Time.find(":"));
Time=Time.substr(Time.find(":")+1);
string Second=Time;
int realTime=0;
//在这个地方把Hour,Minute和Second从string类型转化成int型,如何转化?

if(Hour>23||Hour<0||Minute>59||Minute<0||Second>59||Second<0)
{
assert();
}
else
{
realTime=Hour*3600+Minute*60+Second;

}
return realTime;
}

void assert()
{
cerr << "error input" << endl;
exit(1);
}
...全文
128 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shishiXP 2003-05-10
  • 打赏
  • 举报
回复
#include <stdlib.h>
atoi();
fangyi1120 2003-05-10
  • 打赏
  • 举报
回复
atio( ),好象是,但不确定
lifanxi 2003-05-09
  • 打赏
  • 举报
回复
int h = atoi(Hour.c_str());
int m = atoi(Minute.c_str());
int s = atoi(Second.c_str());
下面用h,m,s即可。


70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧