Sentinels and nested if

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D

    #1

    Sentinels and nested if

    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
  • Victor Bazarov

    #2
    Re: Sentinels and nested if

    D wrote:[color=blue]
    > 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[/color]

    Sure you can.

    Comment

    • D

      #3
      Re: Sentinels and nested if

      Victor Bazarov <v.Abazarov@com Acast.net> wrote in message news:<Bmzdd.599 2$Ae.4495@newsr ead1.dllstx09.u s.to.verio.net> ...[color=blue]
      > D wrote:[color=green]
      > > 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[/color]
      >
      > Sure you can.[/color]

      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

      Comment

      • Victor Bazarov

        #4
        Re: Sentinels and nested if

        D wrote:[color=blue]
        > Victor Bazarov <v.Abazarov@com Acast.net> wrote in message news:<Bmzdd.599 2$Ae.4495@newsr ead1.dllstx09.u s.to.verio.net> ...
        >[color=green]
        >>D wrote:
        >>[color=darkred]
        >>>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[/color]
        >>
        >>Sure you can.[/color]
        >
        >
        > 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.[/color]

        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

        Comment

        Working...