As others have mentioned in the comments, when you test an element of the string to see if it is a letter, any space character you have in your input will cause that test to fail (for the whole string).
For a quick fix, just allow spaces to pass that test; so, change:
} else if (ch < 'A' || ch > 'z') {
to:
} else if ((ch < 'A' || ch > 'z') && ch != ' ') { // Allow spaces.
However, if you want to learn to use C++ to its full potential, you can improve your code in a number of ways:
- Use the
std::string STL type instead of an old-fashioned, "C-Style" character array.
- Use the standard character-checking/modifying functions like
islower and toupper, rather than relying on manual checks that a character is in a range.1
- Learn the power of range-based
for loops with references to elements
- Try to avoid infinite loops with
break statements, if possible – they can make code harder to read and debug.
Here's a version of your code on which I have applied the above techniques:
#include <iostream>
#include <string>
#include <cctype> // For "islower()", "toupper()", "isspace()" and "isalpha()"
using std::cout;
using std::endl;
using std::cin;
int main()
{
std::string str;
cout << "Enter a word/s: ";
std::getline(cin, str);
bool all_alpha = false; // The "bool" type is best suited for yes/no type data
while (!all_alpha) { // No need for a "break" if we test this each time.
all_alpha = true; // Set this to "true", each loop, until we find otherwise
for (auto& ch: str) { // Use a reference to each character in the string
if (std::islower(ch)) {
ch = static_cast<char>(std::toupper(ch)); // toupper returns an int
}
else if (!std::isalpha(ch) && !std::isspace(ch)) {
all_alpha = false;
}
}
if (!all_alpha) {
cout << "Sorry, that is a wrong input\n";
cout << "Enter a word/s: ";
std::getline(cin, str);
}
}
cout << str << endl; // Only need to show the conversion when the loop is done.
return 0;
}
Sorry if I have been too much of a 'schoolmaster' in my answer, and please feel free to ask for any further clarification or explanation of the points I've raised.
1 Note that your tests for lowercase and uppercase letters, and your conversion from lower to upper, assume ASCII character encoding. Now, although that system is hugely predominant on modern computer systems/compilers, there is no strict requirement in the C++ Standard for it to be used. A platform using EBCDIC encoding, for example, will need very different checking/converting algorithms. (But using the in-built routines will cover that.)