Input limit

Hello. CPnewbie here. I need to make this completely functional code that converts all lower case letter from the user's input to upper case also only allow 11 characters for the input. I would like for when they exceed the limit it to either say "you have exceeded the limit, try again" or just show the first 11 characters only or even just end the program... here is the code,...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/
#include <iostream>

using namespace std;
 
int main() 
{ 
	char input[12] = {'\0'}; // initialize all elements to null 
	char keepgoing = 'Y';
 
	while (keepgoing == 'Y')
	{
		cout << "Enter string (no spaces, 11 character limit):  "; 
        cin >> input; 
 
        int i=0; 
        while (input[i] != '\0')
        { 
                input[i] = toupper(input[i]); 
                i++; 
        } 
 
		cout << "Processed string:  " << input << endl; 
		cout << "Keep going? (Y for Yes, Q for Quit):  ";
		cin >> keepgoing;
		}
	
		cin.ignore(2);
        return 0; 
	
}


THANKS!
Use an std::string instead of a char array.
Include the string class, and use the getlen() function to see how many characters are in the string.
To combine and polish what the two posters above me said:

Use std::string instead of a char array is what firedraco said, and I'm all for that. It's excellent advice IMO. Then, from there, when you need the length, use stringNaem.length(), which returns an unsigned int you can use for comparisons. I think that is what AngelHoof wanted to say.

-Albatross
Last edited on
Tss, don't you dare give me credit for that... This one is all yours Albatross! ;)
When I use the stringName.length() the limit actually goes within the () correct? Is this just a variable I need to place in the main function as a parameter and then reference it in my while loop? How can I place that in my while loop to say basically while the input is less than 11 characters, change all lowercase to uppercase and if it's greater than 11 character input, tell them it has exceeded the maximum input and try again?
Thanks!
Topic archived. No new replies allowed.