Loops


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Loops

  1. #1
    stella Guest

    Loops


    Hello...I am working on a program using a while loop. I have set up my program
    like a multiple choice question with one of the choices being X if I want
    to exit my program. When I select X as my answer to exit the program ...the
    program continues to run and will not exit...I have to select alt,control,delete
    to get it to stop running.

    while (letter == 'X')
    {cout << "Exit program" << endl;

    I realize this is a simple question, but I can't remember how to right the
    while statement to exit the program when the letter 'X' is typed in.
    Also, is X a float?

  2. #2
    jonnin Guest

    Re: Loops


    what you want is

    int main()
    {
    char letter = 0;
    while(letter != 'x')
    {
    do stuff, including reading x, etc.
    }

    return(3.14) //can't resist this stuff for fun ;)
    }

    it will quit if letter is x and continue if not...
    also, exit(0) will kill a program whenever that is called, it that is
    useful to you.





    "stella" <stellac2002@yahoo.com> wrote:
    >
    >Hello...I am working on a program using a while loop. I have set up my

    program
    >like a multiple choice question with one of the choices being X if I want
    >to exit my program. When I select X as my answer to exit the program ...the
    >program continues to run and will not exit...I have to select alt,control,delete
    >to get it to stop running.
    >
    >while (letter == 'X')
    >{cout << "Exit program" << endl;
    >
    >I realize this is a simple question, but I can't remember how to right the
    >while statement to exit the program when the letter 'X' is typed in.
    >Also, is X a float?



  3. #3
    stella Guest

    Re: Loops


    "jonnin" <jonnin@vol.com> wrote:
    >
    >what you want is
    >
    >int main()
    >{
    >char letter = 0;
    >while(letter != 'x')
    > {
    > do stuff, including reading x, etc.
    > }
    >
    >return(3.14) //can't resist this stuff for fun ;)
    >}
    >
    >it will quit if letter is x and continue if not...
    >also, exit(0) will kill a program whenever that is called, it that is
    >useful to you.
    >
    >
    >
    >
    >
    >"stella" <stellac2002@yahoo.com> wrote:
    >>
    >>Hello...I am working on a program using a while loop. I have set up my

    >program
    >>like a multiple choice question with one of the choices being X if I want
    >>to exit my program. When I select X as my answer to exit the program ...the
    >>program continues to run and will not exit...I have to select alt,control,delete
    >>to get it to stop running.
    >>
    >>while (letter == 'X')
    >>{cout << "Exit program" << endl;
    >>
    >>I realize this is a simple question, but I can't remember how to right

    the
    >>while statement to exit the program when the letter 'X' is typed in.
    >>Also, is X a float?

    >



  4. #4
    Victor A. Wagner Jr. Guest

    Re: Loops


    "jonnin" <jonnin@vol.com> wrote:
    >
    >what you want is
    >
    >int main()
    >{
    >char letter = 0;
    >while(letter != 'x')
    > {
    > do stuff, including reading x, etc.
    > }
    >
    >return(3.14) //can't resist this stuff for fun ;)


    if your compiler accepts it, it is broken.
    main returns an int, not a double.
    [deleted]

  5. #5
    stella Guest

    Re: Loops


    I tried that and it still want let me exit the program when I type in the
    letter X.. The program continues to run...run...run...
    char letter=0; is at the beginning of my program and the while (letter !=
    X) is at the end of my program...could that be the problem?
    Thanks




    Victor A. Wagner Jr." <vwagner@seisint.com> wrote:
    >
    >"jonnin" <jonnin@vol.com> wrote:
    >>
    >>what you want is
    >>
    >>int main()
    >>{
    >>char letter = 0;
    >>while(letter != 'x')
    >> {
    >> do stuff, including reading x, etc.
    >> }
    >>
    >>return(3.14) //can't resist this stuff for fun ;)

    >
    >if your compiler accepts it, it is broken.
    >main returns an int, not a double.
    >[deleted]



  6. #6
    Mark Sc. Guest

    Re: Loops


    "stella" <stellac2002@yahoo.com> wrote:
    >
    >I tried that and it still want let me exit the program when I type in the
    >letter X.. The program continues to run...run...run...
    >char letter=0; is at the beginning of my program and the while (letter

    !=
    >X) is at the end of my program...could that be the problem?
    >Thanks
    >
    >
    >
    >
    >Victor A. Wagner Jr." <vwagner@seisint.com> wrote:
    >>
    >>"jonnin" <jonnin@vol.com> wrote:
    >>>
    >>>what you want is
    >>>
    >>>int main()
    >>>{
    >>>char letter = 0;
    >>>while(letter != 'x')
    >>> {
    >>> do stuff, including reading x, etc.
    >>> }
    >>>
    >>>return(3.14) //can't resist this stuff for fun ;)

    >>
    >>if your compiler accepts it, it is broken.
    >>main returns an int, not a double.
    >>[deleted]

    >


    X or x makes a big difference, you have to check for 'x' e.g.
    while ( letter != 'x' ) {
    //do whatever
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center