我的邏輯在計算用戶輸入的兩次之間的時間差時是關閉的。邏輯C++計算時差
答案沒有出來應該是什麼,我不能完全弄清楚我的邏輯錯誤。我進了我的助理教師,她對我沒有任何幫助。任何幫助將不勝感激。如果需要,我也會發布我的main()函數。我相信布爾人的一些東西正在變得混亂。
我想從主函數中導入我的變量的值,並計算用戶輸入中有多少分鐘,即2:10 AM = 130分鐘的第一個值,第二個值是2:20 AM即= 140,太多輸入之間10分鐘的差異。程序打印138分鐘...如果任何人都可以指出我的積極方向,將不勝感激。
int computeDifference(int hours, int minutes, int hourFut, int minFut, bool isAM, bool isAMFut){
cout<<isAM<<isAMFut<<endl;
int startTime, endTime;
startTime = calcHours(isAM, hourFut, minFut, isAMFut);
endTime = calcFut(hourFut, minFut, isAMFut, isAM);
diff = startTime - endTime;
if (diff < 0)
diff = abs(diff);
return diff;
}
int calcHours(int hours, int minutes, bool isAM, bool isAMFut){
if ((hours < 12) && (isAM))
return minutes +(hours * 60);
if ((hours == 12 && isAM))
hours = 0;
if(hours < 12 && !isAM)
return minutes + hours * 60;
}
int calcFut(int hourFut, int minFut, bool isAMFut, bool isAM){
if ((hourFut == 12) && isAM)
hourFut = 0;
if ((hourFut) < 12 && !isAM)
return minFut + hourFut * 60;
if ((hourFut < 12) && isAM)
return minFut + hourFut * 60;
}
**編輯主要功能
int main(){
//Declarations
int hours, minutes, hourFut, minFut;
bool isAM, isAMFut;
string amOrPM, amOrPMFut;
//Reading in the data from user
cout<<"Please enter the time (hours minutes) ";
cin>>hours>>minutes;
cout<<endl;
cout<<"Please enter PM or AM"<<endl;
cin>>amOrPM;
//Converting string to all uppercase so it is easier to check
transform(amOrPM.begin(), amOrPM.end(), amOrPM.begin(), ::toupper);
//Checks if user entered am, or pm if not asks them to re-enter
while ((amOrPM != "AM") && (amOrPM != "PM")){
cout<<"Please enter PM or AM: ";
transform(amOrPM.begin(), amOrPM.end(), amOrPM.begin(), ::toupper);
}
//Turns the boolean to true if it is the morning
if (amOrPM == "AM")
isAM = true;
else
isAM = false;
cout<<"It is currently "<<hours<<":"<<minutes<<amOrPM<<endl;
cout<<endl;
cout<<"When would you like to travel too?"<<endl;
//Reading in future time
cout<<"Please enter a time (hours minutes)"<<endl;
cout<<"Please enter a time (hours minutes)"<<endl;
cin>>hourFut>>minFut;
cout<<"Is it AM or PM?"<<endl;
cin>>amOrPMFut;
transform(amOrPMFut.begin(), amOrPMFut.end(), amOrPMFut.begin(), ::toupper);
//Checking if user enters am or pm
while ((amOrPMFut != "AM") && (amOrPMFut != "PM")){
cout<<"Is it AM or PM?"<<endl;
cin>>amOrPMFut;
transform(amOrPMFut.begin(), amOrPMFut.end(), amOrPMFut.begin(), ::toupper);
}
if (amOrPMFut == "AM")
isAMFut = true;
else
isAMFut = false;
cout<<isAM<<endl;
cout<<isAMFut<<endl;
插入您的主要功能,標準輸入和輸出您的預期。我沒有看到爲什麼你需要兩個函數才能將時間轉換爲分鐘。 – AchmadJP
@AchmadJP我添加了main()函數.. – Beez
@AchmadJP我要求用戶輸入兩次,然後我想計算兩次以分鐘爲單位的差異。 – Beez