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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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 theymight enter. Keep in mind, though, whatever you define, some jerk just might enter too many characters.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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 ). 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' ;
}
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287