iccaros Posted May 24, 2005 Report Share Posted May 24, 2005 ok I'm having a problem with the following..the varable CheckHolder is not putting anything out.. it should be a backwords output of LineToCheck and I put some Cout lines to see what it is during processing and it seams to work untill the last line I get blank for CheckHolder#include<iostream>#include <cctype>#include <string>using namespace std;//function prototypes//void clear_screen();//void check(const char);//main functionint main(){ //local varables char LineToCheck[255]; char CheckHolder[255]; int SizeOfChar=0, y = 0; string str; cout << "Enter string:" << endl; cin.get(LineToCheck,255); str = LineToCheck; SizeOfChar = str.length(); cout << "you enterd " << SizeOfChar << " charaters" << endl; for (int x = SizeOfChar; x >= 0; x--) { CheckHolder[y] = LineToCheck[x]; cout << "checkholder = " << CheckHolder[y] << endl; y++; } cout << " Line to check = " << LineToCheck<< endl; cout << "check holder = " << CheckHolder << endl; return 0;}my outputbash-2.05b$ g++ extra.cppbash-2.05b$ ./a.outEnter string:This is a stringyou enterd 16 charaterscheckholder =checkholder = gcheckholder = ncheckholder = icheckholder = rcheckholder = tcheckholder = scheckholder =checkholder = acheckholder =checkholder = scheckholder = icheckholder =checkholder = scheckholder = icheckholder = hcheckholder = T Line to check = This is a stringcheck holder =bash-2.05b$ Quote Link to post Share on other sites
iccaros Posted May 24, 2005 Author Report Share Posted May 24, 2005 to add I can see the output of CheckHolder if I add a for loopfor (int x = 0; x <= SizeOfChar; x++)cout << CheckHolder[x];cout << endl;this is not the a good soultion as I must newt strip out any spaces and puntuation, then make everything lowercase for comparison.. I don't need these answers .. as I need to learn to do them and I can only learn with trial and error.. but this char problem is makeing me mad.. (do I need to read it into a string?) Quote Link to post Share on other sites
jcl Posted May 24, 2005 Report Share Posted May 24, 2005 (edited) The problem is that you're copying the entire string into CheckHolder reversed, including the terminating null character. The null char is inserted at CheckHolder[0], so CheckHolders ends up being a C string of length 0. You need to start copying from the last non-null character and then insert a null at the end of the reversed string.It is a good idea to the use the string class whenever possible. C strings are a legacy feature.Excursus: It's probably beyond the scope of your assignment, but C++ makes this fairly easy if you use more of the features of the standard library:#include <iostream>#include <string>using namespace std;int main(){ Â Â string in; Â Â cin >> in; Â Â cout << string(in.rbegin(), in.rend()) << endl; Â Â return 0;} Edited May 24, 2005 by jcl Quote Link to post Share on other sites
CurlingSteve Posted May 24, 2005 Report Share Posted May 24, 2005 "One Off" errors are very easy to fall into, especially when learning C.A 16 character string for instance is typically looped with:For (x=0;x<16;x++).Beginners typically forget that C arrays are indexed starting at zero, so the scan goes to length - 1 (using the "<" test instead of "<=" is a common way to cover that).And as JCL said, it's very easy to be caught by the terminating zero byte. Quote Link to post Share on other sites
iccaros Posted May 25, 2005 Author Report Share Posted May 25, 2005 The problem is that you're copying the entire string into CheckHolder reversed, including the terminating null character. The null char is inserted at CheckHolder[0], so CheckHolders ends up being a C string of length 0. You need to start copying from the last non-null character and then insert a null at the end of the reversed string.It is a good idea to the use the string class whenever possible. C strings are a legacy feature.Excursus: It's probably beyond the scope of your assignment, but C++ makes this fairly easy if you use more of the features of the standard library:#include <iostream>#include <string>using namespace std;int main(){ string in; cin >> in; cout << string(in.rbegin(), in.rend()) << endl; return 0;} jcl,that captures the first word until the first space.. my problem with that is I need to capture and entire line.. strip out spaces, puctuation and case and see it its the same forwared as it is backwards.. the only ohte rrequirment is I must use cin.get();so now the question.. if I put the input in to a Char first, then move everything but spaces and puncuation into the string I should be half way there right?I am assuming this is putting out the string twice.. once forward (rbegin) and agian (rend) I'll go look up the string class so I understand more.. thanks Quote Link to post Share on other sites
jcl Posted May 25, 2005 Report Share Posted May 25, 2005 (edited) that captures the first word until the first space..Yeah. It was just supposed to incite interest in the standard library.so now the question.. if I put the input in to a Char first, then move everything but spaces and puncuation into the string I should be half way there right?Right. You can combine the editing and reversal in one loop if you wish. Edited May 25, 2005 by jcl Quote Link to post Share on other sites
iccaros Posted May 25, 2005 Author Report Share Posted May 25, 2005 Yeah. It was just supposed to incite interest in the standard libraryit did 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.