大学一年级在将 ascii 转换为 int 时遇到问题。
问题是这段代码
无符号短 iminutes = ((minutes[3]-48)*10) + (minutes[4]-48);
当我在家中的代码块上运行它时,它返回一个不正确的值,当我再次运行它时,我得到一个不同的不正确值。
当我在大学的 Borlands 上运行它时,屏幕会在我阅读它之前升起并消失,所以我也不能在这里使用系统时钟。
现在是复活节,所以即使我在上大学,我也不能惹恼我的导师,因为他们不是。
#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;
}