i must write a program that will convert lower case letters to upper case and vice versa and if a number is entered it must display char is not a letter
here is what ave written but when i run it it does not do the conversion.thanks
//to convert lower case letters to uppercase and vice-versa

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
char ch;

	   cout<<"enter a character";

	   cin>>ch;
{
if((ch>=65)&&(ch<=122))
{
if((ch>=97)&&(ch<=122))
ch=ch-32;

	    cout<<"The upper case character corresponding is"<<endl;}

{if((ch>=65)&&(ch<=90))
ch=ch+32;

	    cout<<"The lower case character corresponding is"<<endl;}


	    cout<<"the character is not a letter"<<endl;
}
	    getch();
}
cout<<"The upper case character corresponding is"<<endl;

you are not outputting ch.

besides this there are some "else" missing.
and you should not use the old headers
K.

>but when i run it it does not do the conversion
You don't print the value of ch after the conversion.

>#include<iostream.h>
This is an old header that is no longer a part of the C++ language. Use <iostream> instead. You also need to consider namespaces, but for now simply adding using namespace std; will suffice.

>#include<conio.h>
This is a nonstandard header that is not supported by all compilers. Even those compilers that do support it offer different contents. That's definitely not conducive to robust and portable programs. Remove this and replace getch with cin.get.

>void main()
main does not, and never has, had a return type of void. The return type of main is int, and anything else is wrong.

>clrscr();
This is one of the functions that is pretty much only supported by Borland. Clearing the screeen when running a console program is almost always antisocial and it serves no useful purpose, so you shouldn't do it.

>{
There's no need to introduce a block before your first if statement. You can safely remove it because it's just clutter.

>if((ch>=65)&&(ch<=122))
Don't use the ASCII values for a test like this, they're not portable at all. A slightly better way would be:

if ( ch >= 'A' && ch <= 'z' )

But there are two distinct issues with this. The first is that between the upper case letters and the lower case letters in the ASCII table, there are special characters starting at 91 and ending at 96 that are not letters. You handle this later, sort of, but a single test is more clear:

if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) )

The second issue is that while you are no longer using ASCII codes in favor of something more portable, you would still be relying on the fact that the letters have contiguous values. This is not true of all character sets, and ASCII is not the only character set out there. While you aren't likely to be using a machine that has something besides ASCII, it's a good idea to get into the habit of programming portably as early as possible.

So what's the easy solution for the second issue? Use the standard library function isalpha in the <cctype> header:

if ( isalpha ( ch ) )

This covers both the first and second issues, but some teachers will freak if you use something that hasn't been taught even if it is far superior to the naive way they were teaching.

>if((ch>=97)&&(ch<=122))
This is where you make the attempt at determining lower case, but the same arguments apply as to the test two lines up. A much better solution would be the standard function islower, also in the <cctype> header.

>ch=ch-32;
Once again, using ASCII code is not portable or obvious. You can do this instead if you don't mind assuming the ASCII character set (because it relies on the letters being contiguous):

ch += 'A' - 'a';

It's a more idiomatic way of saying that you want to convert an ASCII lower case letter to upper case. 'A' - 'a' is -32 in ASCII, so by adding that to ch, you subtract 32 and have the upper case equivalent. The inverse can be done with a similar test:

ch += 'a' - 'A';

Of course, for both tests and even better solution is toupper and tolower from <cctype>. That way your code is portable.

Here is the code with all of the changes I've suggested:

#include <cctype>
#include <iostream>

using namespace std;

int main()
{
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( isalpha ( ch ) ) {
    if ( isupper ( ch ) ) {
      ch = tolower ( ch );
      
      cout<<"The lower case equivalent is "<< ch <<endl;
    }
    else {
      ch = toupper ( ch );

      cout<<"The upper case equivalent is "<< ch <<endl;
    }
  }
  else
    cout<<"The character is not a letter"<<endl;

  cin.get();
}

There is still a problem though. The first call to cin>> probably left a newline in the input stream. Because cin.get stops reading at a newline, it will appear that the call to cin.get is ignored. The solution is to read characters until a newline is reached. You can do it any number of ways, but the simplest is a loop:

while ( cin.get ( ch ) && ch != '\n' )
  ;

Also, we can tighten up the code by removing the assignment to ch when performing the case conversion. We can do this because the new value of ch isn't used except for printing once:

#include <cctype>
#include <iostream>

using namespace std;

int main()
{
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( isalpha ( ch ) ) {
    if ( isupper ( ch ) )
      cout<<"The lower case equivalent is "<< static_cast<char> ( tolower ( ch ) ) <<endl;
    else
      cout<<"The upper case equivalent is "<< static_cast<char> ( toupper ( ch ) ) <<endl;
  }
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

I'm sure you'll notice that I used static_cast<char> for the toupper and tolower calls. The reason for this is because cout automagically determines the type of the object and prints it accordingly. Because toupper and tolower return int, cout will print the ASCII codes rather than the character representations. So the values must be cast to char before they will be printed correctly. This was done implicitly when we were assigning the result of toupper and tolower to ch, but good compilers would give you a warning about assigning from a wider type (int) to a narrower type (char). For the first code to compile cleanly, a cast would still have been needed.

Here is the same code assuming ASCII, without using the useful library functions that your teacher will probably bitch at you for using. ;)

#include <iostream>

using namespace std;

int main()
{
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) ) {
    if ( ch >= 'A' && ch <= 'Z' )
      cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
    else
      cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  }
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

Another way to order the tests to make things slightly clearer would be:

#include <iostream>

using namespace std;

int main()
{
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( ch >= 'A' && ch <= 'Z' )
    cout<<"The lower case equivalent is "<< static_cast<char> ( ch + 'a' - 'A' ) <<endl;
  else if ( ch >= 'a' && ch <= 'z' )
    cout<<"The upper case equivalent is "<< static_cast<char> ( ch + 'A' - 'a' ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

If you aren't allowed to use the functions from <cctype> and you still need something portable then you could write your own. They wouldn't be nearly as efficient though because library functions themselves use nonportable constructs and hide them in a standardized interface that works everywhere. The easiest way to write portable isupper, islower, toupper, and tolower functions would be by searching for the character in a string:

#include <iostream>

using namespace std;

bool is_upper ( char ch )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  for ( int i = 0; upper[i] != '\0'; i++ ) {
    if ( ch == upper[i] )
      return true;
  }

  return false;
}

bool is_lower ( char ch )
{
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; lower[i] != '\0'; i++ ) {
    if ( ch == lower[i] )
      return true;
  }

  return false;
}

char to_lower ( char ch )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; upper[i] != '\0'; i++ ) {
    if ( ch == upper[i] )
      return lower[i];
  }

  return ch;
}

char to_upper ( char ch )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; lower[i] != '\0'; i++ ) {
    if ( ch == lower[i] )
      return upper[i];
  }

  return ch;
}

int main()
{
  char ch;

  cout<<"Enter a character: ";
  cin>> ch;

  if ( is_upper ( ch ) )
    cout<<"The lower case equivalent is "<< to_lower ( ch ) <<endl;
  else if ( is_lower ( ch ) )
    cout<<"The upper case equivalent is "<< to_upper ( ch ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

  // Flush the input stream
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  cin.get();
}

Of course, that's probably beyond the scope of your homework, but it's an entertaining exercise that kills a little bit of time. :)

Comments
damn! a great reply i must say

NICE reply Naru! Or is it Narue? =P Anyways, I have a similar question. Mine should be easier, though ^^;

I just need to make my whole script output all caps. I could have just written my cout's in all caps, but I think he wants me to have a function do it, roflwaffle. Anyway to do this or did I get roflpwnt?

In c++

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
        string str = "How now brown cow.";
        transform(str.begin(),str.end(),str.begin(),toupper);
        cout << str;
        return 0;
}

>NICE reply Naru! Or is it Narue? =P
Thank you. And it's Narue, as you can clearly see by the fact that I had to type the name when I created this account, and I'm perfectly capable of spelling it correctly. :icon_rolleyes:

>roflwaffle. Anyway to do this or did I get roflpwnt?
It's my deepest hope that you're joking. Anyway, since your teacher probably won't accept transform as a solution, have you considered storing the input as a string, looping through the string, and using toupper?

Well no, (and it's a stupid question to begin with) but the question asks that all output be made caps. The question has parts a, b, c, e, and f, where f is the part that asks the caps thing. What this means is I have a ton of cout's for parts a through e, and I was just wonderin' if by chance there was function I could put at the beginning of the script that makes every letter a CAP =P I'm totally new to C++, but if I didn't ask stupid questions, I'd never figure out that they were stupid in the first place ;D

P.S. I'm hecka wierd, so sorry about the roflpwnt's and the roflwaffle's, LOL.
Oh! And why I asked if your name was Naru instead of Narue, was 'cause your little anime character looked alot like Narusegawa, a character from an anime who's name is abbreviated by Naru. Hoho.

>I was just wonderin' if by chance there was function I could
>put at the beginning of the script that makes every letter a CAP
Sorry, unless you anticipated that issue in the first place and set up the proper framework, you're SOL and have to manually convert all of the string literals to use upper case.

>And why I asked if your name was Naru instead of Narue, was
>'cause your little anime character looked alot like Narusegawa
I get that a lot, seeing as how Love Hina is a well known anime/manga.

>a character from an anime who's name is abbreviated by Naru.
Actually, her full name is Narusegawa Naru, so it's not an abbreviation at all. The given name of the character is actually Naru. ;)

Crap! You're right. And to think I thought my knowledge of sappy romantic anime was unsurpassed...

I'll just put my cout's in all caps =P Thanks for the help, hopefully I'll be back with some more lame questions in dah futurezzz!

>And to think I thought my knowledge of sappy romantic anime was unsurpassed...
I'm the queen of anime. :)

Ohhh hoho. Well I'm the self-proclaimed King..... but needless to say the Queen is higher up then the King sometimes, so I'll just bow anyways.

-uberbow-

Simple!!

string *tolow(string *inp){
int i;
        for(i=0;i<(*inp).size();++i){
        (*inp)[i]|=0x20;
        }
return(inp);
}

Edited 6 Years Ago by Nick Evan: Added code-tags

Comments
Take the time to write something worthwhile or just keep your bad posting to yourself.

Simple!!

A reply to a solved, six year old thread without using code-tags. Good job :icon_rolleyes:

Comments
If the thread was resolved, then y do u see my post? Speaks volumes of how sensible the "MODERATOR" is and how gud the website is!! And regarding code-tags, find urself a job in a beauty clinic..U don deserve to be here ;)
This article has been dead for over six months. Start a new discussion instead.