Sentinels and nested if

Discussion in 'C++' started by D, Oct 20, 2004.

  1. D

    D Guest

    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
    D, Oct 20, 2004
    #1
    1. Advertising

  2. 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.
    Victor Bazarov, Oct 20, 2004
    #2
    1. Advertising

  3. D

    D Guest

    Victor Bazarov <> wrote in message news:<Bmzdd.5992$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
    D, Oct 21, 2004
    #3
  4. D wrote:
    > Victor Bazarov <> wrote in message news:<Bmzdd.5992$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
    Victor Bazarov, Oct 21, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,080
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    639
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    407
  4. Ultrus
    Replies:
    3
    Views:
    354
    Stefan Behnel
    Jul 9, 2007
  5. Replies:
    3
    Views:
    450
    Victor Bazarov
    Dec 7, 2007
Loading...

Share This Page