having trouble with if/else statement. this is a code for simulating cpu scheduling.

im having trouble with my if else statement, it shows error:'else without previous 'if'.i would greatly appreciate comments and suggestions.thanks y'all.

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

int main()
{
char choice,ans;
int a; //process input

cout<<"\n\nCHOOSE AN ALGORTIHM YOU WANT:\n";
cout<<"*USE LOWER CASE*\n";
cout<<"(a.)FIRST-IN, FIRST-OUT(QUEUE)\n"
"(b.)SHORTEST JOB FIRST(NP)\n"
"(c.)SHORTEST JOB FIRST(P)\n"
"(d.)PRIORITY (NP)\n"
"(e.)PRIORITY (P)\n"
"(f.)ROUND ROBIN\n"
"(g.)MULTILEVEL QUEUE\n"
"(h.)MULTILEVEL FEEDBACK QUEUE\n"
"(q.)QUIT\n\n";
cout<<"ENTER CHOICE:\n"<<endl;
cin>>choice;
system("CLS");



if(choice == 'a'|| choice =='A' )
{

int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

cout<<"\nYOU HAVE CHOSEN THE FIRST-COME,FIRST-SERVE ALGORITHM!\n";
cout<<"PLEASE ENTER TOTAL NUMBER OF PROCESS\n";
cout<<"[EXAMPLE: P1,P2 = 2 || P1,P2,P3 = 3 ]\n=";

cin>>n;

cout<<"\nBURST TIME\n=";

for(i=0;i<n;i++)

{

cout<<"P["<<i+1<<"]:";

cin>>bt[i];

}



wt[0]=0; //waiting time for first process is 0

//calculating waiting time

for(i=1;i<n;i++)

{

wt[i]=0;

for(j=0;j<i;j++)

wt[i]+=bt[j];

}

cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";

//calculating turnaround time + table

for(i=0;i<n;i++)

{

tat[i]=bt[i]+wt[i];

avwt+=wt[i];

avtat+=tat[i];

cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];

}

avwt/=i;

avtat/=i;

cout<<"\n\nTHE AVERAGE WAITING TIME\n="<<avwt;
cout<<"\nTHE AVERAGE TURN-AROUND TIME\n="<<avtat;
cout<<"\n\n\nCONTINUE?\n(y)=YES || (n)=NO.\n=";
cin>>ans;
cout<<"\n\n\nCONTINUE?\n(y)=YES || (n)=NO.\n=";
cin>>ans;

return 0;
}

else (choice == 'b' || choice == 'B');
{
cout<<"\nYOU HAVE CHOSEN THE SHORTEST JOB FIRST ALGORITHM!\n";
cout<<"PLEASE ENTER TOTAL NUMBER OF PROCESS\n";
cout<<"[EXAMPLE: P1,P2 = 2 || P1,P2,P3 = 3 ]\n=";
cin>>a;


float total,wait[a];
float p[a],amwaiting=0,waiting=0;
int process;
int stack[a];
float burst[a],arrival[a],amburst,temp[a],top=a,priority[a];
int x;

for(x=0;x<a;x++)
{
p[x]=x;
stack[x]=x;
cout<<"ARRIVAL TIME = ";
cin>>arrival[x];
cout<<"BURST TIME = ";
cin>>burst[x];
cout<<"POSITION= ";
cin>>priority[x];

temp[x]=arrival[x];
amburst=burst[x]+amburst;

}

for(x=0;x<amburst;x++)
{

process=stack[0];
if(temp[process]==x)
{

amwaiting=0;
}
else
{
amwaiting=x-(temp[process]);
}
temp[process]=x+1;
wait[process]=wait[process]+amwaiting;
waiting=waiting+(amwaiting);
burst[process]=burst[process]-1;

if(burst[process]==0)
{
for(int x=0;x<top-1;x++)
{
stack[x]=stack[x+1];

}
top=top-1;


for(int z=0;z<top-1;z++) //last element inserted in the stack
{
if((priority[stack[0]]>priority[stack[z+1]]) && (arrival[stack[z+1]] <= x+1))
{
int t=stack[0];
stack[0]=stack[z+1];
stack[z+1]=t;
}

}

}

}
cout<<"\n\nTHE AVERAGE WAITING TIME \n= "<<waiting/a;
float tu=(amburst+waiting)/a;
cout<<"\nTHE AVERAGE TURN-AROUND \n= "<<tu;
cout<<"\n\n\nCONTINUE?\n(Y)=YES || (N)=NO.\n=";
cin>>ans;
system("CLS");

}
else if (choice == 'c' || choice == 'C')
cout<<"";
}
Last edited on
Your return 0 is in the wrong place. It goes at the end of your main function.
thanks for the reply. i tried removing it but nothing has changed. i got expected primary expression before '{'.
Last edited on
firstly this:
1
2
3
4
5
6
7
8
cin>>a;


		float total,wait[a];
		float p[a],amwaiting=0,waiting=0;
		int process;
		int stack[a];
		float burst[a],arrival[a],amburst,temp[a],top=a,priority[a];


You can not easily populate a C-style array with a variable like this. It needs to be a constant value.

Your second issue: I'm going to paste back your formatted if/else code, but remove the code from inside them so you can see the if else's more clearly:
1
2
3
4
5
6
7
8
9
10
11
	if(choice == 'a'|| choice =='A' )
	{

		
	}
	else (choice == 'b' || choice == 'B');
	{
		
	}
else if (choice == 'c' || choice == 'C')
	cout<<"";


Now it should be clear to see what your issue is (hint: 2 small issues).
Last edited on
this is an assignment for my class.im still a little lost at this.i am sorry,but what do you mean by populating it with constant values?

and i tried removing the code from my if/else, and i still don't get the part where i went wrong.lastly am i right that else if statements cant have conditions in them?
Topic archived. No new replies allowed.