Hi again people. I am stuck on this array that converts lower case letters to upper case letters. I need to find out how to make it not display strange characters after the letters are entered. I know it has something to do with the 'null' terminator, but I am lost in how to implement it. I also have to have it parallel instead of perpendicular. Here s my code:

#include <iostream>
#include <cstring>

using namespace std;

void cStringToUpper(int numLetters, char letters[]);

const int numLetters = 11;

int main()			
{
	char letters[numLetters];			//character array
	char cont;	

	do
	{
		cout << "Please enter up to 11 letters." << endl;	
				
		cStringToUpper(numLetters,letters);
	
		cout << " " << endl;
		cout << "Do you wish to enter in another set of"     
                             "letters?\n"	
			     "Press 'Y' to continue or 'Q' to end the program" 
                             << endl;
		cin >> cont;
		cout << "\n" << endl;
		cin.ignore(200,'\n');	
	}
	while(cont == 'y' || cont == 'Y');
	
return 0;
}

void cStringToUpper(int numLetters, char letters[])
{	/*This function will take the users input of letters and convert 
          those letters into a uppercase format.

	  Input: numLetters, letters[] array.
	  Output: The character type in uppercase format.
	  Asumptions: that they enter in a valid character type.
	*/

	int loopCount;
	
		cin.getline(letters, numLetters);
		cout << "\n" << endl;
   
		cout << "Lowercase" << "\t" << "Uppercase\n";
		cout << "---------" << "\t" << "----------\n";
   
for (loopCount= 0;loopCount < numLetters;loopCount++)
{
	cout << "    " << letters[loopCount] << "\t\t";

	if((letters[loopCount]>=97) &&(letters[loopCount] <=122))
                                
        letters[loopCount]-=32;	
        cout << "    " << letters[loopCount]   
               << endl;                                                                 
}
}

Any help would be great.

Member Avatar for iamthwee

Why aren't you using the toupper() function?

No compiler to use at the moment so here's some things you can try doing to see if you can figure out why your code isn't working, assuming you don't want to follow iamthwee's suggestion.

1) be sure there's enough room in letters for the null terminator.
Either try increasing size of letters by one or decrease number of char read in by getline by 1.

2) display letters to the screen after user input to be sure it was read in properly.

3) cast each char in letter to numerical form (ASCII equivalent, whatever) to be sure you have the correct numerical ranges and that the -= 32 does what you think it does.

4) rather than use ignore() on line 28, clear letters, instead of cin, by doing this: letters[0] = '\0';

Sorry people but I cant use toupper() function. The way I have it is the way the assignment asks. I need to display the array like so:

If I entered 'adsgehd,' it would display:

lower case:
adsgehd
Uppercase:
ADSGEHD

The idea behind c-strings is that they end with a '\0' character. You have defined the array to be 11 characters then ask for 11 characters. There is no room for the \0. You must only read 10 characters.

And on a style note, if you ask for the string in main() , you should read the string there. The function should only convert the string.

converts lower case letters to upper case letters.
.
.
not display strange characters

You were displaying all the letters

for (loopCount= 0;loopCount < numLetters;loopCount++)
{ //to display CAPITAL letters
   //You enter inside if block only if it is a small letter.
  //else you don't display the letter itself
  if( (letters[loopCount]>=97) &&(letters[loopCount] <=122)) 
  {
           letters[loopCount]-=32;	        
          cout <<  letters[loopCount]          
  }
}

Thank you all graciously for you help and input! I have come down to this code:

#include <iostream>
#include <cstring>

using namespace std;

void cStringToUpper(int numLetters, char letters[]);

const int numLetters = 11;

int main()			
{
	char letters[numLetters];	//character array
	char cont;
	bool contProg;

		do
		{
			cout << "Please enter up to 11 letters:\n" << endl;
			cStringToUpper(numLetters,letters);
	
			cout << "\n" << endl;
			cout << "Do you wish to enter in another set of letters?\n"	
			        "Press 'Y' to continue or 'Q' to end the program" << endl;
			cin >> cont;
			cout << "\n" << endl;
			cin.ignore(200,'\n');					

			switch(cont)
			{
				case 'Q':
					return 0;
					break;
				case 'y':
				case 'Y':
					contProg = true;
					break;
				default:
					contProg = false;
					cout << "'" << cont << "' is not a valid request!\n"
"                                              Defaulting to main question:\n" << endl;
			}
		}
		while(contProg = true);
}
void cStringToUpper(int numLetters, char letters[])
{	/*This function will take the users input of letters and convert those letters into a
	  uppercase format.

	  Input: numLetters, letters[] array.
	  Output: The character type in uppercase format.
	  Asumptions: that they enter in a valid character type.
	*/

	int loopCount;
	
		cin.getline(letters, numLetters);
		cout << "\n" << endl;
   		   
		for (loopCount= 0; loopCount < numLetters; loopCount++)
		{ 
			if((letters[loopCount] >= 97) && (letters[loopCount] <= 122)) 
				{
					letters[loopCount]-=32;	        
					cout <<  letters[loopCount];          
				}
		}
}

Its is not perfect, but it will do.

Is there a way that I can put a error check if someone enters in more than 11 letters. I tried putting one but the compiler says that it can not change 'char' to 'int'.

Suppose,instead of using

cin.getline(...)//the failbit is set if input
//exceeds max numbercharacters allocated

you manually accept each letter,you can always count the number of characters entered inclusive of white space

int count =0;//This counts the number of characters
 char t[9+1];//MAX=9 characters + '\0'
 char ch;
  while( ( ch=cin.get() )!='\n' )
  {
    if(count>8)
     {
      cout <<"Limit exceeded\n" ;
      cout <<"A few characters will be truncated\n";
      break;
     }
   t[count]=ch;
   count++;
  }
  t[count]='\0';
  cout<< endl <<" The input is:";
  cout << t;
//Even a for loop will do
/*
int count =0;;//This counts the number of characters
 char t[9+1];//MAX=9 characters + '\0'
 char ch;
  for(count =0; ( ch=cin.get() )!='\n'; count++ )
  {
    if(count>8)
     {
      cout <<"Limit exceeded\n" ;
      cout <<"A few characters will be truncated\n";
      break;
     }
   t[count]=ch;

  }
  t[count]='\0';
*/

The

string

datatype handles such things better.

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.