2015-07-09 201 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次。