Is it possible to loop a switch?

Pages: 12
Nov 15, 2009 at 9:01pm
I'm writing a program and I'm wondering if it's possible to loop a switch because my program is suppose to emulate a process going into the critical section of a computer. Something like this:

=========================================
You are in the critical section Reader 2
=========================================

=========================================
You are in the critical section Reader 2
You are in the critical section Reader 5
=========================================

=========================================

You are in the critical section Reader 2
You are in the critical section Reader 5
You are in the critical section Writer 2
=========================================


Until it is eventually full which would be with 3 readers and 3 writer


Thanks
If anyone can help it would be greatly appreciated
Nov 15, 2009 at 9:15pm
Sure you can loop a switch. Just put your switch in a loop.

Or am I misunderstanding?
Nov 15, 2009 at 9:22pm
I tired that but it doesn't seem to restart and give me new information is simply prints this:

============================
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
You are in the critical section Reader 2
=============================

For how ever long the loop is set for


Last edited on Nov 15, 2009 at 9:23pm
Nov 15, 2009 at 9:26pm
Well you're probably not recording the information right.

I won't be able to help without seeing a small section of code showing the basic idea of what you're trying to do.
Nov 15, 2009 at 9:33pm
The code is posted below. I 'm using the rand() function to autocailly select a case in the swtcih to print out. but unfortunately it stays on one during it's loop.

Thanks



#include <iostream.h>
#include <fstream>
using namespace std;


int main()
{





int x, choice;
srand(time(NULL));
x = rand() % 8 + 1;
int count = 0;


ofstream outfile ("readers.txt",ios :: out);






switch (x)
{
while (true)
{
case 1: outfile << "You are in Reader's 1 Critical section" <<endl ; //return 0;
break;
case 2: outfile << "You are in Reader's 2 Critical section" <<endl;//return 0;
break;
case 3: outfile << "You are in Reader's 3 Critical section" <<endl; //return 0;
break;
case 4: outfile << "You are in Reader's 4 Critical section" << endl;//return 0;
break;
case 5: outfile << "You are in Reader's 5 Critical section" << endl;//return 0;
break;
case 6: outfile << "You are in Writer's 1 Critical section" <<endl ;//return 0;
break;
case 7: outfile << "You are in Writer's 2 Critical section" <<endl;//return 0;
break;
case 8: outfile << "You are in Writer's 3 Critical section" <<endl; //return 0;
break;
}
}




count++;

outfile << count << endl;
outfile<< x << endl ;

}

Nov 15, 2009 at 9:37pm
There are a few things wrong with this:

1) You put the loop in the switch. You're supposed to put the switch in the loop.

2) you're not looping your input, so x will never change, so you'll always print the same thing over and over.


Whatever you want to happen multiple times -- that is what needs to be inside the loop:

1
2
3
4
5
6
7
8
while(true)
{
  x = /*change x somehow */;
  switch(x)
  {
    //check the new value of x
  }
}
Nov 15, 2009 at 9:51pm
I will give it a shot thanks
Nov 15, 2009 at 11:50pm
Thank you it works
Nov 16, 2009 at 12:07am
Its there anyway to keep them as a group meaning the first time it goes around it gets


============================
You are in the critical section Reader 2 First run
============================


============================
You are in the critical section Reader 2 Second run
You are in the critical section Reader 5
===========================

===========================
You are in the critical section Reader 2 Third run
You are in the critical section Reader 5
You are in the critical section Writer 2
===========================
Nov 16, 2009 at 2:15am
I don't really understand what you mean, sorry.

Do you mean you want to remember the output of the program from the last time it was run?
Nov 16, 2009 at 2:35am
Yes. I was looking into nested loops but it doesn't seem to work.
Nov 16, 2009 at 2:59am
Looking back, I don't even understand what I was asking before. hahah oh well.

If you want the program to remember something, you need to put it in memory (ie: a variable).

If there's a fixed maximum that you're going to have to remember, the most basic way is to just use an array:

1
2
3
4
5
6
7
8
9
int output[20];  // no more than 20 numbers to be remembered
int count = 0;  // how many have been remembered

while(count < 20)
{
  int x = /*whatever*/;
  output[count] = x;
  ++count;
}


Or if you want to get into STL, a better way to go would probably be a deque or a vector. This lets it dynamically grow as you add number so there's no fixed maximum like there is with an array:

1
2
3
4
5
6
7
8
9
10
11
12
#include <deque>
using namespace std;

...

deque<int> output;

while( /*whatever*/ )
{
  int x = ...
  output.push_back(x);
}


You can then use an iterator to step through the deque and output all the numbers.

-------------------------

Now if you were talking about saving the program output so that when the program closes and is reopened it remembered what it output last time, you'll need to save the output to a file and then reload it in the program. But that's another topic I don't care to get into.
Nov 16, 2009 at 3:15am
Sorry it didn't work what i was basically looking for was something like this

*
**
***
****
*****
******
*******
********

every time the loop runs the * is incremented and saved. i wanted keep it all in a group.
Last edited on Nov 16, 2009 at 3:15am
Nov 16, 2009 at 3:24am
Or more like this:

11
11 12
11 12 13
11 12 13 14
11 12 13 14
Nov 16, 2009 at 3:27am
Right. That's what I was showing.

Of course recording it is only half. When you output you'd have to traverse the array and output each element.
Nov 16, 2009 at 3:34am
I saw just wondering why this doesn't work

for( m=1; m<100; m++)

{
for(n=1; n<100; n++)
{


maybe because it's a switch?
Nov 16, 2009 at 4:30am
Switch has nothing to do with it.

Nested loops do work. And you probably want to do something like that. But you have to arrange everything properly. Your code is probably all misplaced.

I get the impression you're trying random things and going "well that didn't work", and then just trying something else. This hardly ever leads to a solution.

In order to program you need to understand that the computer is very stupid. You have to tell it every little thing you want it to do. It doesn't know how to do anything on its own.

The upside to this is that if you understand the code you're writing you can see what the computer will do before you actually run the program. This is why you need to really understand how loops (and all control structures) work.


If the program doesn't do what you expect, instead of trying random changes, try stepping through the code on your own. Pretend you are the computer and you have to run this program.

Let's try this. Let me see your new code and I'll walk through it with you and point out exactly what the computer is doing. That way you can see why things aren't working the way you expect them to.
Nov 16, 2009 at 1:07pm
#include <iostream>
#include <fstream>
using namespace std;


int main()
{





int x, n =1 , m =1;




ofstream outfile ("readers.txt",ios :: out);

srand(time(NULL));
int count = 0 ;





for( m=1; m<100; m++)

{
for(n=1; n<100; n++)

{
count++;
outfile << "=====================================================" <<endl;
outfile << count << endl;
x = rand() % 8 + 1;

switch (x)
{



case 1: outfile << "You are in Reader's 1 Critical section" <<endl ;
break;
case 2: outfile << "You are in Reader's 2 Critical section" <<endl;
break;
case 3: outfile << "You are in Reader's 3 Critical section" <<endl;
break;
case 4: outfile << "You are in Reader's 4 Critical section" << endl;
break;
case 5: outfile << "You are in Reader's 5 Critical section" << endl;
break;
case 6: outfile << "You are in Writer's 1 Critical section" <<endl ;
break;
case 7: outfile << "You are in Writer's 2 Critical section" <<endl;
break;
case 8: outfile << "You are in Writer's 3 Critical section" <<endl;
break;

outfile << "=====================================================" <<endl;
}
}


}
}

Last edited on Nov 16, 2009 at 1:15pm
Nov 16, 2009 at 1:14pm
That's better; although I still don't understand what this "critical section stuff is."
Nov 16, 2009 at 1:17pm
well I'm trying to get the for statement to work where it keep the statement from the from loop and brings in into the second loop and so on but it doesn't work i still get the same output.
Pages: 12