exit command

M

Micah Cowan

MZaza said:
What command can I use to make the program quite after a certain
condition?

(ITYM "function", not "command").

Heh, it's in your subject line!

#include <cstdlib>

....
if (nasty_thing_happened)
std::exit(EXIT_FAILURE)
....
 
M

MZaza

(ITYM "function", not "command").

Heh, it's in your subject line!

#include <cstdlib>

...
if (nasty_thing_happened)
std::exit(EXIT_FAILURE)
...

I get an error "111 C:\Dev-Cpp\main.cpp expected constructor,
destructor, or type conversion before '(' token"
Here is the code,
}
else if (c==3)
std::exit(EXIT_FAILURE);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
 
M

Micah Cowan

MZaza said:
(ITYM "function", not "command").

Heh, it's in your subject line!

(Please snip signatures, unless they're important. Looks like some
line-folding was also done.)
I get an error "111 C:\Dev-Cpp\main.cpp expected constructor,
destructor, or type conversion before '(' token"
Here is the code,
}
else if (c==3)
std::exit(EXIT_FAILURE);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}

That's not complete code; so I can't diagnose your issue. I do note
that the bracing and indentation don't seem to match up,
quite. Probably you have an extra closing brace after std::exit()?

In any case, please post a minimal, compilable example that
demonstrates the problem you're seeing.
 
M

MZaza

MZaza said:
What command can I use to make the program quite after a certain
condition?
(ITYM "function", not "command").
Heh, it's in your subject line!

(Please snip signatures, unless they're important. Looks like some
line-folding was also done.)


I get an error "111 C:\Dev-Cpp\main.cpp expected constructor,
destructor, or type conversion before '(' token"
Here is the code,
}
else if (c==3)
std::exit(EXIT_FAILURE);
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

That's not complete code; so I can't diagnose your issue. I do note
that the bracing and indentation don't seem to match up,
quite. Probably you have an extra closing brace after std::exit()?

In any case, please post a minimal, compilable example that
demonstrates the problem you're seeing.

Sorry Miach, I'm 3 days old programmer :)
Here's the full code,

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int c;
float x, y;
double r;
char o;

cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

while (true)

{
cout <<"-To use the result in other mathmatical operation
press 1" <<endl <<"-To clear and continue using the calculator press
2" <<endl <<"-To exit 3" <<endl;
cin >>c;
if (c==1)
{
cin >>o >>y;
switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}
else if (c==2)
{
r==0;
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

}
}

system("PAUSE");
return EXIT_SUCCESS;
}

If you have other suggestions about my programming way, I'll really
appreciate it.
 
M

MZaza

<snipped example>
(Please snip signatures, unless they're important. Looks like some
line-folding was also done.)
That's not complete code; so I can't diagnose your issue. I do note
that the bracing and indentation don't seem to match up,
quite. Probably you have an extra closing brace after std::exit()?
In any case, please post a minimal, compilable example that
demonstrates the problem you're seeing.

Sorry Miach, I'm 3 days old programmer :)
Here's the full code,

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int c;
float x, y;
double r;
char o;

cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

while (true)

{
cout <<"-To use the result in other mathmatical operation
press 1" <<endl <<"-To clear and continue using the calculator press
2" <<endl <<"-To exit 3" <<endl;
cin >>c;
if (c==1)
{
cin >>o >>y;
switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}
else if (c==2)
{
r==0;
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

}
}

system("PAUSE");
return EXIT_SUCCESS;

}

If you have other suggestions about my programming way, I'll really
appreciate it.

Sorry, I forgot to paste the new code which I pasted in the exit
function.
Anyway, I guess you know where I pasted it.
 
D

Default User

MZaza wrote:

Sorry, I forgot to paste the new code which I pasted in the exit
function.
Anyway, I guess you know where I pasted it.

No, show us the actual code that you tried to compile.




Brian
 
M

MZaza

No, show us the actual code that you tried to compile.

Brian

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int c;
float x, y;
double r;
char o;

cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

while (true)

{
cout <<"-To use the result in other mathmatical operation
press 1" <<endl <<"-To clear and continue using the calculator press
2" <<endl <<"-To exit 3" <<endl;
cin >>c;
if (c==1)
{
cin >>o >>y;
switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}
else if (c==2)
{
r==0;
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

}
else if (c==3)
std::exit(EXIT_FAILURE);
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
 
M

Micah Cowan

(You forgot to do that again; there was also a lot of unnecessary
context that could have been snipped. People tend to appreciate
brevity on newsgroups.)
Sorry Miach, I'm 3 days old programmer :)

Not a problem. :) However, it's worth noting that newcomers on many
Usenet newsgroups, including this one, are generally expected to lurk
for a while (read without posting), or at least read through a couple
weeks' worth of messages: this gives you a feel for the policies and
expectations of the newsgroup.

Both snipping unneeded context, and posting minimal, compilable
examples are common expectations. Posting a "minimal, compilable"
example means that:

1. It should be compilable. That is, it should be enough code for me
to try to run through my own toolset to help me figure out what's
wrong.
2. It should be minimal. That means, it should contain just enough
code to demonstrate the problem you're having. This often means that
you need to craft a special snippet, try it out yourself, and then paste
it in for your example. Often, this process alone can help tell you
what's going wrong.
Here's the full code,

The code you posted doesn't really constitute a minimal example, in
that it contains a lot of stuff that isn't really relevant to the
problem you're having with std::exit(), and, actually, doesn't contain
the call to std::exit()! You'll have to try again, with a minimal
example that actually demonstrates the problem you're experiencing
with std::exit().

Note, that if the function from which you're attempting to exit is
main(), then you can use the "return" statement equivalently.
#include <cstdlib>
#include <iostream>
#include <math.h>
^
This should really be said:
using namespace std;

The following link explains why the above line is not a terrific idea:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

It's somewhat better to directly import only the specific names that
you're interested in:

using std::cin;
using std::cout;
using std::endl;
using std::pow; // From said:
int main()
{
int c;
float x, y;
double r;
char o;

cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

What will happen if the user types "..." above? Check your C++
textbook and/or the C++ FAQ Lite
(http://www.parashift.com/c++-faq-lite/).
switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

The way you've written the case labels above makes your code a bit
hard to read (the "r=x+y" stuff can be easy to miss). Consider:

case '+':
r=x+y;
cout <<"The result is: " <<r << endl;
break;
case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;

You just informed the user of their error. Great! But then you keep
going anyway. You should consider looping back around and fetching a
line again.

You could avoid some code duplication above with something like:

bool print_result = true;
switch (o)
{
case '+':
r=x+y;
break;
case '-':
r=x-y;
break;
case '*':
r=x*y;
break;
case '/':
r=x/y;
break;
case '^':
r=pow(x, y);
break;
default:
cout <<"Error: wrong input" <<endl;
print_result = true;
}

if (print_result)
cout << "The result is: " << r << endl;
while (true)

{
cout <<"-To use the result in other mathmatical operation
press 1" <<endl <<"-To clear and continue using the calculator press
2" <<endl <<"-To exit 3" <<endl;
cin >>c;
if (c==1)
{
cin >>o >>y;
switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

....etc, etc. Do you really want to retype that whole thing?

If you decide to remove an operator, or add a new one, or adjust the
way one of them works, you have to change it in three different
places, every time. This is not optimal.

Wait until you've gotten far enough into your C++ book or class to
learn how to write functions, and then factor this common code snippet
into one. Alternatively (or perhaps, in addition), you can think about
ways to reorganize the code you've written so that you only need to
perform the calculations in one place.
 
M

MZaza

(You forgot to do that again; there was also a lot of unnecessary
context that could have been snipped. People tend to appreciate
brevity on newsgroups.)


Not a problem. :) However, it's worth noting that newcomers on many
Usenet newsgroups, including this one, are generally expected to lurk
for a while (read without posting), or at least read through a couple
weeks' worth of messages: this gives you a feel for the policies and
expectations of the newsgroup.

Both snipping unneeded context, and posting minimal, compilable
examples are common expectations. Posting a "minimal, compilable"
example means that:

1. It should be compilable. That is, it should be enough code for me
to try to run through my own toolset to help me figure out what's
wrong.
2. It should be minimal. That means, it should contain just enough
code to demonstrate the problem you're having. This often means that
you need to craft a special snippet, try it out yourself, and then paste
it in for your example. Often, this process alone can help tell you
what's going wrong.


The code you posted doesn't really constitute a minimal example, in
that it contains a lot of stuff that isn't really relevant to the
problem you're having with std::exit(), and, actually, doesn't contain
the call to std::exit()! You'll have to try again, with a minimal
example that actually demonstrates the problem you're experiencing
with std::exit().

Note, that if the function from which you're attempting to exit is
main(), then you can use the "return" statement equivalently.


^


The following link explains why the above line is not a terrific idea:http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

It's somewhat better to directly import only the specific names that
you're interested in:

using std::cin;
using std::cout;
using std::endl;
using std::pow; // From <cmath>





What will happen if the user types "..." above? Check your C++
textbook and/or the C++ FAQ Lite
(http://www.parashift.com/c++-faq-lite/).


The way you've written the case labels above makes your code a bit
hard to read (the "r=x+y" stuff can be easy to miss). Consider:

case '+':
r=x+y;
cout <<"The result is: " <<r << endl;
break;










You just informed the user of their error. Great! But then you keep
going anyway. You should consider looping back around and fetching a
line again.

You could avoid some code duplication above with something like:

bool print_result = true;
switch (o)
{
case '+':
r=x+y;
break;
case '-':
r=x-y;
break;
case '*':
r=x*y;
break;
case '/':
r=x/y;
break;
case '^':
r=pow(x, y);
break;
default:
cout <<"Error: wrong input" <<endl;
print_result = true;
}

if (print_result)
cout << "The result is: " << r << endl;






...etc, etc. Do you really want to retype that whole thing?

If you decide to remove an operator, or add a new one, or adjust the
way one of them works, you have to change it in three different
places, every time. This is not optimal.

Wait until you've gotten far enough into your C++ book or class to
learn how to write functions, and then factor this common code snippet
into one. Alternatively (or perhaps, in addition), you can think about
ways to reorganize the code you've written so that you only need to
perform the calculations in one place.

Ok Micah and thanks.
 
M

Micah Cowan

Ian Collins said:
*Please* trip your posts and *don't* quote signatures!
^ ("trim your posts")

MZaza, you're welcome; however, if you don't learn to follow simple
advice about trimming responses to only what's necessary (which advice
you've now been given at least three times now, four if you count
this), you'll probably end up ostracizing yourself from the people who
are most able to help you.

Everyone's time is a limited resource, and while many are here for the
express purpose of making themselves helpful, most don't want to spend
time trying to teach people who don't listen.
 
J

Jerry Coffin

[ ... ]

Okay, my first advice: now that you've gotten exit to work, forget that
you ever even heard of it. In C++, exit is a disaster waiting to happen,
and should be used only in dire emergencies.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int c;
float x, y;
double r;
char o;

cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

The code to print out the result is duplicated for every branch of the
switch statement; given a choice, I'd prefer to only once, in one place.

[ ... ]
switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}

Likewise, this switch statement looks essentially identical to the one
above -- I'd try to eliminate that duplication. I won't bother quoting
it here, but you seem to have the same switch statement yet a third time
as well.

[ ... ]
else if (c==3)
std::exit(EXIT_FAILURE);
}
}

system("PAUSE");
return EXIT_SUCCESS;

Since this code is in main(), you don't need (or want) to call exit() --
you want to just return from main() and leave it at that.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top