标题:[求助]程序执行有问题哦
只看楼主
taijixing
Rank: 1
等 级:新手上路
威 望:1
帖 子:118
专家分:0
注 册:2007-6-9
 问题点数:0 回复次数:1 
[求助]程序执行有问题哦

#include <iostream>
#include <string>
#include <sstream>

using namespace std;


string int_to_string (int n)
{
ostringstream outstr;
outstr<<n;
return outstr.str();
}

void read_time(int &hours,int &minutes)
{
string line;
getline(cin,line);
istringstream instr(line);

instr>>hours;
minutes=0;

char ch;
instr.get(ch);
if (ch==':')
instr>>minutes;
else
instr.unget();

string suffix;
instr>>suffix;
if (suffix=="pm")
hours=hours+12;
}


string time_to_string (int hours,int minutes,bool military)
{
string suffix;
if (!military)
{
if (hours<12)
suffix="am";
else
{
suffix="pm";
hours=hours-12;
}
if (hours==0)
hours=12;
}
string result=int_to_string(hours)+":";
if (minutes<10)
result+="0";
result+=int_to_string(minutes);
if (!military)
result=result+" " +suffix;
return result;

}


int main()
{
cout<<"please enter the time:"<<endl;
int hours;
int minutes;
read_time(hours,minutes);
cout<<"military time:"<<time_to_string(hours,minutes,true)<<endl;
cout<<"using am/pm"<<time_to_string (hours,minutes,false)<<endl;
return 0;
}

分钟读不出来

搜索更多相关主题的帖子: include minutes return 
2007-07-31 23:50
Arcticanimal
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:341
专家分:20
注 册:2007-3-17
得分:0 
char ch;
instr.get(ch);
if (ch==':')//这里的问题,如果没有输入':'就没有读取minutes了
instr>>minutes;
else
instr.unget();

try new catch
2007-08-02 08:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-159557-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.275262 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved