I have this Lab for a class that I am taking.. It works as it suppose to... The problem I am having is that if I enter more than 11 char it goes all crazy.. I don't want it to do that...

#include <iostream>
#include <cstring> 

using namespace std; 

void cStringToUpper(char letters[]); 
const int numLetters = 12; 

int main()			

{	
	char letters[numLetters];	
	char cont;	
	bool contProg = false; 

	do		
	{	
		for (int ct = 0; ct < numLetters; ct++) 
		{
			letters[ct] = '\0';
		} 

		cout << "Please enter up to 11 letters,:\n" << endl;

		cStringToUpper(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 = true;					
			cout << "'" << cont << "' is not a valid request!\n"
				"Defaulting to main question:\n" << endl;			

		}		
	
	}while(contProg = true);
}
 
void cStringToUpper(char letters[])
{		
	int loopCount; 		
	cin.getline(letters, numLetters);
	letters[11]= '\0';
		cout << "\n" << endl; 		
	for (loopCount= 0; loopCount < numLetters; loopCount++)		
	{ 			
		if((letters[loopCount] >= 97) && (letters[loopCount] <= 122)) 				
		{					
			letters[loopCount]-=32;	        					
			cout <<  letters[loopCount];          				
		}		
		else
		{
			cout <<  letters[loopCount];
		}
	}
}

First, tell us what you mean by it goes crazy? (I think I know, but you need to clarify the problem.)

Your function should not be doing input or output. Use getline( ) in main(), and pass the string to the function. Keep in mind the job of a function should be limited, ideally to a single task.

Is it necessary for you to place the NULL terminator at the end of the string array in your function after getting the input? Should you ever have to place the NULL after using getline()?

And when transforming the string you don't have to do it manually, you can do it in a for-loop instead and just copy every element in the array with toupper().

for(unsigned i=0; i<strlen(yourString); i++){
   yourString[i] = toupper( yourString[i] );
}

This will also work for any length string, but not for the extended ascii signs (åäö..).

If I enter 12 or more letters. It goes into a loop and I have to close it out. I wantto be able to enter a lot of letters and for it to only process the first 11 letters...

If I use the toupper will the ! stay as a !? So if I enter the word
23fju$%%lkl
It will be
23FJU$%%LKL?
What I have does that right now.

And when transforming the string you don't have to do it manually, you can do it in a for-loop instead and just copy every element in the array with toupper().

for(unsigned i=0; i<strlen(yourString); i++){
   yourString[i] = toupper( yourString[i] );
}

This will also work for any length string, but not for the extended ascii signs (åäö..).

1. The input string entered by the user can have no white space (but, as always with cin, a <CR> is required to enter it).
2. Use a global constant to limit the number of characters the user can enter to 11 (not including the <CR> required by cin or the null terminator for the C-style string).
3. Assume the user does not enter more than 11 characters.
4. Create and use a function called cStringToUpper that receives the C-style string and changes all the lower-case letters to upper case. It should execute a while loop to test each character in the string one at a time until the null terminator is detected and, if the character is a lower case letter, change it to upper case with an arithmetic operation on its code.
5. Write the program so that it repeats and asks the user to enter another line of text or just the letter Q (in upper case) to quit.

For testing...
Test 1: zarathustra (maximum length and has both first and last letters of the alphabet).
Test 2: 2beOr!2Be (less than maximum and has non-letter characters and mixed case letters).
Test 3: Enter upper case Q to quit

Which I got that to work... I just like to make a clean program and I don't like that it loops when I enter more letters than needed...

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.