第一年有学习问题将ascii转换为int。将ascii substr转换为int
问题是这样的一段代码
无符号短iminutes =((分钟[3] -48)×10)+(分钟[4] -48);
当我在家里的代码块上运行它时,它返回一个不正确的值,当我再次运行它时,我得到了一个不正确的值。
当我在大学的Borland上运行它时,屏幕刚刚起来并消失,因此我无法在此处使用系统时钟。
现在是复活节了,即使我在大学时,我也不能因为他们不是我的老师而烦恼。
#include <iostream.h>
#include <conio.h>
#include <string>
//#include <time.h>
//#include <ctype.h>
using namespace std;
int main() {
bool q = false;
do {
// convert hours to minutes ... then total all the minutes
// multiply total minutes by $25.00/hr
// format (hh:mm:ss)
string theTime;
cout << "\t\tPlease enter time " << endl;
cout <<"\t\t";
cin >> theTime;
cout << "\t\t"<< theTime << "\n\n";
string hours = theTime.substr (0, 2);
cout <<"\t\t"<< hours << endl;
unsigned short ihours = (((hours[0]-48)*10 + (hours[1] -48))*60);
cout << "\t\t"<< ihours << endl;
string minutes = theTime.substr (3, 2);
cout <<"\t\t"<< minutes << endl;
unsigned short iminutes = ((minutes[3]-48)*10) + (minutes[4]-48);
cout << "\t\t" << iminutes << endl;
cout << "\n\n\t\tTotal Minutes " <<(ihours + iminutes);
cout << "\n\n\t\tTotal Value " <<(ihours + iminutes)*(25.00/60) << "\n\n";
}
while (!q);
cout << "\t\tPress any key to continue ...";
getch();
return 0;
}
另一个问题是,他减去'48',而不是'” 0''。 (还有,他没有验证他有数字开头。) – 2012-04-04 09:46:47