By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Next Character

P: 4
Hi,

I'm very new to c++. I need to write a prgrm that convert each character received into the next character in the alphabet. As a special case convert z to a. Here what I wrote so far.
appreciate your suggestions.
Thanks

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main(void)
  4. {
  5.     int a;
  6.     char b;
  7.     cout <<"Enter an alphabet character: ";
  8.     cin>>a;
  9.     if (a=='z')
  10.         {
  11.             b='a';
  12.         }
  13.  
  14.     else
  15.         b=int(a)+1;
  16.     cout<<b<<endl;
  17.     return 0;
  18. }
  19.  
Mar 11 '07 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916
What you have written doesn't compile, this

int(a)

is not the syntax for a cast.

Additionally you code assumes that 'b' = 'a' + 1. The C and C++ standards do not guarantee this so unless you can be sure that the execution character set is one where this is true (like ASCII for instance) and will always be true (it is specified as being ASCII) then you can not rely on 'b' == 'a' + 1 being true.

There are a number of character sets out there where 'b' != 'a' + 1 is true.
Mar 12 '07 #2

P: 4
It didn't help much, but thnx anyway. The prgrm compile just fine in MV C++, but instead of the next alphabet, I get the next ASCII.
I hope that another expert would explain it better .

What you have written doesn't compile, this

int(a)

is not the syntax for a cast.

Additionally you code assumes that 'b' = 'a' + 1. The C and C++ standards do not guarantee this so unless you can be sure that the execution character set is one where this is true (like ASCII for instance) and will always be true (it is specified as being ASCII) then you can not rely on 'b' == 'a' + 1 being true.

There are a number of character sets out there where 'b' != 'a' + 1 is true.
Mar 12 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
What you have written doesn't compile, this

int(a)

is not the syntax for a cast.
Sorry my mistake, it is not a valid cast but is does instantiate a new integer with a value of a which and returns it resulting in a similar action.

If you are wondering why your program is not working as expected it is because you have declared a as an int, when the cin executes if you enter a letter since with can not be converted to an integer (because that would require digits) not conversion happens and a is assigned an error value (or not assigned anything).
Mar 12 '07 #4

P: 4
I agree with you, and when use char instead of int, I get the exact result. But the instructor asked to use this form : b=int(a)+1, and I just can't figure it out.
Thnx

Sorry my mistake, it is not a valid cast but is does instantiate a new integer with a value of a which and returns it resulting in a similar action.

If you are wondering why your program is not working as expected it is because you have declared a as an int, when the cin executes if you enter a letter since with can not be converted to an integer (because that would require digits) not conversion happens and a is assigned an error value (or not assigned anything).
Mar 12 '07 #5

P: 4
This is the prgrm where I declared a as char. But as I mentioned before, one of the requirement is to use b=int(a)+1

char a;
char b;
cout<<"This program converts each input character \n";
cout<<"into the next alphabet character\n\n";
cout<<"Enter an alphabet character: ";
cin >>a;
if (a=='z')
{
b='a';
}
else
b= toascii(a)+1;
cout << "The next alphabet character to "
<<a<< " is "<< b <<endl ;
}




I agree with you, and when use char instead of int, I get the exact result. But the instructor asked to use this form : b=int(a)+1, and I just can't figure it out.
Thnx
Mar 12 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
I agree with you, and when use char instead of int, I get the exact result. But the instructor asked to use this form : b=int(a)+1, and I just can't figure it out.
Thnx
I have no idea what you instructor is driving at since in this program

b = a + 1;

works fine.
Mar 12 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.