stopping functions with if, else if and else

Aug 21, 2009 at 9:39pm
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 Aug 21, 2009 at 9:59pm
Aug 21, 2009 at 10:00pm
use the exit() function, it is defined in the header <cstdlib>
Aug 21, 2009 at 10:16pm
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 ?
Aug 21, 2009 at 10:19pm
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);.
Aug 21, 2009 at 10:21pm
Try exit(0).
Aug 21, 2009 at 10:22pm
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 Aug 22, 2009 at 12:09am
Aug 21, 2009 at 10:32pm
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 Aug 21, 2009 at 10:39pm
Aug 21, 2009 at 10:51pm
std::cin.get(); does not pause the screen !!
Aug 21, 2009 at 11:08pm
It does, but only if there is nothing left in the input buffer (there probably is a '\n' still sitting in it)
Aug 21, 2009 at 11:48pm
@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
Aug 22, 2009 at 12:12am
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.
Aug 22, 2009 at 12:13am
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
Aug 22, 2009 at 12:14am
I hope he doesn't log out without seeing that I solved his problem right before he posted -.- ...
Aug 22, 2009 at 12:16am
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 Aug 22, 2009 at 12:19am
Aug 22, 2009 at 12:23am
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
Aug 22, 2009 at 12:26am
at mcleano

thank you for this cin.ignore() I'll keep it in mind :)
Aug 22, 2009 at 12:35am
At mcleano:

Then would you kindly explain what is solving the problem?
Aug 22, 2009 at 12:44am
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.
Aug 22, 2009 at 4:40pm
@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 Aug 22, 2009 at 4:44pm
Aug 22, 2009 at 10:58pm
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.