exit a function()

  • Thread starter Thread starter SteveDB1
  • Start date Start date
S

SteveDB1

Hi all.
I'm using VS 2008 Express C++.
I created a console application back in 1999, and updated it for VC++ 6.0 in
2001.
I've updated again this past month, and have found enough differences that I
now need to use a different exit routine.
In 2001, I could use a basic escape with a do {}while(ch!= 27);
Now that apparently does not work.
So, how do I exit a function now with C++ 2008 express?

My general form is:

------------------------------------------------------------------
# include "stdafx.h"
# include <iostream>
# include <cmath>
# include <xutility>
# include <stdlib.h>
# include <cctype>
# include <conio.h>
# define PI 3.1415926535898

int n;
int ParA, ParB, ParC, ParD, ParE;

using namespace std;

int functionA (int& ParA, int& ParB, int& ParC, int& ParD)
//ParA through ParE are parameters/variables (please pardon me if I
//incorrectly stated the terms) within my functions.
{
secondary menu to explain how to use this function-A.
do
{
core code for this function.
} while (ch_n != 27);
//return n; //where n is an equation within the core code.
exit(0);
}
int functionB (int& ParA, int& ParB, int& ParC, int& ParD, int& ParE);

secondary menu to explain how to use this function-B.
do
{
core code for this function.
} while (ch_(n+1) != 27);
//the "ch_(n+1) is a character subscript I used for my while escape.

//return n; //where n is an equation within the core code.

exit(0); //this does not work to exit my functions.
}
int functionC (int& ParA, int& ParB);

secondary menu to explain how to use this function-C.
do
{
core code for this function.
} while (ch_(n+2) != 27);
//return n; //where n is an equation within the core code.
exit(0);
}

int main()
{
Do
{
main welcome menu which gives options to access my functions,
or exit the program.

Switch(ch)
{
case '1' : functionA(ParA, ParB, ParC, ParD);
break;
case '2' : functionB(ParA, ParB, ParC, ParD, ParE);
break;
case '3' : functionC(ParA, ParB);
break;
default : exit(0);
}
} while (ch != 27);
}
 
SteveDB1 said:
Hi all.
I'm using VS 2008 Express C++.
I created a console application back in 1999, and updated it for VC++ 6.0
in
2001.
I've updated again this past month, and have found enough differences that
I
now need to use a different exit routine.
In 2001, I could use a basic escape with a do {}while(ch!= 27);
Now that apparently does not work.
So, how do I exit a function now with C++ 2008 express?

My general form is:

------------------------------------------------------------------
# include "stdafx.h"
# include <iostream>
# include <cmath>
# include <xutility>
# include <stdlib.h>
# include <cctype>
# include <conio.h>
# define PI 3.1415926535898

int n;
int ParA, ParB, ParC, ParD, ParE;

using namespace std;

int functionA (int& ParA, int& ParB, int& ParC, int& ParD)
//ParA through ParE are parameters/variables (please pardon me if I
//incorrectly stated the terms) within my functions.
{
secondary menu to explain how to use this function-A.
do
{
core code for this function.
} while (ch_n != 27);
//return n; //where n is an equation within the core code.
exit(0);
}
int functionB (int& ParA, int& ParB, int& ParC, int& ParD, int& ParE);

secondary menu to explain how to use this function-B.
do
{
core code for this function.
} while (ch_(n+1) != 27);
//the "ch_(n+1) is a character subscript I used for my while escape.

//return n; //where n is an equation within the core code.

exit(0); //this does not work to exit my functions.
}
int functionC (int& ParA, int& ParB);

secondary menu to explain how to use this function-C.
do
{
core code for this function.
} while (ch_(n+2) != 27);
//return n; //where n is an equation within the core code.
exit(0);
}

int main()
{
Do
{
main welcome menu which gives options to access my functions,
or exit the program.

Switch(ch)
{
case '1' : functionA(ParA, ParB, ParC, ParD);
break;
case '2' : functionB(ParA, ParB, ParC, ParD, ParE);
break;
case '3' : functionC(ParA, ParB);
break;
default : exit(0);
}
} while (ch != 27);
}


exit() is for terminating the application, not to exit a function.
To exit a function, use return.

Mark
 
Mark,
Thank you for your response.
I've replaced the exit() with "return 0;"
I still have essentially the same problem- it will not allow me to exit the
function.
I can exit the program with a zero, but no keystroke of any key allows me
out of the function, and if I want to exit, I must close the program by
clicking the close 'x' at the top of the cmd console window.
I guess my initial question following the return 0 issue, would I be better
off without using the do-while since the escape key no longer allows me the
out it once did?
Again, thank you for your helps.
 
SteveDB1 said:
Mark,
Thank you for your response.
I've replaced the exit() with "return 0;"
I still have essentially the same problem- it will not allow me to exit
the
function.
I can exit the program with a zero, but no keystroke of any key allows me
out of the function, and if I want to exit, I must close the program by
clicking the close 'x' at the top of the cmd console window.
I guess my initial question following the return 0 issue, would I be
better
off without using the do-while since the escape key no longer allows me
the
out it once did?
Again, thank you for your helps.


I'm missing something....I can't see where/how "ch" gets set or even what
type of variable it is.

Mark
 
Mark,
I apologize for that, I had only listed a basic form.
I did declare it.

char ch, ch1, ch2, ch3;
 
SteveDB1 said:
Mark,
I apologize for that, I had only listed a basic form.
I did declare it.

char ch, ch1, ch2, ch3;


Does it get set with _getch() ??

I don't see anything in your original code that should be different compiled
with a newer compiler.

The problem is your app isn't exiting, correct?
Using the debugger, what value for ch are you getting when you press ESC?

Mark
 
Mark,
_getchar();
I was actually using that before, and for some reason the form ( ch =
_getchar(); ) that I'd had did not work once I imported the old cpp file into
VS 2008 Express. I started posting over on a site called- Daniweb.com before
I began meandering around the newsgroups-- I didn't know that Visual Studio
had a newsgroup forum until two weeks ago, and by then I'd completely rewrote
the original code to its present form. In fact, what I originally posted that
you responded to, I pulled off a post I'd done on the same issue at Daniweb.

To be honest, I've not used the debug tool in this new program before, and
in trying it a few minutes ago, I made it as far as the main menu before it
finished. I think I was doing something wrong.

Ok, I figured out how to step over, and into specific locations. I see where
I'd missed that before.
My thinking at this point now that I've done the debug tool is that in no
previous rendition of this code did I ever have a _getchar(); within my
actual functions. I'm now wondering if I actually need this for each of my
ch_n's within the functions.
In fact, I've always used cin>> ch_n; for my function escape in the past.
I've just changed them to now be- ch_n = _getchar(); for each of my escapes.
(the _n is subscript language used in physics, for this it's just referencing
that n is a substitute for three different numbers. I.e., ch1, ch2, ch3.)

I just tried inserting a ch_n = _getchar(); in each of my functions.

Well, that didn't work. It threw 3 errors, one for each one.
Here's the error line in my output box for the third one.

e:\steve'sdocs\visual studio
2008\projects\steveangle\steveangle\steveangle.cpp(148) : error C3861:
'_getchar': identifier not found

and yes, I do have <conio.h> in my include list.

What else can I use?
Again.... I really appreciate your help.
Best.
 
SteveDB1 said:
Mark,
_getchar();
I was actually using that before, and for some reason the form ( ch =
_getchar(); ) that I'd had did not work once I imported the old cpp file
into
VS 2008 Express. I started posting over on a site called- Daniweb.com
before
I began meandering around the newsgroups-- I didn't know that Visual
Studio
had a newsgroup forum until two weeks ago, and by then I'd completely
rewrote
the original code to its present form. In fact, what I originally posted
that
you responded to, I pulled off a post I'd done on the same issue at
Daniweb.

To be honest, I've not used the debug tool in this new program before, and
in trying it a few minutes ago, I made it as far as the main menu before
it
finished. I think I was doing something wrong.

Ok, I figured out how to step over, and into specific locations. I see
where
I'd missed that before.
My thinking at this point now that I've done the debug tool is that in no
previous rendition of this code did I ever have a _getchar(); within my
actual functions. I'm now wondering if I actually need this for each of my
ch_n's within the functions.
In fact, I've always used cin>> ch_n; for my function escape in the past.
I've just changed them to now be- ch_n = _getchar(); for each of my
escapes.
(the _n is subscript language used in physics, for this it's just
referencing
that n is a substitute for three different numbers. I.e., ch1, ch2, ch3.)

I just tried inserting a ch_n = _getchar(); in each of my functions.

Well, that didn't work. It threw 3 errors, one for each one.
Here's the error line in my output box for the third one.

e:\steve'sdocs\visual studio
2008\projects\steveangle\steveangle\steveangle.cpp(148) : error C3861:
'_getchar': identifier not found

and yes, I do have <conio.h> in my include list.

What else can I use?


_getch instead of _getchar :)

Mark
 
SteveDB1 said:
Mark,
_getchar();
I was actually using that before, and for some reason the form ( ch =
_getchar(); ) that I'd had did not work once I imported the old cpp file
into
VS 2008 Express. I started posting over on a site called- Daniweb.com
before
I began meandering around the newsgroups-- I didn't know that Visual
Studio
had a newsgroup forum until two weeks ago, and by then I'd completely
rewrote
the original code to its present form. In fact, what I originally posted
that
you responded to, I pulled off a post I'd done on the same issue at
Daniweb.


Also, this newsgroup is directed toward managed C++ (C++/CLI).

You may also want to try microsoft.public.vc.language for native C++
questions - there's lots more traffic there :-)

Mark
 
Hi Mark,
I thought I was in that forum.
Since Visual Studio Express 2008 C++ is what I was using, I looked all over
the place for C++, and this was what I found.
I guess I'll look more.
Thanks again for your assistance.
Best.


:
 
SteveDB1 said:
Hi Mark,
I thought I was in that forum.


This is "microsoft.public.dotnet.languages.vc", not
"microsoft.public.vc.language " :-)

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


Since Visual Studio Express 2008 C++ is what I was using, I looked all
over
the place for C++, and this was what I found.
I guess I'll look more.
Thanks again for your assistance.
Best.


:
 
After you told me about it, I finally found the group. It did take some
serious digging though.
Also, since you spent so much time assisting me, I thought you'd like to
know that I finally figured out what my problem was.
It turns out that one of the changes that's been made in the newer is that I
needed to place my character calls within the functions, instead of making
them globals.
Now that I've changed that, it works great-- exactly as in days of old.....
Thanks again for your spending the time.



Mark Salsbery said:
SteveDB1 said:
Hi Mark,
I thought I was in that forum.


This is "microsoft.public.dotnet.languages.vc", not
"microsoft.public.vc.language " :-)

Mark
 
Back
Top