[問題] C++ 時間換算
這是一題24小時換12小時的問題?請高手們幫我看一下為什麼我跑出來會是下列的樣子.
Enter the hours for the 24 hour time: 13
Enter the minutes for the 24 hour time: 40
The time converted to 12 hour format is: 80:40 P.M.
#include <iostream>
using namespace std;
//
// function declarations
//
void input(int& hours, int& minutes);
void output(int hours, int minutes, char type);
int convertTo12Hour(int hours, char& type);
int main()
{
int hours;
int minutes;
char type;
char answer;
do {
input(hours, minutes);
hours = convertTo12Hour(hours, type);
output(hours, minutes, type);
cout << "Perform another calculation? (y/n): ";
cin >> answer;
} while ((answer == 'Y') || (answer == 'y'));
return 0;
} // function main
//
// function definitions
//
void input(int& hours, int& minutes)
{
//
// prompt the user for the number of hours in 24 hour format
//
cout << "Enter the hours for the 24 hour time: ";
cin >> hours;
//
// prompt the user for the number of minutes
//
cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
} // function input
void output(int hours, int minutes, char type)
{
//
// display the output
//
cout << "The time converted to 12 hour format is: " << hours << ":";
//
// special handling for leading 0s on the minutes
//
cout.width(2);
cout.fill('0');
cout << minutes;
//
// Output A.M. or P.M. based on type.
//
if (type == 'A')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
} // function outpu
int convertTo12Hour(int hours, char& type)
{
if (hours == 0)
{
hours = 12;
return 'A';
}
if (hours >= 12)
if (hours == 12)
return 'P';
else
{
hours -= 12;
return 'P';
}
return 'A'
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 71.199.79.106
推
03/09 06:09, , 1F
03/09 06:09, 1F
推
03/09 06:54, , 2F
03/09 06:54, 2F
→
03/09 06:55, , 3F
03/09 06:55, 3F
→
03/09 06:55, , 4F
03/09 06:55, 4F
→
03/09 06:56, , 5F
03/09 06:56, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章