What i need to do is convert the first letter of ever word into a caps letter. I know that in ASCII the lower case letters are between 97 and 122 and all i need to do it -32 to get the caps version. Now my problem is how do i change that letter to ASCII subtract 32 then print the letter value. This is what i have so far.

#include <iostream>
using namespace std;

//Main
int main()
{
	int count;
	int row;

	cout << "How many words will be inputed?";
	cin >> row;

	int const ROWS = row;
	int const SIZE = 30;
	char word[ROWS][SIZE];

	cout << "Please enter a few words and i will change every letter into caps.";

	for ( count = 0; count < ROWS; count++)
		cin >> word[count];

	cout << "Here is a list of the words you entered with the first letter in caps.";
	for ( count = 0; count < ROWS; count++)
	{
		if (static_cast<int>(word[count][0]) > 97 && static_cast<int>(word[count][0]) < 122)
		{
			
                 ***This is where i need help with the conversion***



		}	
	}
	return 0;


}

im getting a few errors like 'static_cast' : cannot convert from 'char [30]' to 'int'

Recommended Answers

All 6 Replies

C++ doesn't really know the difference between an int and a char: both are just numbers but one is typically 8 bits wide and one is typically 32 bits wide.

I/O routines know that the string of numbers you give it are meant to be printed as ASCII if it is type char, or converted to an ASCII representation of the number if it is type int. That is the only difference.

So, you don't need the static_cast stuff.

Also, a lowercase letter can be made uppercase just by subtracting the difference between an ASCII lowercase and uppercase letter: mychar -= 'a' - 'A'; Assuming ASCII is inherently non-portable (though most PCs do actually use either ISO-8859-1 or ASCII), so you can #include <ctype> to get routines that will do this for you: toupper() and tolower().

Hope this helps.

How will i know what letters to subtract from if im asking for an input i went ahead and changed this:

cout << "Here is a list of the words you entered with the first letter in caps.";
	for ( count = 0; count < ROWS; count++)
	{
		if (word[count][0]) > 97 && (word[count][0] < 122)
			word[count][0] -= (word[count][0] - (word[count][0] -32))
		else 
			cout << word[count]<< " ";
		}	
	}

But im not sure if im doing this right.

After testing the code i did come with the final result i wanted thanks to Duoas. Now my question is, how do i set it so i make an array that i do not know how big it will be since the user will input the info. in other words i have it set currently to a constant of 30 but im not sure how many words the user will use. and because i have it set to 30 there will be 30 loops which that may be too much or too little.

Actually, your array is row arrays of SIZE chars. Just use a std::string instead of an array of char. string word[ row ]; For those that missed the answer to the last thing, mychar can be any lowercase character, but you always subtract ('a'-'A') from it.

Glad to be of help.

After testing the code i did come with the final result i wanted thanks to Duoas. Now my question is, how do i set it so i make an array that i do not know how big it will be since the user will input the info. in other words i have it set currently to a constant of 30 but im not sure how many words the user will use. and because i have it set to 30 there will be 30 loops which that may be too much or too little.

Since you don't know how many characters a user might enter, you just have to make the array large enough to hold a maximum they might enter. Keep in mind, though, whatever you define, some jerk just might enter too many characters.

this will not work at all (assuming you are using standard c++).

int row;
cin >> row;
int const ROWS = row;
int const SIZE = 30;
char word[ROWS][SIZE];

ROWS is not a constant known at compile time.

the right way to start learning c++ is by using std::vector and std::string instead of struggling with arrays and memory management. (these are much harder to handle correctly.) see http://www.research.att.com/~bs/bs_faq.html#how-to-start

to read a string from input, see http://www.research.att.com/~bs/bs_faq2.html#read-string
to read several values, see http://www.research.att.com/~bs/bs_faq2.html#simple-program
to understand why std::vector and std::string are much easier to use, see http://www.research.att.com/~bs/new_learning.pdf

to convert a char to upper case, this

if (word[count][0]) > 97 && (word[count][0] < 122)
      word[count][0] -= (word[count][0] - (word[count][0] -32))

is not needed; it also is not portable. instead use int std::toupper(int) (declared in header <ctype>). see http://www.cplusplus.com/reference/clibrary/cctype/toupper.html

if you are forced to use arrays as you start learning c++ (because of a teacher who should have known better),
a. define a sufficiently large array and keep track of how many elements are actually used
b. use a safer function like istream& get ( char* s, streamsize n, char delim ); eg.

#include <iostream>
#include <cctype> // for toupper
using namespace std;
int main()
{
  int const MAX_ROWS = 100 ;
  int const SIZE = 30;
  char words[MAX_ROWS][SIZE];
  cout << "Please enter a few words and i will change every letter into caps.\n"
          "enter a new line after each word.\nend the input with an eof.\n";
  
  int num_words = 0 ;
  while( num_words<MAX_ROWS )
  {
    cin >> ws ;
    if( cin.get( words[num_words], SIZE ) ) ++num_words ;
    else break ; // eof
  }

  cout << "Here is a list of the words you entered with the first letter in caps.\n";
  for ( int i = 0 ; i<num_words ; ++i )
  {
    words[i][0] = toupper( words[i][0] ) ;
    cout << words[i] << '\n' ;
  }
}
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.