Sentinels and nested if

D

D

Can I use a sentinel with a while loop and a nested if statement in a
program?
For example I ask the user for a letter of the alphabet and the output
returns a number(non-ascii). Then the program continues to loop until
the sentinel character is used. Thanks
 
V

Victor Bazarov

D said:
Can I use a sentinel with a while loop and a nested if statement in a
program?
For example I ask the user for a letter of the alphabet and the output
returns a number(non-ascii). Then the program continues to loop until
the sentinel character is used. Thanks

Sure you can.
 
D

D

Victor Bazarov said:
Sure you can.

Then my coding is off.

cout <<"This program is designed to convert an upper case letter of
the alphabet ";
cout << "and convert it into a number that corresponds to a number on
a standard ";
cout << "telephone dial.\n";
cout <<"When you are finished please enter a '?' to stop the
program.\n";
cout <<"Enter your character " << counter << ":";
cin >> ch;
while (ch != '?')
{
if (ch >= 'A' && ch <= 'C')
num = 2;
else if (ch >= 'D' && ch <= 'F')
num = 3;
..
..
..I purposely omitted the rest of the code for the sake of saving space
..
..
cout <<"The number for that character is " << num << endl;
cout << "Enter your character " << ++counter << ":"<< endl;
cin >>ch;


My variables are:
char ch;
int num, counter = 1;

Now the loop iterates only once then quits even though the sentinel is
not entered. I was reading in one book that I can reference the
sentinel again in an if statement but that would be redundant. Any
suggestions on were I'm coding wrong? Thanks.
D
 
V

Victor Bazarov

D said:
Then my coding is off.

cout <<"This program is designed to convert an upper case letter of
the alphabet ";
cout << "and convert it into a number that corresponds to a number on
a standard ";
cout << "telephone dial.\n";
cout <<"When you are finished please enter a '?' to stop the
program.\n";
cout <<"Enter your character " << counter << ":";
cin >> ch;
while (ch != '?')
{
if (ch >= 'A' && ch <= 'C')
num = 2;
else if (ch >= 'D' && ch <= 'F')
num = 3;
.
.
.I purposely omitted the rest of the code for the sake of saving space
.
.
cout <<"The number for that character is " << num << endl;
cout << "Enter your character " << ++counter << ":"<< endl;
cin >>ch;


My variables are:
char ch;
int num, counter = 1;

Now the loop iterates only once then quits even though the sentinel is
not entered. I was reading in one book that I can reference the
sentinel again in an if statement but that would be redundant. Any
suggestions on were I'm coding wrong? Thanks.

With all due respect to your effort, there is no way to make a suggestion
or arrive to a conclusion without seeing the complete code. Remove all
irrelevant things (like extra output and additional processing) and post
the code. Let's make sure you can convert A, B, or C to 2 and C, D, or E
to 3 (just like you showed here). Adding the rest later should be easy
enough.

So, post the _minimal_ complete code that you have and we can discuss it.
Try not to use "..." or "I purposely omitted...". The code should compile
straight from the newsgroup posting.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top