2015-07-09 44 views
-1

我正在開發一個程序,用戶輸入關於他的日常生活的某些數據,以及他必須做的事情以及他們的優先級。我已經做了一個算法來解決這個問題,但是當它執行時我得到了一些奇怪的輸出。我已經inlcuded下面的程序和輸出(I知道程序是rudimentery):給出-2147483648輸出的C++程序

#include <iostream> 
#include <cstdlib> 
#include <cstdio> 

using namespace std; 

int bodyProgram() 
{ 
int A; 
cout << "How many activities do you have per weekday this week?: "; 
cin >> A; 

string NumActivities[7][2] = 
{ 
    { 
     "How much time do you spend per meal?:", 
     "How many meals do you have per day?:", 
    }, 
    { 
     "How much time do you spend in the bathroom? (Excluding taking a pee):", 
     "How many times do you go to the bathroom per day?:", 
    }, 
    { 
     "When do you wake up?(ex. 7:00am should be typed as 07 00): ", 
    }, 
    { 
     "How many naps do you take per day?(Enter 0 if none):", 
     "How long per nap?(0 if none):", 
    }, 
    { 
     "Do you take any breaks, excluding meals? (Enter 0 if none):", 
     "How long per break?(0 if none):", 
    }, 
    { 
     "How much time do you spend at work or school? (H:M ex. 7 hours and 10 minutes is 7 10):", 
     "How much time do you spend commuting each way (In minutes)?:", 
    }, 
    { 
     "Enter the name of the activity:", 
     "Enter the priority level of the activity from 1-10, 1 being optional and 10 being of utmost importance:", 
    }, 
}; 

string Name[100]; 
float Time[6]; 
float Amount[6]; 
float NormalSleep; 
float NormalSchool; 
float Normal; 
int priority[100]; 

cout << "Please enter all information in minutes unless specified\n"; 

for (int i=0; i<=6; i++) 
{ 
    if (i==2) 
    { 
     float SleepH; 
     float SleepM; 

     cout << NumActivities[2][0]; 
     cin >> SleepH >> SleepM; 
     cin.clear(); 

      float F; 
      F = SleepM/60; 

      float total; 
      total= SleepH +F; 

     NormalSleep = total; 


    } 

    else if (i==5) 

    { 
     float SH; 
     float SM; 
     float com; 

     cout << NumActivities[5][0]; 
     cin >> SH >> SM; 

     cout << NumActivities[5][1]; 
     cin >> com; 

     if ((SH==0) && (SM==0) && (com)) 
     { 
      continue; 
     } 

     float SF; 
     SF=SM/60; 

     float SchoolTime; 
     SchoolTime=SH+SF; 

     float ComTotal; 
     ComTotal=com * 2; 

     float ComDiv; 
     ComDiv=ComTotal/60; 

     float Total; 
     Total=SchoolTime+ComDiv; 

     float TotalDiv; 
     TotalDiv=Total/60; 

     NormalSchool=TotalDiv; 


    } 

    else if (i>5) 

    { 
     for (int f=0; f<A; f++) 
     { 
      int TPri; 
      TPri=i-6; 

      cout << NumActivities[6][0]; 
      cin >> Name[i]; 

      cout << NumActivities[6][1]; 
      cin >> priority[TPri]; 
     } 
    } 

    else 
    { 
     cout << NumActivities[i][0]; 
     cin >> Time[i]; 

     cout << NumActivities[i][1]; 
     cin >> Amount[i]; 

     float AcT; 
     AcT= Time[i]*Amount[i]; 

     float Hours; 
     Hours = AcT/60; 

     Normal=Normal+Hours; 
    } 

    } 

    float NormalAdd; 
    NormalAdd=Normal+NormalSchool+NormalSleep; 




    int NormalND; 
    NormalND=NormalAdd; 

    float NormalDec; 
    NormalDec = NormalAdd-NormalND; 

    int NormalTime; 
    NormalTime=NormalDec*60; 

    cout <<NormalND<<" hours and "<<NormalTime<< " minutes\n"; 

    float FACTime[50]; 

    float Left; 
    Left=24-Normal; 

    float Alloc; 
    Alloc=Left/A; 

    float Allpri110; 
    Allpri110=Alloc*5; 

    float Allpri29; 
    Allpri29=Alloc*4; 

    float Allpri38; 
    Allpri38=Alloc*3; 

    float Allpri47; 
    Allpri47=Alloc*2; 

    float Allpri6; 
    Allpri6=Alloc*1.2; 

    for (int i=0; i<=A; i++) 
    { 
     if (priority[i]==1) 
     { 
      FACTime[i]=Alloc-Allpri110; 
     } 

     if (priority[i]==2) 
     { 
      FACTime[i]=Alloc-Allpri29; 
     } 

     if (priority[i]==3) 
     { 
      FACTime[i]=Alloc-Allpri38; 
     } 

     if (priority[i]==4) 
     { 
      FACTime[i]=Alloc-Allpri47; 
     } 

     if (priority[i]==5) 
     { 
      FACTime[i]=Alloc; 
     } 

     if (priority[i]==6) 
     { 
      FACTime[i]=Alloc+Allpri6; 
     } 

     if (priority[i]==7) 
     { 
      FACTime[i]=Alloc+Allpri47; 
     } 

     if (priority[i]==8) 
     { 
      FACTime[i]=Alloc+Allpri38; 
     } 

     if (priority[i]==9) 
     { 
      FACTime[i]=Alloc+Allpri29; 
     } 

     if (priority[i]==10) 
     { 
      FACTime[i]=Alloc+Allpri110; 
     } 
    } 


    int FTimeND; 
    FTimeND = FACTime[2]; 

    float FTimeDec; 
    FTimeDec = FACTime[2]-FTimeND; 

    int FTime; 
    FTime = FTimeDec*60; 

    cout <<FTimeND<<" hours and "<<FTime<< " minutes\n"; 


return 0; 
} 






int main() 
{ 
bodyProgram(); 
return 0; 
} 

此預期的輸出結果爲:

'How many activities do you have per weekday this week?: 2 
Please enter all information in minutes unless specified 
How much time do you spend per meal?:15 
How many meals do you have per day?:2 
How much time do you spend in the bathroom? (Excluding taking a pee):30 
How many times do you go to the bathroom per day?:2 
When do you wake up?(ex. 7:00am should be typed as 07 00): 08 45 
How many naps do you take per day?(Enter 0 if none):0 
How long per nap?(0 if none):0 
Do you take any breaks, excluding meals? (Enter 0 if none):0 
How long per break?(0 if none):0 
How much time do you spend at work or school? (H:M ex. 7 hours and 10  minutes is 7 10):7 00 
How much time do you spend commuting each way (In minutes)?:30 
Enter the name of the activity:AC1 
Enter the priority level of the activity from 1-10, 1 being optional:1   
Enter the name of the activity:AC2 
Enter the priority level of the activity from 1-10, 1 being optional:6 
10 hours and 22 minute 
x hours and x minutes' 

然而,這是輸出我得到:

'How many activities do you have per weekday this week?: 2 
Please enter all information in minutes unless specified 
How much time do you spend per meal?:15 
How many meals do you have per day?:2 
How much time do you spend in the bathroom? (Excluding taking a pee):30 
How many times do you go to the bathroom per day?:2 
When do you wake up?(ex. 7:00am should be typed as 07 00): 08 45 
How many naps do you take per day?(Enter 0 if none):0 
How long per nap?(0 if none):0 
Do you take any breaks, excluding meals? (Enter 0 if none):0 
How long per break?(0 if none):0 
How much time do you spend at work or school? (H:M ex. 7 hours and 10  minutes is 7 10):7 00 
How much time do you spend commuting each way (In minutes)?:30 
Enter the name of the activity:AC1 
Enter the priority level of the activity from 1-10, 1 being optional:1   
Enter the name of the activity:AC2 
Enter the priority level of the activity from 1-10, 1 being optional:6 
-2147483648 hours and -2147483648 minute 
-2147483648 hours and -2147483648 minutes' 

只有當我要求程序輸出數組FACTime中的第二個值並將其轉換爲小時和分鐘時,纔會出現此錯誤,並顯示以下幾位代碼:

int FTimeND; 
    FTimeND = FACTime[2]; 

    float FTimeDec; 
    FTimeDec = FACTime[2]-FTimeND; 

    int FTime; 
    FTime = FTimeDec*60; 

    cout <<FTimeND<<" hours and "<<FTime<< " minutes\n"; 

我無法找到解決方案,甚至將-2147483648的輸出轉換爲hex也無濟於事。如果我拿走了代碼,它會輸出一切正常,但它會輸出。所有的幫助表示讚賞。

+0

你是什麼輸入? – Axalo

+0

@Axalo我忘了提及我的輸入包含在輸出中。只需在提示旁邊查看我的輸入。 – Sunny

+0

最後一部分根本沒有意義。由於兩個操作數相等,不應該使'FACTime [2] -FTimeND'總是爲0嗎? – Axalo

回答

1

FACTime[2]從不初始化,也不是大部分priority[]

在你的第一個循環,你設置priority[TPri]只有當我> 5,並設置TPri=i-6因此這隻能是0,所以優先級[0]設置,但沒有別的。在第二個循環中,FACTime[2]僅在priority[2]對if/else塊中的值進行求值時設置,但不會因爲priority[2]未初始化。

如果優先級陣列中的每個元素的目的是作爲用戶輸入對應於一個單一的活動,你可能想要的東西,如:

for (int f=0; f<A; f++) 
{ 
    cout << NumActivities[6][0]; 
    cin >> Name[f]; 

    cout << NumActivities[6][1]; 
    cin >> priority[f]; 
} 
+0

我明白了,我爲那個基本錯誤付出了代價。我並不認爲不初始化會導致這種情況,因爲我以前的所有蜥蜴都沒有這樣做。感謝您的知識。 – Sunny

+0

好的,新問題。該計劃的預期輸出是6.35左右,我得到24.75。 – Sunny

+0

您正在訪問未初始化的值只是一個症狀。你的迭代不應該讓你首先訪問這些值。 –

0

首先,Normal未被初始化使用,所以這可能是第一個輸出行的問題。

但是真正的問題似乎是優先考慮的。它只包含1個項目,但最後的for循環運行3次。