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
#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?
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);.
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).
#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;
returntrue;
}
else
{
std::cout << "Exiting" << std::endl;
returnfalse;
}
}
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;
}
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 !!
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.
#include <iostream>
usingnamespace std;
int respond;
void Hello()
{
cin>>respond;
if (respond == 1)
{
cout<<"We are aliens!!\n";
}
elseif (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.
#include <iostream>
usingnamespace 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
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
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.
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.