Terminating upon a conditional?

Hi, I'll try not to be too wordy, but this is my first program I'm writing for my COMP class and I'm obviously still very green when it comes to programming. My professor is pretty old fashioned, so we type up our code using TextPad and do the compiling and linking ourselves through the DOS prompt (I'm assuming there has to be an easier way).

Anyways, the program itself is very basic and asks for three inputs, and then provides two possible values for X using a slight variation of the quadratic equation. He wants the program to end with an appropriate error message if the values input result in a negative under the radical or if the denominator becomes zero.

Like I said, I'm very rough around the edges so I'm sure I'm making this much longer than it needs be, but is there a way to have the program terminate under an "if" conditional? For instance for my portion to ensure my value under the radical is positive:

if (3 - 2*a < 0)
{

cout <<"ERROR: Your value for 'a' doesn't allow for Real answers.";
system("pause");
// Is there something I can put here inside the brackets where the program will only terminate under this conditional?

}

I tried searching Google and this forum for a while and found many instances, but none like this one, and like I said, I don't have enough programming experience to be able to easily parallel one problem with another. I would appreciate even the smallest of help. Thanks in advance!
If you in main, just return.

1
2
3
if(something) {
    return 0;
}
1
2
3
4
5
6
7
8
if (3 - 2*a < 0)
{

cout <<"ERROR: Your value for 'a' doesn't allow for Real answers.";
system("pause");
return 0;

}
LOL
Last edited on
Error E2467 proj1.cpp 30: 'main()' cannot return a value in function main()
Error E2467 proj1.cpp 38: 'main()' cannot return a value in function main()

So I'm receiving these two error messages when I'm trying to compile after I've added
"return 0;"
to the end of both of my "if" statements.

Am I just setting these conditionals up wrong or perhaps have I done something wrong in the beginning? I'm sorry these questions probably sound like they're coming from a 5-year old.
@firedraco:
If you are returning because of an error, I think that you shouldn't return 0 (EXIT_SUCCESS) but instead an error code.

@Legal
Show some code.
Which one do you have?
1
2
3
main()
void main()
int main()


My professor is pretty old fashioned, so we type up our code using TextPad and do the compiling and linking ourselves through the DOS prompt (I'm assuming there has to be an easier way).

http://xkcd.com/378/
I kinda figured my way around it just by using an If Else conditional and generalizing my error message a bit. I imagine the professor won't be too happy, but nonetheless the program works flawlessly. Thanks for the help guys!

Oh and for what its worth I was using void main(void)
So you're telling: my program doesn't receive parameters and doesn't return anything.
But later you're trying to do return something;, so the compiler complains.

Also in C++ main must return int (it's weird that the compiler didn't say anything).
I believe the C standard for main function prototype is below but it seems C or C++ compiler can accept other variations too.

1
2
3
int main();
int main(int argc, char *argv[]);
int main(int argc, char **argv);
The argc and argv variables can be named anything. Some people don't know this.
Topic archived. No new replies allowed.