User:Lucky Weerasekara

Hi I'am Lakruwan Weerasekara (Lucky) from Kandy Sri Lanka.

Software Development....

WRITING TYPICAL LOOPS You use a loop any time you want to repeat actions. In addition to displaying a message multiple times, several programming situations commonly use loops. One situation where a loop is useful is in an interactive program, when you want to force the user to enter specific values so you are assured the values are valid before proceeding with the program. Another common situation is one where the user should enter hundreds or thousands of similar records for processing.

The limitation to the approach in Figure 4-5 is that any response other than ‘T’ is assumed to be false. If the user enters a ‘t’, ‘R’, ‘X’, or any other character, the if statement displays “You responded False”. In some situations, this might be an acceptable outcome. However, perhaps the user did not intend the response to be “False”; maybe the user made a typographical error. To be more certain of the user’s intention, you can force the user to specifically enter a ‘T’ or an ‘F’ before proceeding. One approach would be to use code like that in Figure 4-6. In this example, after the user enters a response, it is tested, and if it is not ‘T’ or ‘F’, the user is prompted a second time and given another chance to enter an appropriate response. Then the program continues.

cin >> userResponse; if(userResponse != 'T' && userResponse != 'F') { cout << "Invalid response. Please enter a T or an F" << endl; cin >> userResponse; } if(userResponse == 'T') cout << "You responded True" << endl; else cout << "You responded False" << endl;

STRINGS CREATED AS ARRAYS OF CHARACTERS In C++ an array of characters and a string can be declared in the same way. You might create an array of characters if you need to store six one-letter department codes, or five one-letter grades that can be assigned on a test. These examples are called character arrays. When you want to store related characters that are used together, such as someone’s name, then the characters constitute a string. You also can store the individual characters of a string in an array. However, C++ programmers do not refer to an array of characters as a string unless the last usable character in the string is the null character. The null character is represented by the combination ‘\0’ (backslash and zero), or by using the constant NULL that is available if you use the statement #include at the top of any file you compile.

char firstName[] = "Mary"; char firstName[] = {"Mary"}; char firstName[5] = "Mary"; char firstName[5] = {"Mary"}; char firstName[5] = {'M', 'a', 'r', 'y', '\0'};

Each statement reserves five character positions, and stores an ‘M’ at firstName[0], an ‘a’ at firstName[1], an ‘r’ at firstName[2], a ‘y’ at firstName[3], and a ‘\0’ at firstName[4]. (When you store a string, the curly braces are optional, but when you store individual characters, you must include them.) Assuming the compiler assigns the firstName array to memory address 2000, and assuming that character variables occupy one byte of storage in your computer system, Figure 5-28 shows how firstName looks in memory.