Scanning for input in a char array and popping out
Feb 28, 2011 at 4:29am UTC
Hello everyone,
I have a program written where a user inputs several type of OS processes with time required to run.
For instance the input would be some thing like this:
New 10 //New process starts @ 10sec
CPu 12
Write 18
New 15 //Another process starts @ 15sec
My question is when a user inputs a process, if it is a New process, how can I use scanf(or something like that) to catch the 'New' process & move everything from that point on till another 'New' process is entered into a new queue or an array.
My code
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
using namespace std;
int main()
{
int numProcess = 0;
int Processtime[100];
int serviceTime = 0;
int totalTime = 0;
float averageTime = 0.0;
int wait = 0;
char Processname[100][100];
char SearchNew = 'NEW' ;
cout<<"Please enter no. of processes." <<endl;
scanf("%d" , &numProcess);
for (int i = 0; i<numProcess; i++)
{
cout<<"Enter name for each process." <<endl;
cout<<i+1<<endl;
//Attempt to scan for 'New' Process
/**\\ do
{
cout<<"Fond New Process"<<endl;
}while(Processname[i]=='NEW');**/
}
for (int i = 0; i<numProcess; i++)
{
cout<<"Enter Process time" <<endl;
cout<<"\t" <<Processname[i]<<endl;
scanf("%d" , &Processtime[i]);
}
cout<<"Processname \t Process time" <<endl;
for (int i = 0; i< numProcess; i++)
{
printf("\t %s\t \t%d\n" ,Processname[i],Processtime[i]);
}
for (int i = 0; i<numProcess; i++)
{
printf("Process %s from %d" , Processname[i], wait,(wait+Processtime[i]));
wait+=Processtime[i];
}
for (int i = 0; i<(numProcess-1); i++)
{
serviceTime+=Processtime[i];
totalTime+=serviceTime;
}
averageTime = (float )totalTime/numProcess;
cout<<"Average waiting time" <<averageTime<<endl;
totalTime = averageTime=serviceTime=0;
for (int i = 0; i<numProcess; i++)
{
serviceTime+=Processtime[i];
totalTime+=serviceTime;
}
averageTime = (float )totalTime/numProcess;
cout<<"Turn around time" <<averageTime<<endl;
system("Pause" );
return 0;
}
I would appreciate if someone give me some specific example & not give a link.
Thankyou
Topic archived. No new replies allowed.