Arrays for beginners

Hi, I'm totally new here, trying to make first steps in programming. Could you check the code? It's rewritten from the book, but doesn't work :/
I'm using Win7 + DevC++

#include <iostream>
using namespace std;

enum steps {nothing, script, firsttrial, secondtrial, recording=6, montage, postsynchron};

void trial_state (steps t[], int hmany, int trial_day);

//*******************

int main()
{
const int scene_number=10;
steps scene[scene_number]={script, firsttrial, secondtrial};

int trial_day=1;
scene[2]=recording;
scene[5]=montage;
scene[9]=recording;
scene[4]=secondtrial;
trial_state(scene,scene_number, trial_day);

}


//**********************

void trial_state(steps s[], int hmany, int day)
{
cout <<day <<".day of montage\n"<<"Trial state: \n";
for(int i=0; i<0; hmany++)
{
cout << "Scene no"<< i <<",";
for (int k=0; k<s[i]; k++)
{
cout<<"#";
}
cout<<endl;
}

}

In trial_state(), outer loop, the condition is always false.
I'm guessing what you want to do is for(int i = 0; i < hmany; ++i)
A few problems with this code.

1
2
3
4
scene[2]=recording;
scene[5]=montage;
scene[9]=recording;
scene[4]=secondtrial;

You're storing into non-consecutive locations in scene. The other locations are uninitialized and will contain garbage.

 
for(int i=0; i<0; hmany++)

The above loop doesn't make sense. Your termination condition is i<0 which will never be true. I think you probably want:
 
for(int i=0; i<hmany; i++)


PLEASE USE CODE TAGS (the <> formatting button), not the quote tags.

Last edited on
The other locations are uninitialized and will contain garbage.

The unspecified elements will be init'd to 0.
Missed the initializer.
Thx for the replies. Works ok right now.

Could you explain what do you mean by initializer? Is there still anything wrong with the code?
Isn't the 13th line initializer?

Is it ok to stop the program with cin.get()? I've used it twice

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
#include <iostream>
using namespace std;

enum steps {nothing, script, firsttrial, secondtrial, recording=6, montage, postsynchron};

void trial_state (steps t[], int hmany, int trial_day);

//*******************

int main()
{
    const int scene_number=10;
    steps scene[scene_number]={script, firsttrial, secondtrial};
    
    int trial_day=1;
    scene[2]=recording;
    scene[5]=montage;
    scene[9]=recording;
    scene[4]=secondtrial;
    trial_state(scene,scene_number, trial_day);
     


//**********

trial_day++;

scene[1]=secondtrial;
scene[5]=postsynchron;
scene[0]=script;
scene[8]=script;
trial_state(scene, scene_number, trial_day);
cin.get();
}
//**********************

void trial_state(steps s[], int hmany, int day)
{
     cout <<day <<".day of montage\n"<<"Trial state: \n";
     for(int i=0; i<hmany; i++)
     {
             cout << "Scene no "<< i <<",";
             for (int k=0; k<s[i]; k++)
             {
                 cout<<" #";
             }
             cout<<endl;
     }
       cin.get();
}
Last edited on
Isn't the 13th line initializer?

Yes, but you initialize only the first three. All the others will be initialized to 0. Note that this happens because you supplied a value for at least one element. If you didn't the array would have been uninitialized and would have contained garbage.

Is it ok to stop the program with cin.get()?

Yes.
Topic archived. No new replies allowed.