C++/c++

Advertisement


Question
please give me more information about switch case.is it possible to call a function with object in switch case?
i m facing a problem when i run this code
when i m running this code only default case is working both case 1 and case 2 r not working.


void main()
{
int ch;
addrecord rd;
cout<<"\n enter ur choice[1 for student/2 for teachers]";
cin>>ch;
switch(ch)
{
case1:rd.getdata_st();
      break;
case2:rd.getdata_t();
     break;
default:cout<<"invalid entry";
      return;
}
}

where addrecord is a class and rd is an object of tis class. getdata_st() & getdata_t() are two  member functions of this class.getdata_st() take information about student and getdata_t() take information about teacher.  

Answer
switch(ch)
{
     case  1:rd.getdata_st();
         break;
     case  2:rd.getdata_t();
         break;
     default:cout<<"invalid entry";
         return;
}

You need a space after the keyword 'case'
case labels are the keyword 'case' , one or more white spaces, a constant expression, a colon ':' eg.

case  1 :

case  2 :

etc.


'case1:' is an identifier label (also called a statement label) which can be the target of a 'goto' eg.

case1: if( i < 7 ) std::cout << "hello world\n" ;

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.