我正在开发一个程序,用户输入关于他日常生活的某些数据,以及他必须做的事情,以及他们的优先级。我已经做了一个algoritham来解决这个问题但是在执行它时我得到了一些奇怪的输出。我已经包含下面的程序和输出(我知道程序是rudimentery):
By 1889, central telephone exchange operators were known as
'hello-girls' due to the association between the greeting and the telephone
这个的预期输出是:
#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'
当我要求程序输出数组FACTime中的第二个值并使用以下代码将其转换为小时和分钟时,才会发生此错误:
'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'
我无法找到解决方案,甚至将-2147483648的输出转换为十六进制也无济于事。如果我带走了代码,它输出一切都很好但是用它输出它。感谢所有帮助。
答案 0 :(得分:1)
FACTime[2]
永远不会被初始化,也不会是priority[]
的大部分内容。
在第一个循环中,仅当i&gt;时才设置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];
}
答案 1 :(得分:0)
有一件事Normal
正在被使用而没有被初始化,所以这可能是你的第一个输出线的问题。
但真正的问题似乎是优先考虑的问题。它只包含1个项目,但最后的for循环运行3次。