// ptr16.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // Simple program that gets strings from the user and displays them back. // Notice that the program crashed at some point. Why? // Key Points: 1) Memory allocation. #include #include int main() { char str1[10], str2[6], ans = 'y'; // Declare 2 strings do { cout << " Enter your first string. It must be less than 10 characters: "; cin >> str1; cout << " Now enter the second one. It must be less than 6 characters: "; cin >> str2; cout << " \n The first sting entered was :" << str1 << "\n"; cout << " and the second one is: " << str2 << "\n"; cout << " Do you want to continue? Enter 'y' if so; any other key otherwise: " ; cin >> ans; } while ( ans == 'y' ); return 0; } /* // The following is the result of compilation //Compilation successful // We executed the program using two different machines. // Here are the results from both of them //================================================================ // The following is the result of running the program on machine 1 //================================================================ Enter your first string. It must be less than 10 characters: this is a test Now enter the second one. It must be less than 6 characters: The first sting entered was :this and the second one is: is Do you want to continue? Enter 'y' if so; any other key otherwise: // The program terminated at this point, since the character entered was not 'y' // Run again. Note that spaces, tabs and new line characters are ignored by ">>" Enter your first string. It must be less than 10 characters: this is y Now enter the second one. It must be less than 6 characters: The first sting entered was :this and the second one is: is Do you want to continue? Enter 'y' if so; any other key otherwise: Enter your first string. It must be less than 10 characters: he llo again Now enter the second one. It must be less than 6 characters: The first sting entered was :hello and the second one is: again Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: hello third time Now enter the second one. It must be less than 6 characters: The first sting entered was :hello and the second one is: third // The program terminated at this point, since the character entered was not 'y' // Run again. Enter your first string. It must be less than 10 characters: this-is-9 Now enter the second one. It must be less than 6 characters: hello The first sting entered was :this-is-9 // This is actually 10, the return key is part of the string and the second one is: hello Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: this-is-11 Now enter the second one. It must be less than 6 characters: How?! The first sting entered was :this-is-11How?! and the second one is: How?! Do you want to continue? Enter 'y' if so; any other key otherwise: n // Run the program again. Watch how strings behave!! Enter your first string. It must be less than 10 characters: it-is-8 Now enter the second one. It must be less than 6 characters: this-is-more-than-6 The first sting entered was :it-is-8 and the second one is: this-is-more-than-6 Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: BY_LUCK! Now enter the second one. It must be less than 6 characters: will-this-time-the-test-pass-too? The first sting entered was :BY_LUCK! and the second one is: will-this-time-the-test-p{°Öz( // This is what we call " garbage " Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: LUCK_#_2 Now enter the second one. It must be less than 6 characters: they-say-Can-not-depend-on-luck-you-may-crash-if-hit-illegal-memory_!!!!! Memory fault(coredump) // Program crashed */ //================================================================ // The following is the result of running the program on machine 2 //================================================================ /* Enter your first string. It must be less than 10 characters: this-is-9 Now enter the second one. It must be less than 6 characters: hello The first sting entered was :this-is-9 and the second one is: hello Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: this-is-11 Now enter the second one. It must be less than 6 characters: How?! The first sting entered was :this-is-11 and the second one is: How?! Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: it-is-8 Now enter the second one. It must be less than 6 characters: this-is-more-than-6 The first sting entered was :more-than-6 and the second one is: this-is-more-than-6 Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: BY_LUCK! Now enter the second one. It must be less than 6 characters: will-this-time-the-test-pass-too? The first sting entered was :s-time-the-test-pass-too? and the second one is: will-this-time-the-test-pass-too? Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: LUCK_#_2 Now enter the second one. It must be less than 6 characters: they-say-Can-not-depend-on-luck-you-may-crash-if-hit-illegal-memory_!!!!! The first sting entered was :-Can-not-depend-on-luck-you-may-crash-if-hit-illegal-memory_!!!!! and the second one is: they-say-Can-not-depend-on-luck-you-may-crash-if-hit-illegal-memory_!!!!! Do you want to continue? Enter 'y' if so; any other key otherwise: y Enter your first string. It must be less than 10 characters: LUCK_#_3 Now enter the second one. It must be less than 6 characters: they-say-Can-not-depend-on-luck-you-may-crash-if-hit-illegal-memory_!!!!!Here-is-a-terribly-long-string-surely-mofre-than-6-characters!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Press any key to continue (Notice that the program did not crash but it did NOT terminate normally since no response was given by the program) */