-
Help with a program
Hello once again,
I'm trying to write a program that reads input until a '@' is typed in...also, I want to echo the input only without digits, and also convert any uppercase letters to lowercase and vice versa...I have this so far but I'm getting a couple of errors and I'm curious why....any help would be great. Thanks-Chap
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cin.get(ch);
while (ch != '@')
{
cout << ch;
if (isdigit(ch))
cout << " ";
else if (tolower(ch))
cout << ch;
else (toupper(ch))
cout << ch;
cin.get(ch);
}
cout << "Done\n";
cin.get();
cin.get();
return 0;
}
-
> tolower(ch)
You want
if ( islower(ch) ) ch = toupper(ch);
-
You are outputting the character twice inside the while loop. I don't think the first cout << ch; needs to be there.
The number they type will show up when they type it, though, so if you want it to not show up as they type that is a much bigger problem.
-
I try to edit it , now it's work:
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while (true)
{
cout << "Please enter a letter (enter @ to quit): " << flush;
cin >> ch;
if ( ch == '@') break;
if (isdigit(ch)) cout << " ";
if (islower(ch)) cout << (char) toupper(ch) << endl;
if (isupper(ch)) cout << (char) tolower(ch) << endl;
}
cout << "Done\n";
cin.get();
cin.get();
return 0;
}
EDIT:
//#include <cctype> not add this head , prog also can work.