No Output for Array

I've created arrays that consist of the times that certain meds are supposed to be taken. I've voided out most everything just so that I can work on the first array. But, it's not printing anything. It's supposed to print an "X" when the int j is equal to one of the numbers in the array. Any help is greatly appreciated!! Hope I gave enough info.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;

//Function Prototype

class KeepRunning 
{public:~KeepRunning() {
    system("pause"); }
};               
void prescriptionSchedule()
{
     //Variable assignment to begin counter as time at 0.
     int j = 0000;                                          
     //Establish times for each medicine to be consumed. 
     int ironPill [3] = {800, 1200, 1800};                                         
     //int antibiotic [] = {0400, 800, 1200, 1600, 2000, 2400};
     // int aspirin [] = {800, 2100};
     //int decongestant [] = {1100, 2000};

     int ironPillTime = sizeof(ironPill)/sizeof(int);
     // int antibioticTime = sizeof(antibiotic)/sizeof(int);
     // int aspirinTime = sizeof(aspirin)/sizeof(int);
     //int decongestantTime = sizeof(decongestant)/sizeof(int);
    
      cout << "Iron Pill:";
      for(int j = 0000; j < ironPillTime; j += 0100)          
           if(j == ironPill[0])
	          cout << "\t\t  X";
           if(j == ironPill[1])
              cout << "\t\t\t\t  X";
           if(j == ironPill[3])
              cout << "\t\t\t\t\t\t   X" << endl;
              }

//	for(j = 0; j < antibioticTime; j += 100)
//  if(clock == antibiotic[j])
//	cout << "X" << '\t';

//  for(j = 0; j < aspirinTime; j += 100)
//	if(clock == aspirin[j])
//	cout << "X" << '\t';

//	for(j = 0; j < decongestantTime; j += 100)
//	if(clock == decongestant[j])
//	cout << "X" << '\t';
	


int main ()
{   
    KeepRunning kr;
    cout << "\nTake prescription when hour is marked with 'X'\n" << endl;
    cout << "\t\t\tPrescription Schedule\n" << endl;    
    cout << "\t    0400  0800  1100  1200  1600  1800  2000  2100  2400" << endl;  
    prescriptionSchedule();   	

    return 0;
}
Print the 'j' in the loop as a debug measure.
I'm sorry, but could you show me what you mean? Thanks!
closed account (j3Rz8vqX)
Iron pill is 3.
1
2
    int ironPillTime = sizeof(ironPill)/sizeof(int);
    cout<<sizeof(ironPill)/sizeof(int)<<endl;


j is 0,100,200,300,...

after 1 iteration:

i=100 > Iron pill.

Loop break.
for(int j = 0; j < ironPillTime; j += 100)
Ohh. So it's not outputting anything because it's reading ironPillTime as the number of values in the array? How to I make it call to the pointers? I thought that's what the sizeof line did?
closed account (j3Rz8vqX)
I believe you want to continue checking while the largest number hasn't been tested against; which is 1800.

Size of is returning 3, the size of your array:
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main ()
{
    int ironPill [3] = {800, 1200, 1800};
    int ironPillTime = sizeof(ironPill)/sizeof(int);
    cout<<sizeof(ironPill)/sizeof(int)<<endl;
    return 0;
}

Integer in my unit has a size of 4, and 3 integers would have a size of 12. After dividing my int(4) it would be left with 3.

A non-permanent static-solution may be:
for(int j = 0; j < 1800; j += 100)

For a dynamic solution, you should try to determine the largest value within your array of integers and test it against that.
for(int j = 0; j < largestValue; j += 100)

In your case, {800, 1200, 1800}, 1800 was the largest value.
Okay thanks a million. Now I've got an output, finally. But it keeps repeating the same output. How can I make it print these things only once?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <time.h>
using namespace std;

//Function Prototype

class KeepRunning 
{public:~KeepRunning() {
    system("pause"); }
};                   
void prescriptionSchedule(void)
{
     //Variable assignment to begin counter as time at 0.
     int j = 0;                                          
     //Establish times for each medicine to be consumed. 
     int ironPill [3] = {800, 1200, 1800};                                         

     int ironPillTime = sizeof(ironPill)/sizeof(int);
   
    cout << "Iron Pill: ";
    for(int clock = 0000; clock < 2400; clock += 100)
	{   
       for(int j = 0; j < ironPill[3]; j++) 
         if(j == ironPill[0])
	       cout << "\t   X";
         if(j == ironPill[1])
           cout << "\t\t\t\t  X";
         if(j == ironPill[2])
           cout << "\t\t\t\t\t\t   X";
}
}
int main ()
{   
    KeepRunning kr;
    cout << "\nTake prescription when hour is marked with 'X'\n" << endl;
    cout << "\t\t\tPrescription Schedule\n" << endl;    
    cout << "\t    0400  0800  1100  1200  1600  1800  2000  2100  2400" << endl;  
    prescriptionSchedule();   	

    return 0;
}


I should add that I'm trying to call each point of the array at it's value. So, I'm trying to print a certain output for {800, 1200, 1800}. That's why I set
if(j == ironPill[0])
and such. Am I trying to do something that's impossible, or am I just doing it wrong?? :/. This is just not my strong point, so I'm sorry if I'm asking stupid questions!
Last edited on
closed account (j3Rz8vqX)
What is it repeating? What is the output?

Also maybe encapsulate line 24-29 in curly brackets to ensure the embedded for-loop is applied to all 3 ifs and not just the first.

1
2
3
4
5
6
7
8
9
       for(int j = 0; j < ironPill[3]; j++) 
       {
         if(j == ironPill[0])
	       cout << "\t   X";
         if(j == ironPill[1])
           cout << "\t\t\t\t  X";
         if(j == ironPill[2])
           cout << "\t\t\t\t\t\t   X";
       }
I'm not really sure how to show you a picture of my output, but it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Take prescription when hour is marked with 'X'.

                                 Prescription Schedule

              0400   0800   1100   1200   1600   1800   2000   2100   2400
Iron Pill:             X                    X                    X
               X                    X                    X
               X                    X                    X 
               X                    X                    X
               X                    X                    X
               X                    X                    X
               X                    X                    X
               X                    X                    X
               etc.
Last edited on
closed account (j3Rz8vqX)
Maybe this is what yo umean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <time.h>
using namespace std;

//Function Prototype

class KeepRunning
{
    public:~KeepRunning() {
        cin.get();}
//    system("pause"); }
};
void prescriptionSchedule(void)
{
    //Variable assignment to begin counter as time at 0.
    //Establish times for each medicine to be consumed.
    int ironPill [3] = {800, 1200, 1800};

    //int ironPillTime = sizeof(ironPill)/sizeof(int);

    cout << "Iron Pill: ";
    for(int clock = 0000; clock < 2400; clock += 100)
    {
            if(clock == ironPill[0])
                cout << "\t   X";
            if(clock == ironPill[1])
                cout << "\t\t\t\t  X";
            if(clock == ironPill[2])
                cout << "\t\t\t\t\t\t   X";
    }
}
int main ()
{
    KeepRunning kr;
    cout << "\nTake prescription when hour is marked with 'X'\n" << endl;
    cout << "\t\t\tPrescription Schedule\n" << endl;
    cout << "\t    0400  0800  1100  1200  1600  1800  2000  2100  2400" << endl;
    prescriptionSchedule();

    return 0;
}
Wow, yeah that helped a lot. I tried that with clock before, but it wasn't compiling so I gave up. Thanks a bunch! Now it's not printing my "0" point of the array for the antibiotics though???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <time.h>
using namespace std;

//Function Prototypes

class KeepRunning                                 //Forces program to continue. 
{public:~KeepRunning() {
          cin.get();}};
                   
void ironPillSchedule(void) {                 
     int ironPill [3] = {800, 1200, 1800};                                          
     int ironPillTime = sizeof(ironPill)/sizeof(int);
    
     cout << "Iron Pill: ";
     for(int clock = 0000; clock < 2400; clock += 100) {
         if(clock == ironPill[0])
	       cout << "\t    X";
         if(clock == ironPill[1])
           cout << "\t\tX";
         if(clock == ironPill[2])
           cout << "\t    X" << endl;}
}     
void antibioticSchedule(void) {
     int antibiotic [6] = {0400, 800, 1200, 1600, 2000, 2400};
     int antibioticTime = sizeof(antibiotic)/sizeof(int);
     
     cout << "Antibiotic: ";
     for(int clock = 0000; clock < 2400; clock += 100) {   
         if(clock == antibiotic[0])
	       cout << "\t\tX";   //***NOT PRINTING 'X' HERE*** 


It's printing for 4 out of the five, all but the first and the last. So, not printing [0] and [5], but all in between??? Sorry, I suck at this :(
closed account (j3Rz8vqX)
Lines based on the source code provided in the last post:

Line 25:
In the array, change the first object value to: 400, not 0400.
{400, 800, 1200, 1600, 2000, 2400}

If you use cout<<antibiotic[0]<<endl;, it'll print out 256.

Line 29:
Change the limit to clock <=2400;, allowing it to include the last iteration.

Also:
Change:
Sorry, I suck at this :(
to
Thanks, I'm still learning :)


Have a good day.

Tip: Everyone starts somewhere.
Thank you so much!! It's so much easier to learn when you can understand your mistakes!!
Topic archived. No new replies allowed.