C++ program giving -2147483648 output -


i developing program user inputs data daily life, , things has do, along priority level them. have made algoritham figure out when executing weird output. have inlcuded program , output below (i know program rudimentery):

#include <iostream> #include <cstdlib> #include <cstdio>  using namespace std;  int bodyprogram() {  int a; cout << "how many activities have per weekday week?: "; cin >> a;  string numactivities[7][2] = {     {         "how time spend per meal?:",         "how many meals have per day?:",     },     {         "how time spend in bathroom? (excluding taking pee):",         "how many times go bathroom per day?:",     },     {         "when wake up?(ex. 7:00am should typed 07 00): ",     },     {         "how many naps take per day?(enter 0 if none):",         "how long per nap?(0 if none):",     },     {         "do take breaks, excluding meals? (enter 0 if none):",         "how long per break?(0 if none):",     },     {         "how time spend @ work or school? (h:m ex. 7 hours , 10 minutes 7 10):",         "how time spend commuting each way (in minutes)?:",     },     {         "enter name of activity:",         "enter priority level of activity 1-10, 1 being optional , 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 information in minutes unless specified\n";  (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)      {         (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 , "<<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;      (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 , "<<ftime<< " minutes\n";   return 0; }       int main() {  bodyprogram();  return 0; } 

the intended output is:

'how many activities have per weekday week?: 2 please enter information in minutes unless specified how time spend per meal?:15 how many meals have per day?:2 how time spend in bathroom? (excluding taking pee):30 how many times go bathroom per day?:2 when wake up?(ex. 7:00am should typed 07 00): 08 45 how many naps take per day?(enter 0 if none):0 how long per nap?(0 if none):0 take breaks, excluding meals? (enter 0 if none):0 how long per break?(0 if none):0 how time spend @ work or school? (h:m ex. 7 hours , 10        minutes 7 10):7 00 how time spend commuting each way (in minutes)?:30 enter name of activity:ac1 enter priority level of activity 1-10, 1 being optional:1          enter name of activity:ac2 enter priority level of activity 1-10, 1 being optional:6 10 hours , 22 minute x hours , x minutes' 

however output get:

'how many activities have per weekday week?: 2 please enter information in minutes unless specified how time spend per meal?:15 how many meals have per day?:2 how time spend in bathroom? (excluding taking pee):30 how many times go bathroom per day?:2 when wake up?(ex. 7:00am should typed 07 00): 08 45 how many naps take per day?(enter 0 if none):0 how long per nap?(0 if none):0 take breaks, excluding meals? (enter 0 if none):0 how long per break?(0 if none):0 how time spend @ work or school? (h:m ex. 7 hours , 10        minutes 7 10):7 00 how time spend commuting each way (in minutes)?:30 enter name of activity:ac1 enter priority level of activity 1-10, 1 being optional:1          enter name of activity:ac2 enter priority level of activity 1-10, 1 being optional:6 -2147483648 hours , -2147483648 minute -2147483648 hours , -2147483648 minutes' 

this error happens when ask program output second value in array factime , convert hours , minutes following bit of code:

    int ftimend;     ftimend = factime[2];      float ftimedec;     ftimedec = factime[2]-ftimend;      int ftime;     ftime = ftimedec*60;       cout <<ftimend<<" hours , "<<ftime<< " minutes\n"; 

i unable find solution , converting output of -2147483648 hex doesn't help. if take away code outputs fine it outputs that. appreciated.

factime[2] never initialized , neither of priority[].

in first loop, set priority[tpri] if > 5, , set tpri=i-6 can therefore 0. priority[0] set, nothing else. in second loop, factime[2] set if priority[2] evaluates in if/else blocks, doesn't because priority[2] uninitialized.

if each element in priority array intended correspond single activity entered user, wanted like:

for (int f=0; f<a; f++) {     cout << numactivities[6][0];     cin >> name[f];      cout << numactivities[6][1];     cin >> priority[f]; } 

Comments