2016-11-21 5 views
-1

私は現在の時刻をユーザーに尋ねることになっている「タイムトラベル」プログラムを書いています、彼らの目標走行時間、それらの値を中継するための2つの時刻の差を計算します関数を使用し、時間差が大きすぎる場合は時間の移動を許可しないか、または許可されている場合は、将来または過去の場合に印刷します。今の私の問題は、ジャンプが発生した後、プログラムの最初の反復または最初のジャンプが関連していると考えられる現在の時刻が更新されておらず、プログラムが目標時刻の代わりにデフォルトになることですこれはジャンプが発生した後の技術的な "現在の時間"であるはずです。私はこれを数時間試してみることができませんでした。だから誰かが私を助けてくれることを願っています。プログラムにおける分に変換する「タイムトラベル」

は、あなたの代わりに「現在の時間」のためにユーザーの入力を得るための、システム時刻コールを使用していないのはなぜ

#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std; 

int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs,  int t_mins, bool t_am); 
void print_future(); 
void print_past(); 
void print_SecurityProtocol(); 
int main() 
{ 
int c_hrs, c_mins; 
int t_hrs, t_mins; 
int system_time2; 
int time_difference2; 
bool c_am = 0; 
bool t_am = 0; 
char c, c2, Y, N, y, n, jumpAgain; 
string am_or_pm_current, am_or_pm_target; 
bool g_stop = true; 
cout << "Welcome to Po Sled" << endl; 
cout << "\tSystem booting..." << endl; 
for (int i = 0; i<1; i++) //for loop to run once 
{ 
    cout << "\n\tEnter current time below: " << endl; 
    cout << "\t>Enter hour: "; //User inputs current time in hours 
    cin >> c_hrs; 
    while (c_hrs > 12 || c_hrs < 1) 
    { 
     cout << "Error: Please enter an hour in the range [1, 12]: "; 
     cin >> c_hrs; 
    } 
    cout << "\t>Enter minutes: "; //User inputs current time in minutes 
    cin >> c_mins; 
    while (c_mins > 59 || c_mins < 0) { 
     cout << "Error: Please enter minutes in the range [0, 59]: "; 
     cin >> c_mins; 
    } 
    cout << "\t>Is it AM or PM?: "; //Classifying if current time is AM or PM 
    cin >> am_or_pm_current; 
    while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") { //Checks if valid input, if not repeats message until valid 
     cout << "\tError: Please enter AM/am or PM/pm: "; 
     cin >> am_or_pm_current; 
    } 
    if ((am_or_pm_current == "am") || (am_or_pm_current == "AM")) 
    { 
     c_am = 1; 
    } 
    else if ((am_or_pm_current == "pm") || (am_or_pm_current == "PM")) 
    { 
     c_am = 0; 
    } 
    cout << "\n\tCurrent system time set to " << c_hrs << ":" << setw(2) << setfill('0') << c_mins << am_or_pm_current << endl; 
    cout << "\n\t\tIs this time correct (Y or N)? "; 
    cin >> c; 
    while (c != 'Y' && c != 'y' && c != 'n' && c != 'N') 
    { 
     cout << "\t\t\tError: Please enter Y/y or N/n: "; 
     cin >> c; 
    } 
    if (c == 'N' || c == 'n') 
    { 
     continue; 
    } 
    else 
    { 
     cout << "\n\tSystem initializing and TARDIS unit warming...." << endl; 
     cout << "\tThe Po Sled is engaged and ready for input." << endl; 
    } 
} 
do { 
     //Starts a loop for target jump 
     cout << "\n\tEnter target time below: " << endl; //Enters target time of jump 
     cout << "\t>Enter Hour: "; 
     cin >> t_hrs; 
     while (t_hrs > 12 || t_hrs < 1) { 
      cout << "Error: Please enter an hour in the range [1, 12]: "; 
      cin >> t_hrs; 
     } 
     cout << "\t>Enter minutes: "; 
     cin >> t_mins; 
     while (t_mins > 59 || t_mins < 0) { 
      cout << "\tError: Please enter minutes in the range [0, 59]: "; 
      cin >> t_mins; 
     } 
     cout << "\n\tIs it AM or PM?: "; 
     cin >> am_or_pm_target; //Classifying if target time is AM or PM 
     while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") 
     { //Validates input is AM or PM 
      cout << "\tError: Please enter AM/am or PM/pm: "; 
      cin >> am_or_pm_target; 
     } 
     if ((am_or_pm_target == "am") || (am_or_pm_target == "AM")) 
     { 
      t_am = 1; 
     } 
     else if ((am_or_pm_target == "pm") || (am_or_pm_target == "PM")) 
     { 
      t_am = 0; 
     } 
     cout << "\tTarget time set to " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target; //Sets target time 
     cout << "\n\t\tIs this time correct (Y or N)? "; //Validates if target time entered is correct 

     cin >> c2; 
     while (c2 != 'Y' && c2 != 'y' && c2 != 'n' && c2 != 'N') 
     { 
      cout << "\t\t\tError: Please enter Y/y or N/n: "; 
      cin >> c2; 
     } 
     time_difference2 = compute_time_difference(c_hrs, c_mins, c_am, t_hrs, t_mins, t_am); 

     if (time_difference2 > 360) //If time difference is greater than 6 hours prints error function 
     { 
      print_SecurityProtocol(); 
      continue; 
     } 
     if (c2 == 'N' || c2 == 'n') 
     { 
      continue; 
     } 
     cout << "\tJump was made, the current time is " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target << endl; 

     if (time_difference2 < 0 && time_difference2 > -360) //If time difference is less than 0 prints past function 
     { 
      print_past(); 
     } 
     else if (time_difference2 >= 0 && time_difference2 <= 360) //If time difference is ahead of current time prints future function 
     { 
      print_future(); 
     } 
     cout << "\tWould you like to jump again (Y/N)? "; 
     cin >> jumpAgain; 
     while (jumpAgain != 'Y' && jumpAgain != 'y' && jumpAgain != 'n' && jumpAgain != 'N') //Input validation 
     { 
      cout << "\t\t\tError: Please enter Y/y or N/n: "; 
      cin >> jumpAgain; 
     } 
     if (jumpAgain == 'n' || jumpAgain == 'N') //User exiting program 
     { 
      if (time_difference2 < 0) 
      { 
       cout << "\t\tSystem shutting down; enjoy the past.\n" << endl; 
      } 
      else if (time_difference2 >= 0 && time_difference2 < 360) 
      { 
       cout << "\t\tSystem shutting down; enjoy the future.\n" << endl; 
      } 
     } 
     if (jumpAgain == 'Y' || jumpAgain == 'y') 
     { 
      continue; 
     } 

} while (jumpAgain != 'n' && jumpAgain != 'N'); 
return 0; 
} 

int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs,  int t_mins, bool t_am) //Computes time differences. 
{ 
int currentTime_hours, currentTime_minutes, targetTime_hours, targetTime_minutes, currentTime, targetTime; 
int time_difference; 
if (c_am == 1) //if c_am is true and it is morning 
{ 
    if (c_hrs == 12) 
    { 
     currentTime_hours = c_hrs * 0; 
    } 
    else if (c_hrs != 12) 
    { 
     currentTime_hours = (c_hrs * 60); 
     currentTime_minutes = c_mins; 
     currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time 
    } 
} 
else if (c_am == 0) //if c_am is false and it is afternoon time 
{ 
    if (currentTime_hours == 12) 
    { 
     c_hrs = c_hrs * 60; 
    } 
    else if (currentTime_hours != 12) 
    { 
     currentTime_hours = ((c_hrs + 12) * 60); 
     currentTime_minutes = c_mins; 
     currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time 
    } 
} 
if (t_am == 1) //if t_am is true and it is morning time 
{ 
    if (targetTime_hours == 12) //if target hours equal to 12 special math 
    { 
     targetTime_hours = t_hrs*0; 
    } 
    else if (targetTime_hours != 12) //else do this math 
    { 
     targetTime_hours = ((t_hrs) * 60); 
     targetTime_minutes = t_mins; 
     targetTime = targetTime_hours + targetTime_minutes; 
    } 
} 
else if (t_am == 0) //if target time equal to pm then do this math 
{ 
    if (targetTime_hours == 12) 
    { 
     targetTime_hours = t_hrs * 60; 
    } 
    else if (targetTime_hours != 12) //else if target time not equal to 12 then do normal pm math 
    { 
     targetTime_hours = ((t_hrs + 12) * 60); 
     targetTime_minutes = t_mins; 
     targetTime = targetTime_hours + targetTime_minutes; 
    } 
} 
time_difference = targetTime - currentTime; 
cout << "the difference computed is " << time_difference; 
return time_difference; 
} 

void print_SecurityProtocol() //Function which prints security protocol error message 
{ 
cout << "\tSecurity Protocols Engaging" << endl; 
cout << "\t\tError: The time difference is greater than 6 hours." << endl; 
cout << "\t\t\tPlease re-enter target time." << endl; 
} 
void print_past() //Function that prints when a user is in the past 
{ 
cout << "\tHold onto your lower posterior regions" << endl; 
cout << "\n\t\tYou are now in the relative past" << endl; 
} 
void print_future() //Function that prints when a user is in the future 
{ 
cout << "\tHold onto your lower posterior regions" << endl; 
cout << "\n\t\tYou are now in the relative future " << endl; 
} 
+0

'c_hrs = c_hrs * 60;' - それは正しくありませんか?私はまた、現在の時刻を新しく入力した時刻に設定する場所を見ることができません。 –

+0

@ KenY-N申し訳ありませんが{currentTime_hours = c_hrs * 60;}に変更されました。また、このプログラムでは、現在の時刻を新しく入力した時刻に設定する方法がわからないという問題もあります。私はちょうどジャンプの後に私のdo-whileのc_hrs = t_hrs等のようにそれを作るべきですか? –

答えて

0

お時間をいただき、ありがとうございますか!また、12時間ではなく24時間制を使用し、プロセス内でam/pmを満たす必要がなくなりました。

AMヨーロッパ、ロシア、オーストラリア、または米国北部のドライブを除いて、c_hrs * 60は(少なくとも)60時間(最大でも)24 * 60です。過度の時間の録音です。時間(c_mins * 60)の代わりに分(MINUTES)を参照するつもりはありませんか?

+0

私が書いているプログラムは、12時間の入力システムを使用するために持っているか、私は間違いなく24時間時計を使用しています。また、私は時間の値を合計の分に換算して比較しています。 {c_hrs * 60}は時間値に60を掛けて合計分値を取得します。たとえば、24 * 60の場合は1分の1日となり、1時の場合は13時間* 60となります。 –

関連する問題