#include <iostream.h>
int main(void){

char a;
char b;

cim >> b;
b=b-'a'+'A';
cout << b;

return 0;

}

I am like about 2 weeks into programming, so I am pretty simplistic in my programs. If you put that into like a Borland Compiler (not sure if other compilers use exact same format) it will change a lower case letter into an upper case letter. That part I have down through a lot of work (lot of work for me b/c I am not really that good at programming). I want to make it so my program can keep going after one.

Basically, right now, I type in a lower case letter after running the program and it becomes upper case. I want it so that after I get an uppercase letter, I can go to a new line and type another lowercase letter which will become uppercase. I do not want to need to re-run the program. I want to be able to type like "a" and get "A" and then automatically get sent to the next line so I can type like "x" and then get "X". The program always terminates after it converts I letter. I am currently like testing while loop restrictions to get it to work, but i have been overall unsuccessful. Also, I want it to terminate if I don't type in a character. Thanks for your help. I have looked at codes that convert letters, but they are very advanced. My code gets the job done, but is very short b/c I can't really do it any other way.

Member Avatar for GreenDay2001

You could add a simple infinite loop for that, and set a condition that if input character is something other than lower case it would break:

while( 1 ) {
    cin >> b;
    if( (b < 'a') || (b > 'z') )
        break;
    b=b-'a'+'A';
    cout << b;
}

use the standard tolower() function (sometimes its a macro) to convert a character to lower case and it will work on every compiler's implementation

b = tolower(b);

Your program is wrong. iostream.h is not a valid c++ header file, IOSTREAM is.

There is no such thing in C++ as "cim". "cin" is used to input data.

Here I'll give you a code that can change the case of character (assuming that only Alphabets are the input). It'll change the lower to upper and upper to lower.

#include <iostream>
int main(void)
{

char b;
int x = 0;
while ( x < 10 )
{
	std::cout<<"Enter an Alphabet : " ;
	std::cin >> b;

	if (b < 'A' || b > 'Z')
	{
		b = b - 32;
		std::cout << "Lower case : " << b << std::endl;;
	}
	else
	{
		b = b + 32;
		std::cout << "Upper case : " << b << std::endl;
	}
	x++;
}

std::cin.ignore();
std::cin.get();
return 0;

}

Try this out. It'll give the desired result you want. It'll will loop the statements within while 10 times, you can change that as how you please.

Member Avatar for GreenDay2001

> Your program is wrong. iostream.h is not a valid c++ header file, IOSTREAM is.
Right, but maybe his teacher expects him to use old compilers like Turbo C++, like my syllabus and teacher :)

> loop 10 times
He wants the program to be terminated when user wants, so better we place acondition than loop it exactly 10 times

> b = b + 32; Using this would be better practice b=b-'a'+'A'; Since its not necessary that you use ASCII character encoding always.

>Using this would be better practice b=b-'a'+'A'; Since its not
>necessary that you use ASCII character encoding always.
You're splitting hairs when there's no difference at all. Both solutions are non-portable and rely on the setup of the ASCII character set. The latter is a better choice only because it avoids magic numbers.

Member Avatar for GreenDay2001

>Using this would be better practice b=b-'a'+'A'; Since its not
>necessary that you use ASCII character encoding always.
You're splitting hairs when there's no difference at all. Both solutions are non-portable and rely on the setup of the ASCII character set. The latter is a better choice only because it avoids magic numbers.

Ok got it. I reckon standard function in ctype, would be the best practice. But, is there any way to implement a portable function for this??

>But, is there any way to implement a portable function for this??
If you only allow the basic C++ character set, sure:

namespace {
  const char *upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const char *lower = "abcdefghijklmnopqrstuvwxyz";
}

int convert_char ( int c, const char *from, const char *to )
{
  int i = 0;

  while ( from[i] != '\0' && from[i] != c )
    ++i;

  if ( from[i] == '\0' )
    return c;

  return to[i];
}

int to_upper ( int c )
{
  return convert_char ( c, lower, upper );
}

int to_lower ( int c )
{
  return convert_char ( c, upper, lower );
}
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.