#include <iostream>
#include <iomanip>
using namespace std;


int main()
{

char again;

do
{
    // Define the varibles
		char letter = 0; 
	
	// Tell them to enter a lower case.
		cout << "Enter an lowercase letter: ";  
		cin >>("%c", &letter);                    

	// Check whether the input is lowercase and
	// make sure it is between "a" and "z" then convert 
	//from lower case to upper case 
		if(letter >= 'a')                    
		 if (letter <= 'z')                
		 {                                  
		  letter = letter - 'a'+ 'A';      
		 cout << "The uppercase: " << letter << endl;
		}
     
	// If it is not an upper case letter. 
		 else
		cout << "Enter a lowercase letter please.\n";

 
	// Run it again
		cout << "Would you like to run it again? [Y/N?] ";
		cin >> again;
		cin.ignore(1, '\n');
		}while (again == 'Y' || again == 'y');
	return 0;
}

It runs perfectly but I need it to have many characters, such as if I put a "kkdkda" when I debug it, it will read "K" not "KKDKDA" and thats what I need it to say. How do I get it to say KKDKDA in my script when it runs?

Recommended Answers

All 8 Replies

You only have one loop rather than two. For a program such as this I would expect an outer loop to manage the number of strings that are processed and an inner loop to handle processing a string:

do {
  char ch;

  while ( cin.get ( ch ) )
    cout<< (char)toupper ( ch ) <<'\n';

  // ...
} while ( toupper ( again ) != 'N' );

you can use the macro toupper() to convert a character to upper case. If you have a whole string to convert then create a loop and convert each charcter

string str = "hello world";
for(int i = 0; i < str.length(); i++)
   str[i] = toupper(str[i]);

In c++, an even easier way is to use the c++ transform() function

#include <alborithm>
#include <string>
using namespace std;

int main()
{
   string str = "hello world";
   transform(str.begin(), str.end(), str.begin(), tupper);
}

I'm curious about this input method

cin >>("%c", &letter);

Looks like you're trying to mix C's scanf( ) method and the C++ cin>>.
While it might seem to be working, in fact what it's doing is reading in and storing as if inputting to a char array, which means that not only is a character being stored, but in fact many are being stored, plus a NULL terminator ( '\0' ) to a variable that was declared as a single char.

You're storing (a lot of) data to memory that's doesn't belong to your variable "letter"

>In c++, an even easier way is to use the c++ transform() function
It's not easier when you do it the correct way:

#include <algorithm>
#include <iostream>
#include <string>

struct UpperCase {
    int operator() ( int ch )
    {
        return toupper ( (unsigned char)ch );
    }
};

int main()
{
    std::string str = "hello world";

    std::transform ( str.begin(), str.end(), str.begin(), UpperCase() );
    std::cout<< str <<'\n';
}

Why so complicated? Because for various annoying and obscure reasons that I've explained before, using toupper directly like you did is non-portable at best and undefined at worst.

commented: Thank you for reposting this. I was lazy one day and had need of one. Even knowing what I was looking for required a bit more effort than I cared for. It is nice when the right version finds its way into more threads. +18
#include <al[B]b[/B]orithm>
#include <string>
using namespace std;

int main()
{
   string str = "hello world";
   transform(str.begin(), str.end(), str.begin(), [B]tupper[/B]);
}

> I think you've made some typos:
> #include <alborithm> I don't know any 'alborithm' lol :P
> I even don't know any 'tupper' :P

You're right jephthah, but the problem is that I gave him wrong rep, it was my fault, I was to fast :angry: ...

it happens to everyone. i covered for you.

it happens to everyone. i covered for you.

And if Ancient Dragon is also doing his contribution it will be complete :P LOL ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.