#include <iostream> #define NULL 0 #define MAX_LIMIT 5 //#define MAX_LENGTH 2 bool ComparePC2S(const char *,const std::string &); int main() { const std::string EXIT_STRING="exit"; std::cout<<"Over"<<std::endl; char * pChar(NULL); pChar=new char[MAX_LIMIT]; memset(pChar,‘\0‘,MAX_LIMIT); std::cin.get(pChar,MAX_LIMIT); while(!ComparePC2S(pChar,EXIT_STRING)) { std::cin.sync();//std::cin.ignore(MAX_LENGTH,‘\0‘); std::cout << "Please enter your message: "; memset(pChar,‘\0‘,MAX_LIMIT); std::cin.get(pChar,MAX_LIMIT); } delete [] pChar; return 0; }; bool ComparePC2S(const char * pChar,const std::string & str) { const char * pCharTemp=str.c_str(); if(strlen(pChar)!=strlen(pCharTemp)) return false; int index; for(index=0;index<std::strlen(pChar);index++) { if(pChar[index]!=pCharTemp[index]) return false; } return true; };
std::string compare,布布扣,bubuko.com
原文:http://www.cnblogs.com/hongjiumu/p/3588274.html