#include"iostream"usingnamespace std;intmain(){char choice;bool validInput =false;
cout <<"Please enter one of the following choices: \n"<<"c) carnivore p) pianist \n"<<"t) tree g) game \n";while(!validInput){
cin.get(choice);
cin.ignore();// 使用这个用于清理cin多余的换行符, 必须多输出 "Please enter c, p, t, or g: " 这个语句// cin >> choice;if(choice =='c'|| choice =='p'|| choice =='t'|| choice =='g'){
validInput =true;}else{
cout <<"Please enter c, p, t, or g: "<< endl;}}switch(choice){case'c': cout <<"A maple is a carnivore"<< endl;break;case'p': cout <<"A maple is a pianist"<< endl;break;case't': cout <<"A maple is a tree"<< endl;break;case'g': cout <<"A maple is a game"<< endl;break;}return0;}
#include"iostream"usingnamespace std;longfactorial(int n);// function prototypeintmain(){while(true){int i;
cout <<"Enter n: \n";
cin >> i;if(i<0){// Enter a negative number to exit the loop
cout <<"Negative numbers have no factorial! \n";break;}else{
cout <<factorial(i)<< endl;}}return0;}longfactorial(int n){long fact =0;if(n ==0){return1;}
fact = n *factorial(n-1);if(n==1){::printf("%d! = %ld\n",1, fact);}else{::printf("%d! = %ld\n", n, fact);}return fact;}