User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,584 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,585 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3283 | Replies: 6
Reply
Join Date: Oct 2007
Posts: 46
Reputation: helixkod is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

converting from char to ASCII to char

  #1  
Nov 2nd, 2007
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'
Last edited by helixkod : Nov 2nd, 2007 at 1:32 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: converting from char to ASCII to char

  #2  
Nov 2nd, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 46
Reputation: helixkod is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

Re: converting from char to ASCII to char

  #3  
Nov 2nd, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 46
Reputation: helixkod is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

Re: converting from char to ASCII to char

  #4  
Nov 2nd, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: converting from char to ASCII to char

  #5  
Nov 2nd, 2007
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.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: converting from char to ASCII to char

  #6  
Nov 2nd, 2007
Originally Posted by helixkod View Post
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.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: converting from char to ASCII to char

  #7  
Nov 2nd, 2007
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/c...e/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.
  1. #include <iostream>
  2. #include <cctype> // for toupper
  3. using namespace std;
  4. int main()
  5. {
  6. int const MAX_ROWS = 100 ;
  7. int const SIZE = 30;
  8. char words[MAX_ROWS][SIZE];
  9. cout << "Please enter a few words and i will change every letter into caps.\n"
  10. "enter a new line after each word.\nend the input with an eof.\n";
  11.  
  12. int num_words = 0 ;
  13. while( num_words<MAX_ROWS )
  14. {
  15. cin >> ws ;
  16. if( cin.get( words[num_words], SIZE ) ) ++num_words ;
  17. else break ; // eof
  18. }
  19.  
  20. cout << "Here is a list of the words you entered with the first letter in caps.\n";
  21. for ( int i = 0 ; i<num_words ; ++i )
  22. {
  23. words[i][0] = toupper( words[i][0] ) ;
  24. cout << words[i] << '\n' ;
  25. }
  26. }
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC