iccaros Posted May 29, 2005 Report Share Posted May 29, 2005 ok here is my final project usign the string funtions.plus I found a search code I am not sure what it does.. and some explanation would be helpfulhere is the program //extra credit#include<iostream>#include<cctype>#include<string>using namespace std;//function prototypesvoid clear_screen();string Check (const string& );string make_lower(const string& );string check_Punctuation(const string& , const string& );//main functionint main(){ //local varables char LineToCheck[255]; string TestInput,SetToTest; clear_screen(); //get input and hold it in char LineToCheck, convert to string to edit and check cout << "Enter Line that could be a palendrome:" << endl; //get input in form required can recive up to 255 char cin.get(LineToCheck,255); //place input into string to process TestInput = LineToCheck; SetToTest = Check(TestInput); //compair original input - punctuation and capitialization aginst //input reverst using the inherent string class reverses if ( SetToTest == string(SetToTest.rbegin(), SetToTest.rend()) ) cout << "your input is a palendrome" << endl; else cout << "your input is not a palendrome" << endl; return 0;} //------Clear Screen--------------------------------------------------------void clear_screen(){#if defined WIN32system ("cls");#elif defined UNIXsystem ("clear");#endif}//------Check loop----------------------------------------------------------string Check (const string& s){//create loop local stringsstring punctuation(",;:.?!'\" ");string TestString;string LowerCheck (s);//make all char lower caseLowerCheck = make_lower(LowerCheck);//remove puctuationTestString = check_Punctuation(punctuation, LowerCheck);return TestString;}//------make lower case-----------------------------------------------------string make_lower(const string& s){ string holder(s); for (int i = 0; i < s.length( ); i++) //tolower is part of the cctype lib returns lowercase version of c if there is one, // otherwise it returns the character unchanged. holder[i] = tolower(s[i]); return holder;}//remove Punctuation funtionstring check_Punctuation(const string& punct, const string& Holder){ //funtion only string string no_punctuation; //set variables to hold length of string for loops int holder_length = Holder.length( ); int punct_length = punct.length( ); for (int i = 0; i < holder_length; i++) { //create a 1 char string to hold the test canadate string SingleChar = Holder.substr(i,1); //Find location of successive characters that are in string punct that int locationOfChar = punct.find(SingleChar, 0); // SingleChar not in punct, so keep it if (locationOfChar < 0 || locationOfChar >= punct_length) no_punctuation = no_punctuation + SingleChar; } return no_punctuation;}this compiles and runs from G++ and Dev_c++ windows but in VC++ I get this errorerror C2665: 'basic_string<char,struct std::char_traits<char>,class ::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' : none of the 7 overloads can convert parameter 1 from type 'class std::reverse_iterator<char *,char,char &,char *,int>'Error executing cl.exe.I don't know why this is wrong as on the MS developer site it show the same example.also this is the code I found for punctuation.. I undersand most of it.. but this part if (locationOfChar < 0 || locationOfChar >= punct_length) no_punctuation = no_punctuation + SingleChar;make no sence.. at least not the or part.. I can see if location of punct = 0 then its not a punctuation but the or location being greater or = to the length of punct ...im lost..thanks Quote Link to post Share on other sites
jcl Posted May 29, 2005 Report Share Posted May 29, 2005 (edited) this compiles and runs from G++ and Dev_c++ windows but in VC++ I get this errorerror C2665: 'basic_string<char,struct std::char_traits<char>,class ::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' : none of the 7 overloads can convert parameter 1 from type 'class std::reverse_iterator<char *,char,char &,char *,int>'Error executing cl.exe.I don't know why this is wrong as on the MS developer site it show the same example.It compiles without complaints with the Visual C++ Toolkit 2003.also this is the code I found for punctuation.. I undersand most of it.. but this part if (locationOfChar < 0 || locationOfChar >= punct_length) Â Â Â Â no_punctuation = no_punctuation + SingleChar;make no sence.. at least not the or part.. I can see if location of punct = 0 then its not a punctuation but the or location being greater or = to the length of punct ...im lost..find() is defined as returning sting::npos if the search fails. string::npos is defined as a value of type string::size_type, initialized according to the standard to a value -1. size_type is unsigned, so -1 produces the largest value of size_type (-1 is all-bits-one). The left-hand side of the || handles the case where locationOfChar is interpreted as a signed value (-1) and the right-hand side handles the case where it's an unsigned value.That expression could be replaced byif (locationOfChar != -1)or something similar, but this would be preferablestring::size_type locationOfChar;// ...if (locationOfChar != string::npos)because it makes the intent clear and ensures that that string::npos can be represented by locationOfChar. On a typical 64-bit (I32LP64) platform, size_type will be wider than int, so assigning the return value of find() to an int will result in truncation. Edited May 30, 2005 by jcl Quote Link to post Share on other sites
iccaros Posted May 30, 2005 Author Report Share Posted May 30, 2005 so in either case its looking that it did not find anthing contained in string punct..I'll re-read the string and cctype funtions with this explanation .. see if I can make better sence..I'll also try your method and see if I can play with it.. thanks..as to not compiling.. for school we have to use VC++ V6 introductory edition version. so its old.. Quote Link to post Share on other sites
jcl Posted May 30, 2005 Report Share Posted May 30, 2005 (edited) as to not compiling.. for school we have to use VC++ V6 introductory edition version. so its old..Ah. That version might not have the needed ctor.Incidentally, VC++ didn't conform to ISO C++ until very recently so if you continue to use it you may find yourself adapting a lot of code. The Mozilla C++ portability guide is worth looking over. Edited May 30, 2005 by jcl Quote Link to post Share on other sites
iccaros Posted June 1, 2005 Author Report Share Posted June 1, 2005 I have been gong over hte guid.. now my brain herts.. I have lots to learn..but have one more c++ calss before they let me take jave ... hmm wite once run everywere.... well everywhere with a VM that is.. Quote Link to post Share on other sites
jcl Posted June 1, 2005 Report Share Posted June 1, 2005 but have one more c++ calss before they let me take jave ...hmm wite once run everywere....well everywhere with a VM that is.. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.