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
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
=============================
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;
}
}
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
===========================
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>
usingnamespace 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.
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.
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;
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.