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

R

Rahmi Acar

#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!
 
D

David White

Rahmi Acar said:
#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
 
J

John Harrison

Rahmi Acar said:
#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
 
K

Karl Heinz Buchegger

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 );
}
 
J

Jonathan Mcdougall

#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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top