Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sentinels and nested if

Reply
Thread Tools

Sentinels and nested if

 
 
D
Guest
Posts: n/a
 
      10-20-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-20-2004
D wrote:
> 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.
 
Reply With Quote
 
 
 
 
D
Guest
Posts: n/a
 
      10-21-2004
Victor Bazarov <(E-Mail Removed)> wrote in message news:<Bmzdd.5992$(E-Mail Removed)09.us.to. verio.net>...
> D wrote:
> > 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.


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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-21-2004
D wrote:
> Victor Bazarov <(E-Mail Removed)> wrote in message news:<Bmzdd.5992$(E-Mail Removed)09.us.to. verio.net>...
>
>>D wrote:
>>
>>>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.

>
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments