trying to simulate a buffer and producer

I'm trying my hardest to simulate a Buffer and producer and I did it with if statements, well the loop is acting funny it prints out numbers out of order, I want it to print out just 1,2,3,4,5 representing each address in the Buffer when it prints out these numbers its representing that address is full. Should I be using a switch statement instead if not how do I keep the loop from printing out all five if statement results 5 time times in a row because its printing five random numbers 5 X 5 times, i would appreciate any advice

here's my code
I know my if statements are disorganized
but what I'm trying to do is store a number in that address ex.myBuffer[0]=1
cout<<myBuffer[0];
and so on!!!!!

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;

const int N=5;
int myBuffer[N];


void producer(){
	
int bite=0;
	while (true){
		for(int i=0;i<N;i++){
			if(myBuffer[i]==0){
				myBuffer[0]=1;
				cout<<myBuffer[0]<<endl;
		      
		    }
			if(myBuffer[i]==1){
				myBuffer[1]=2;
				cout<<myBuffer[1]<<endl;
		      
			    }
				if(myBuffer[i]==2){
				    myBuffer[2]=3;
					cout<<myBuffer[2]<<endl;
				
					}
					if(myBuffer[i]==3){
						myBuffer[3]=4;
						cout<<myBuffer[3]<<endl;
					
						}
						if(myBuffer[i]==4){
							myBuffer[4]=5;
							cout<<myBuffer[4]<<endl;
						
						  }
		               }
		
	               }
}

int main(){

producer();

system ("PAUSE");
return 0;
}
I have no idea what you think a "buffer and producer" are, but that's some seriously goofy logic you've got going on there. Perhaps you should look up the use of 'else' in combination with if, or maybe just be a little more straightforward:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

const int N=5;
int myBuffer[N];

void producer()
{
    for ( int i = 0;  i < N;  ++i )
        myBuffer[i] = i+1 ;

    for ( int i = 0;  i < N;  ++i )
        cout << myBuffer[i] << endl ;
}

int main(){

    producer();

    system ("PAUSE");
    return 0;
}
Topic archived. No new replies allowed.