System("cls") doesn't work with my case 4

I can't figure out why my case 4 won't work. Every case is suppose to cls after the operation finish. But somehow case 4 will just ignore and go back to main menu.



#include<iostream>
#include<string>
#include<iomanip>
#define num_student 2

using namespace std;

int main()
{
bool Back=true;

string firstName[num_student];
string lastName[num_student];
unsigned int studentId[num_student]={0};
char option;
char answer;
int temp;
int size = sizeof(studentId)/sizeof(unsigned int);

do
{
system("cls");
cout<<endl<<endl;
cout<<setw(46)<<"1. Create a student list"<<endl<<endl;
cout<<setw(54)<<"2. Search a student by studentId"<<endl<<endl;
cout<<setw(59)<<"3. Sort the student list by studentId"<<endl<<endl;
cout<<setw(49)<<"4. Display the student list"<<endl<<endl;
cout<<setw(41)<<"5. Quit the program"<<endl<<endl;
cout<<endl<<setw(44)<<"Enter your choice: ";
cin>>option;
system("cls");

switch (option)
{
case '1' :
{
for(int index=0;index<num_student;index++)
{
cout<<endl<<endl<<"Enter student ID : ";
cin>>studentId[index];
cin.ignore();
cout<<endl<<"Enter first name : ";
getline(cin,firstName[index]);
cout<<endl<<"Enter last name : ";
getline(cin,lastName[index]);
}
}
break;
case '2' :
{
int searchId;
bool found=false;
cout<<endl<<endl<<"\tEnter student ID : ";
cin>>searchId;
for (int a=0;a<num_student;a++)
{
if(searchId==studentId[a])
{
cout<<endl<<"\tStudent ID : "<<studentId[a];
cout<<endl<<endl<<"\tStudent first name : "<<firstName[a];
cout<<endl<<endl<<"\tStudent last name : "<<lastName[a];
found=true;
}
}
if(!found)
{
cout<<endl<<endl<<"\tThis student ID doesn't exist.";
cout<<endl<<endl<<"\tTry again? (Y or N) : ";
cin>>answer;

}while(answer=='y'||answer=='Y');
{
}
}

break;
case'3' :
{
for(int b=0;b<size-1;b++)
{
for(int k=b+1;k<size;k++)
{
if(studentId[k]>studentId[b])
{
temp=studentId[b];
studentId[b]=studentId[k];
studentId[k]=temp;
}
}
}
}
break;
case'4' :
{
for(int i=0;i<num_student;i++)
{
cout<<endl<<"\tStudentID : "<<studentId[i];
cout<<endl<<"\tFirst name : "<<firstName[i];
cout<<endl<<"\tLast name : "<<lastName[i];
cout<<endl;
}
}
break;
case'5' :
{
return EXIT_SUCCESS;
}
break;
}
}
while (Back=true);
{
}
cin.ignore();
cin.get();
return EXIT_SUCCESS;
}
closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
case '3' :
for(int b=0;b<size-1;b++)
 {
   for(int k=b+1;k<size;k++)
    {
     if(studentId[k]>studentId[b])
      {
        temp=studentId[b];
        studentId[b]=studentId[k];
        studentId[k]=temp;
    }
  }
}
break;


there was an extra } brace before the break;
check that you don't have too many
Last edited on
closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
case'4' :
  for(int i=0;i<num_student;i++)
    {
        cout<<endl<<"\tStudentID : "<<studentId[i];
        cout<<endl<<"\tFirst name : "<<firstName[i];
        cout<<endl<<"\tLast name : "<<lastName[i];
        cout<<endl;
     }
break;


from what I saw, you don't have any clear screens in any of the cases.
there are system("cls"); in the do loop before the cases only.
you have extra braces after some case statements.
Last edited on
closed account (1CfG1hU5)
maybe visit reference section to for case format

http://msdn.microsoft.com/en-us/library/k0t5wee3.aspx

looking at this web page, shows can use braces after case: statement

one example shows no braces afte case:, another one shows with braces.

dos compilers did not need braces after "case 'A':" for example

Topic archived. No new replies allowed.