M
MZaza
What command can I use to make the program quite after a certain
condition?
condition?
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)
...
MZaza said:(ITYM "function", not "command").
Heh, it's in your subject line!
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;
}
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.
<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.
No, show us the actual code that you tried to compile.
Brian
MZaza said:}
else if (c==3)
std::exit(EXIT_FAILURE);
}
}
else if (c==3)
std::exit(EXIT_FAILURE);
}
The brace above does not belong.
Brian
Sorry Miach, I'm 3 days old programmer
Here's the full code,
^#include <cstdlib>
#include <iostream>
#include <math.h>
This should really be said:using namespace std;
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;
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;
(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:ow; // 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.
MZaza said:Ok Micah and thanks.
^ ("trim your posts")Ian Collins said:*Please* trip your posts and *don't* quote signatures!
#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;
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;
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.