Hi, I've been learning C++ for about 8 weeks now and I'm having a little problem with an exercise I've been doing, the program in itself is very simple, take a string from the keyboard display information about its length and display it in forward and reverse oreder, no problem.
The problem I have come across is that when I put it into a while loop to make the program run again if 'y' is entered and terminate if 'n' is entered, if 'n' is entered it terminates but if 'y' is entered, the second time the code runs (and subsequent) times it seems to skip past the cin.getline command and just print out a length of nothing and print out the forward and reverse of nothing, here is my code.
I tried to solve the problem by intitializing the string with null values but that hasn't solved the problem, I guess it's something to do with that line of code as it just seems to ignore it, I want the program to continue until the iser signals its end by entering 'n' or 'N', any help is greatly appreciated.Code:#include <iostream.h> #include <string.h> #include <conio.h> #include <mem.h> #define NULL '/0' void arraylength (void); void main() { arraylength(); } void arraylength (void) { char array[101]; char yesno; while (yesno!='n'&&yesno!='N') { memset (array, NULL, sizeof(array)); clrscr(); cout<<"Enter a sentence up to 100 hundred characters.\n\n"; cin.getline(array,100,'\n'); cout<<"\nLength: "<<strlen(array)<<"\n"; cout<<array<<"\n"; cout<<strrev(array)<<"\n\n"; cout<<"Again?: "; cin>>yesno; } cout<<"\n\n"; cout<<"Thankyou Bye."; }
Thanks.
Ben Leedham.