how may in hell may i take advantage of a IF statement in two separate functions? like quit anytime

Discussion in 'C++' started by Rahmi Acar, Jul 25, 2003.

  1. Rahmi Acar

    Rahmi Acar Guest

    #include <iostream.h>
    int main()
    {
    char nCh;
    cout << "type t for test rutine q for quit\n\n";
    cin >> nCh;
    if (nCh == 't')
    // type q to quit any time in this example.
    {
    cout << "Here comes the test rutine type q for quit\n\n";
    main();
    }
    else
    if (nCh == 'q')
    {
    cout << "Good bye\n\n";
    }
    return 0;
    }
    i will be thank full for any respons!
     
    Rahmi Acar, Jul 25, 2003
    #1
    1. Advertising

  2. Rahmi Acar

    David White Guest

    Rahmi Acar <> wrote in message
    news:YWZTa.19516$KF1.307859@amstwist00...
    > #include <iostream.h>
    > int main()
    > {
    > char nCh;
    > cout << "type t for test rutine q for quit\n\n";
    > cin >> nCh;
    > if (nCh == 't')
    > // type q to quit any time in this example.
    > {
    > cout << "Here comes the test rutine type q for quit\n\n";
    > main();
    > }
    > else
    > if (nCh == 'q')
    > {
    > cout << "Good bye\n\n";
    > }
    > return 0;
    > }
    > i will be thank full for any respons!


    Your code worked for me. That is, it displayed the appropriate message for
    the inputs 't' and 'q', and it exited the program following the message for
    'q'. But since, evidently, it is illegal to call 'main' recursively, you
    should move everything to another function.

    A loop would seem far more appropriate for what you are doing than
    recursion. What exactly are you trying to do and what went wrong?

    DW
     
    David White, Jul 25, 2003
    #2
    1. Advertising

  3. "Rahmi Acar" <> wrote in message
    news:YWZTa.19516$KF1.307859@amstwist00...
    > #include <iostream.h>
    > int main()
    > {
    > char nCh;
    > cout << "type t for test rutine q for quit\n\n";
    > cin >> nCh;
    > if (nCh == 't')
    > // type q to quit any time in this example.
    > {
    > cout << "Here comes the test rutine type q for quit\n\n";
    > main();
    > }
    > else
    > if (nCh == 'q')
    > {
    > cout << "Good bye\n\n";
    > }
    > return 0;
    > }
    > i will be thank full for any respons!
    >


    And where are the two seperate functions?

    If you have two functions and you want to put an if statement in both, then
    put an if statement in both.

    You can't have one if statement working in two different functions, you need
    two if statements.

    john
     
    John Harrison, Jul 25, 2003
    #3
  4. Re: how may in hell may i take advantage of a IF statement in twoseparate functions? like quit anytime with button Q

    Rahmi Acar wrote:
    >


    maybe you are looking for something like this:

    bool foo()
    {
    char c;

    cin >> c;

    if( c == 'q' )
    return false;

    ...

    return true;
    }

    int main()
    {
    char c;
    bool continue = true;

    do {
    cout << "Enter code";
    cin >> c;

    if( c == 't' )
    continue = foo();

    else if( c == 'q' )
    continue = false;

    } while( continue );
    }


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 25, 2003
    #4
  5. On Fri, 25 Jul 2003 01:26:39 +0200, "Rahmi Acar"
    <> wrote:

    >#include <iostream.h>
    >int main()
    >{
    > char nCh;
    > cout << "type t for test rutine q for quit\n\n";
    > cin >> nCh;
    > if (nCh == 't')
    >// type q to quit any time in this example.
    > {
    > cout << "Here comes the test rutine type q for quit\n\n";
    > main();
    > }
    > else
    > if (nCh == 'q')
    > {
    > cout << "Good bye\n\n";
    > }
    > return 0;
    >}
    >i will be thank full for any respons!


    I think what you mean is that typing 'q' does not work. The problem
    is with the main() recursion. After typing 't' once, typing 'q' only
    returns from the most nested main() :


    main()
    - type 't'
    ==> main()
    - type 't'
    ==> main()
    - type 'q'
    <===
    - type 'q'
    <===
    type 'q'


    Two things : it is illegal to call main() ; recursion is not the
    solution in this case.

    # include <iostream>

    void test()
    {
    std::cout << "Here comes the test rutine type q for quit\n\n";
    }


    int main()
    {
    char nCh;

    // endless loop
    while (true)
    {
    std::cout << "type t for test rutine q for quit\n\n";

    std::cin >> nCh;

    if (nCh == 't')
    {
    test();
    }
    else if (nCh == 'q')
    {
    std::cout << "Good bye\n\n";
    return 0;
    }
    }

    return 0;
    }

    Jonathan
     
    Jonathan Mcdougall, Jul 25, 2003
    #5
  6. Re: how may in hell may i take advantage of a IF statement in twoseparate functions? like quit anytime with button Q

    Mike Wahler wrote:
    >
    > Karl Heinz Buchegger <> wrote in message
    > news:...
    >
    > > int main()
    > > {
    > > char c;
    > > bool continue = true;
    > >
    > > do {
    > > cout << "Enter code";
    > > cin >> c;
    > >
    > > if( c == 't' )
    > > continue = foo();
    > >
    > > else if( c == 'q' )
    > > continue = false;
    > >
    > > } while( continue );
    > > }

    >
    > Isn't 'continue' a keyword? :)
    >


    Right you are.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 28, 2003
    #6
    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. int
    Replies:
    7
    Views:
    374
    Cowboy
    May 20, 2004
  2. Julia
    Replies:
    2
    Views:
    359
    Julia
    Feb 22, 2005
  3. nikhilketkar
    Replies:
    6
    Views:
    499
    Alex Martelli
    Aug 18, 2007
  4. Visitbazaar Info
    Replies:
    0
    Views:
    943
    Visitbazaar Info
    Mar 31, 2008
  5. Visitbazaar Info
    Replies:
    1
    Views:
    403
    John Timney \(MVP\)
    Mar 31, 2008
Loading...

Share This Page