C++ 在Xcode-C+中重复程序+;
该程序应该在用户输入错误数据数小时、分、秒后重复 我无法让程序在Xcode上循环 例如: 用户输入小时、分钟和秒的无效数据。 程序显示“无效数据”,然后询问用户是否要重复该程序,如果用户输入“y或y”,程序要求用户再次输入时间C++ 在Xcode-C+中重复程序+;,c++,C++,该程序应该在用户输入错误数据数小时、分、秒后重复 我无法让程序在Xcode上循环 例如: 用户输入小时、分钟和秒的无效数据。 程序显示“无效数据”,然后询问用户是否要重复该程序,如果用户输入“y或y”,程序要求用户再次输入时间 #include <iostream> #include <iomanip> using namespace std; struct Time { int hours ; int seconds; int minutes;
#include <iostream>
#include <iomanip>
using namespace std;
struct Time
{
int hours ;
int seconds;
int minutes;
};
void getTime(Time &time);
bool isTimeValid(Time &time);
void addOneSecond(Time &time);
void displayTime(Time &time);
const int MAX_HOURS = 23;
const int MAX_MINS = 59;
const int MAX_SECS = 59;
int main()
{
Time time;
getTime(time);
isTimeValid(time);
addOneSecond(time);
displayTime(time);
return 0;
}
void getTime(Time &time)
{
cout << "Enter the time in \"military time\", (24-hour format), in"
<< " the following order:\nHH:MM:SS, (Hours, Minutes, Seconds).\n\n";
cout << "Hours: ";
cin >> time.hours;
cout << "Minutes: ";
cin >> time.minutes;
cout << "Seconds: ";
cin >> time.seconds;
cout << endl << endl;
}
bool isTimeValid(Time &time)
{
bool answer = ' ';
if (((time.hours >= 0) && (time.hours <= MAX_HOURS)) &&
((time.minutes >= 0) && (time.minutes <= MAX_MINS)) &&
((time.seconds >= 0) && (time.seconds <= MAX_SECS)))
{
return true;
}
else
{
cout << "Invalid Time.\n\n";
return false;
}
cout << "Do it again? (Y/N)";
cin >> answer;
do
{
getTime(time);
isTimeValid(time);
addOneSecond(time);
displayTime(time);
} while(toupper(answer) == 'T' );
}
void addOneSecond(Time &time)
{
time.seconds++;
if (time.seconds > MAX_SECS)
{
time.seconds = 0;
time.minutes++;
}
}
void displayTime(Time &time)
{
cout.fill('0');
cout << "After adding one second, the time is " << setw(2) << time.hours
<< ":" << setw(2) << time.minutes << ":" << setw(2)
<< time.seconds << ".\n\n";
}
#包括
#包括
使用名称空间std;
结构时间
{
整小时;
整数秒;
整数分钟;
};
void getTime(时间和时间);
bool isTimeValid(时间和时间);
void addones(时间和时间);
无效显示时间(时间和时间);
const int MAX_HOURS=23;
const int MAX_MINS=59;
常数int MAX_SECS=59;
int main()
{
时间;
getTime(时间);
有效期(时间);
加一秒(次);
显示时间(time);
返回0;
}
void getTime(时间和时间)
{
cout time.minutes;
cout>time.seconds;
cout}while(toupper(答案)='T')代码>应该是}而(toupper(答案)='Y')代码>相反?您将answer
声明为bool
,尝试将char
保存到其中,然后使用'T'
检查其值,即使您希望用户提供Y/N
!好的,我把它改为char-answer='';我将while语句改为=='Y'。还是不行