<7. Do-While Loops,and ASCII code



In a do-while loop a test occurs at the end. The code below gets characters from the keyboard and writes them into an array until the user presses return. Since '\n' is the C++ code equivalent of pressing enter, you can use '\n' to test if the user pressed the enter key. The != not equal operator can let you know that the enter key has not been pressed.

	while(letter != '\n');

Use a counter (an int variable) to keep track of how many characters the user entered. Since cin.get() will get spaces too, you can put phrases, sentences, or many paragraphs into the array. The ++ after a variable simply adds 1 to the variable.

	count++;

The ++ after count adds 1 to the value of count. Everytime the statement count++; is read in the loop, the value of count goes up by 1. Loops that require input need priming before the loop starts so cin.get(letter); myarray[count] = letter; precedes the loop.


char myarray[2000];
char letter;
int count = 0;
cout << "\nPlease enter your text: "  << flush;
cin.get(letter);
myarray[count] = letter;
do{
	count++;		//add 1 to count
	cin.get(letter);
	myarray[count] = letter;   
}while(letter != '\n');
cout << endl << count;

- ASCII code
ASCII stands for American Standard Code for Information Interchange. ASCII code is how characters are reperesented in the bytes of a computer's RAM or disk space. You should know that computers process everything in one's and zero's. The most basic unit in the computer is a bit. A bit is either on (1) or off (0). Eight bits are used to make a byte. There are 256 different permutations of one's and zero's in a byte. In other words, a byte can hold a value ranging from zero (0000 0000) to 255 (1111 1111). In ASCII code the values from 0 to 127 are used to represented various characters including the digits 0-9, the uppercase alphabet, the lowercase alphabet, and punctuation. The values from 128 to 255 contain special characters and this part of the ASCII code varies between computer brands and operating systems. You can examine a portion of the ASCII code in the table below.

	ASCII  			ASCII			ASCII
	code   character	code	character	code	character
	
	33	!		40	(		47	/
	34	"		41	)		48	0	
	35	#		42	*		49	1
	36	$		43	+		50	2
	37	%		44	,		51	3
	38	&		45	-		52	4
	39	'		46	.		53	5



In C++, a char variable is contained in a single byte. The char() function allows you to select the value of a byte so that it displays the character you want. For example the following code
	cout << char(36)<< char(49)<< char(46)<< char(53)<< char(48);
displays
$1.50
. Refer to the values in the ASCII table above if you are not sure why.

The Do-While loop below displays the digits 0-9 in reverse order. It starts with char(58) which holds the ASCII value for the character 9 (not the actual decimal value of 9). The -- after count is the opposite of ++. The line count--; means decrease the value of count by 1.


count = 58;
do{
cout << char(58) << ", ";
count--;		  // subtract 1 from count;
}while(count > 47);	// tells us to stop when count reaches 47 
(Note: This Do-While loop doesn't require input so no priming is needed.)

Assignment #4
1.Write a program to produce an ASCII table on-screen. Do not print the first 31 characters.
 (Remember to stop at 255.)
2.Write a program that gets the users name and then prints it backward to the screen.