/*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 |
|
|
cin>>c;, there is a newline character remaining in the input buffer. That prevents the getline command from operating properly.cin.ignore(100,'\n');