I'm trying to figure out how to not cout a char left blank. In the program it will allow 80 char however if less are given it couts' a strange pattern. How do I stop the pattern from showing? Thanks for the help.
Description:Program lets a user enter up to 80 characters into an array and converts all
the lowercase letters into uppercase. It will not change uppercase letters.
Limitations or issues: limited to 80 char
Credits: Not Applicable
*/
#include <iostream>
usingnamespace std;
int main()
{
constint MIN_LOWERCASE = 97; // Constant for the minimum lowercase ASCII code
constint MAX_LOWERCASE = 122; // Constant for the maximum lowercase ASCII code
constint MIN_UPPERCASE = 65; // Constant for the minimum uppercase ASCII code
constint MAX_UPPERCASE = 90; // Constant for the maximum uppercase ASCII code
constint SIZE = 81; // Constant for the size of a line of input
char line[SIZE]; // Array to hold a line of input
cout << "Enter a string of 80 or fewer characters: ";
cin.getline(line, SIZE);
for (int count = 0; count < SIZE; count ++)
{
if (line[count] <= MAX_LOWERCASE && //change lowercase to uppercase and display
line[count] >= MIN_LOWERCASE)
{
line[count] -= 32;
cout << line[count];
}
elseif (line[count] <= MAX_UPPERCASE && //leave uppercase unchanged and display
line[count] >= MIN_UPPERCASE)
{
cout << line[count];
}
elseif (line[count] == line[count]) //display all other characters
{
cout << line[count];
}
}//end for
system ("pause");
return 0;
}//end code
Enter a string of 80 or fewer characters: Starting Out with C++!
STARTING OUT WITH C++! ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠Press any key to continue . . .
This is an initialization problem. getline places the read characters into the array. If the inputted characters is 80 characters or less, then the remaining characters are uninitialized garbage.
You should initialize the line array to zero: char line[SIZE] = {0};
Then, you should jump out of your print loop if you see a char that's zero.