functions

please how do i go about calling in between functions without using goto........for example if i have done some codings and after operation i want to ask if the user wants to perform another function.
something like this below:




cout << "Thank you playing this game.\n";
goto end


end:
{
cout <<"Do you want to play another game: (y / n):";
cin >>d;
if (d == 'y')
{ cout << "\n\n";
return main();
}
}

how do i avoid using ""goto"" as in above?????...linking the main function and end??
It sounds like this job you gave here is a job for a good-old loop (No functions needed! Can you believe it?). I recommend do-while loop with the condition (d == 'y').

1
2
3
4
do
{
    //Insert code here.
} while (condition);


-Albatross
Last edited on
I agree with Albatross. We can finished it with do-while looping. Maybe your code will be like this :
1
2
3
4
5
6
do
{
         cout <<"Do you want to play another game: (y / n):";
         cin >>d;
}while(toupper(d) == 'Y');
cout << "Thank you playing this game.\n";


I'm using toupper() function just in case when the user input y in Uppercase
:D
Thanks for the replies but what i mean is that the statement below


cout << "Thank you playing this game.\n";
goto end


is one of the nested branches of an if-else loop that has about 16 branches and i need to refer to the end function on all of these branches in case the user wants to return to the main function to perform another operation....How do i go about it
Try to create a function to support that line.
closed account (S6k9GNh0)
erm, lucifer, I don't think that's what he had in mind...

1
2
3
4
5
do
{
   //Game logic
}
while (bPlayAgain);
If you need function style of writing those code instead of goto or loop just:

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
#include<iostream>
using namespace std;
bool askuser();
int main(){
//your codes
cout << "Thank you playing this game.\n";

if(askuser()){
//play another game
}
//else exit.
return 0;
}

bool askuser()
{
char d;
cout <<"Do you want to play another game: (y / n):";
cin >>d;
if (d == 'y'){
return true;}
else
{
return false;
}

}


Sorry if am wrong. :)
Last edited on
@Frank : Do you mean The Statement Thank you playing this game is printed every time user has finished a game ??
@Frank && @Computer : I'm sorry, because i'm not perfect in English, so sometimes i can't understand problems perfectly, so Sorry all ..
Last edited on
Topic archived. No new replies allowed.