stopping functions with if, else if and else

Peace,
I read the thread that is about how to ask smart questions, and I think I can ask
now, firstly my English is bad so I think I can't explain good way, and also I couldn't understand few things in that thread, translators don't work perfectly from English to Arabic...

Okay, I am trying to prevent a function() from going to main()
I coudn't
here is an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
void Hello()
{
     int respond;
     std::cin >> respond;
     if (respond == 1) // if it equals 1 it would proceed perfectly to main()
     {
          std::cout << "We Are Aliens!!" << std::endl;
     }
     else
     {
          std::cout << "Exiting" << std::endl; 
         /* how to make it really exiting ??
            and does not proceed to go to main()
            I tried return Hello(); but it says I can't use
            return in functions
         */
     }
}

int main()
{
    Hello();
    std::cout << "Hello ?" << std::endl;
             /* it proceeds here either after if
                or after else
             */
     system("pause");
}



I know that I can make the respond() in the main but isn't there any other way?

thank you in advance
Last edited on
use the exit() function, it is defined in the header <cstdlib>
thank you for the fast respond
there is a problem now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib> // done..
void Hello()
{
     int respond;
     std::cin >> respond;
     if (respond == 1)
     {
          std::cout << "We Are Aliens!!" << std::endl;
     }
     else
     {
          std::cout << "Exiting" << std::endl; 
          exit(); // done..
     }
}

int main()
{
    Hello();
    std::cout << "Hello ?" << std::endl;
     system("pause");
}


the problem now is when I compile it, it opens a new (window/tab/page) near the main.cpp
that is called stdlib.h and it shows me this error:

_CRTIMP void __cdecl exit (int) __MINGW_ATTRIB_NORETURN;


any idea ?
Exit is called with an int argument which is the code that main will return. e.g. if you want your program to return 0; from main() at that point, use exit(0);.
Try exit(0).
I don't think thats really how exit() is meant to be used. As far as i'm aware all functions should return to main, and then main should return (to the OS as its caller I think).

Try chaning your code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
bool Hello() // change the return type of function
{
     int respond;
     std::cin >> respond;
     if (respond == 1) // if it equals 1 it would proceed perfectly to main()
     {
          std::cout << "We Are Aliens!!" << std::endl;
          return true;
     }
     else
     {
          std::cout << "Exiting" << std::endl; 
          return false;
     }
}

int main()
{
    if(!Hello()) //if function returns false,
       return 1; // exit with 1 - 1 usually means that an error occured
    std::cout << "Hello ?" << std::endl;
     
     std::cin.get(); // <<<<<< DON'T USE system("pause")
     return 0;
}


Refer to this on use of system(): http://www.cplusplus.com/forum/articles/11153/
Last edited on
Oh thanks now it worked
thank you people for this great help

@mcleano

the exit(0); worked greatly
sorry but I am not good with bool till now all I know that
it only carries true or false instead of numbers or letters
thank you for the system("pause"); info
I am reading it now
the problem is I can't see what happens after executing !! and this is the only way I know !!

thank you so much
I appreciate your helps
Regards
Last edited on
std::cin.get(); does not pause the screen !!
It does, but only if there is nothing left in the input buffer (there probably is a '\n' still sitting in it)
@firedraco
oh I give up (I took more than half an hour trying to find the problem, but I couldn't)
so can you tell me where is the problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
void Hello()
{
     int respond;
     std::cin >> respond;
     if (respond == 1)
     {
          cout << "We Are Aliens!!";
          
     }
     else
     {
          cout << "Exiting"; 
          exit(0);
     }
}

int main()
{
    Hello();
    cout << "Hello ?";
    cin.get();
    return 0;
}


I tried to erase all the "\n" but still the same
I use Dev-C++ v4.9.9.2

thank you in advance
First of all, when you declare that you are "using namespace std;", you no longer need to put std:: before every std function. So std::cin >> respond; would just be
 
cin >> respond;


You can't exactly prevent a function from going to main; main is where the program begins at whenever it starts up, no matter what. If you don't have a main, you have no program.

This is what I came up with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
int respond;
void Hello()
{
  cin>>respond;
  if (respond == 1)
  {
    cout<<"We are aliens!!\n";
  }
  else if (respond != 1) // specifically point out to exit only if 
  // respond did not equal 1
  {
    cout<<"Exiting!\n";
    exit(0);
  }
}
int main()
{
  Hello();
  cout<<"Hello?\n";
  cin.get();
  cin.get(); // added in a cin.get(); to prevent program from cutting out
  // after "Hello?" was printed
}


User won't be able to proceed if respond didn't equal one, which is what I am assuming you meant to do.
YES, I found the problem :D

I have to type cin.get(); twice

this proves what you said firedraco, my output includes a letter that executes after the cin.get();
so by typing it twice I will have another chance

thank you so much people

regards
I hope he doesn't log out without seeing that I solved his problem right before he posted -.- ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
void Hello()
{
     int respond;
     cin >> respond; // why were you using std::cin?
     cin.ignore() // clear the input buffer of '\n' after the user inputs the value for respond
     if (respond == 1)
     {
          cout << "We Are Aliens!!";
          
     }
     else
     {
          cout << "Exiting"; 
          exit(0);
     }
}

int main()
{
    Hello();
    cout << "Hello ?";
    cin.get();
    return 0;
}


[EDIT

I hope he doesn't log out without seeing that I solved his problem right before he posted -.- ...


Writing cin.get(); twice is not solving the problem and if you think it is you really need to re-think things
Last edited on
at Warrior2009
Oh god, I am really sorry :D
the difference between our comments is 1 minute or maybe less :D

yea I have just know that about std you can check my previous code;

and about the main(); what I meant is:
after main() begins I call a function called hello()
so the hello includes if and else
so if the input did not match the right letter it would stop proceeding to the next line in main()
and quits the program

Thank you so much :)

(I don't know if the smile, :D, :) .. are bad to use in programming forums) sorry
at mcleano

thank you for this cin.ignore() I'll keep it in mind :)
At mcleano:

Then would you kindly explain what is solving the problem?
You want to specifically remove the '\n' from the input stream at the point where it is put in, not at the end where other input might have already removed it.
@Warrior:

Gladly. If you look at my code, specifically at line 7, the code cin.ignore(); is doing as firedraco said:

You want to specifically remove the '\n' from the input stream at the point where it is put in, not at the end where other input might have already removed it.


By default, cin.ignore(); is set to ignore a '\n'. So the '\n' that is left in the input stream by cin>>respond; is ignored.

The problem with your code is that if the OP wanted to alter his code to get another user input, using your solution of adding a cin.get(); at the end of the code would not work because the '\n' thats left in the input stream from the first user input, will be carried through to the next time cin is called - I believe...

Also, get() should be used if you're expecting something from the user, ignore should be used when you want to rid something from the users input.
Last edited on
If you want to return from a void function, simple use return; with nothing else.

1
2
3
void f () {
	return;
}
Topic archived. No new replies allowed.