invoke a function but the result confuses me

invoke one function called the_size(), and there is an if conditional and else conditional. however both of them are executed. why?

/*invoke the_size() and it leads to the output of else */
#include <iostream>   
using namespace std;
bool boool=true; 
void the_size();        

int main()
{ int c; 
cout<<"please select one function by entering the number"<<endl;
cout<<"3.the_size\()"<<endl;
cin>>c;
if(c==3)
the_size();
system("pause");      
return 0;
}
void the_size()
{ if(boool==false)        
  	  cout<<"you entered a wrong command,please enter again"<<endl;
    else 
  	  cout<<"If you want to konw the sizes of different types, please enter true"<<endl; 
   string bol;
   getline(cin,bol);
    if (bol=="true")                                          
{ cout<<"\"sizeof\"(type or variable) in byte(8 bits)"<<endl;
  int size;
  
  size=sizeof(char);
  cout<<" size of char is "<<size<<endl;
  
  size=sizeof(short);
  cout<<" size of short is "<<size<<endl;
    
  size=sizeof(int);
  cout<<" size of int is "<<size<<endl;
  
  size=sizeof(long);
  cout<<" size of long is "<<size<<endl;
  
  size=sizeof(float);
  cout<<" size of float is "<<size<<endl;
  
  size=sizeof(double);
  cout<<" size of double is "<<size<<endl;
}
else  { boool=false;the_size();}         
}



/* result on the screen*/
If you want to konw the sizes of different types, please enter true
you entered a wrong command,please enter again
ajiaiaoj
you entered a wrong command,please enter again
pokokdgaga
you entered a wrong command,please enter again
At the end:

1
2
else  { boool=false;the_size();}         
}


You're setting boool to false, and calling the_size again, which results in the routine being called again and printing different output.

This routine probably should not be calling itself.
After this statement cin>>c;, there is a newline character remaining in the input buffer. That prevents the getline command from operating properly.
Add this line to remove it.
cin.ignore(100,'\n');

I'd recommend using a loop (while or do-while) rather than have the function call itself.
Topic archived. No new replies allowed.