prompt the user to rerun the program

closed account (Diz60pDG)
I completed this program, but I want to promt the user to re run the program by pressing y or n for yes or no

This is what I have so far, but the part at the end is not working
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
double average();
char chr;

int main()
{
	cout << "The avergae is: " << average() << endl;
}
double average()
{
	double input = 0;
	double total = 0;
	int count = 0;
	double average;

	do
	{
		cout << "Enter a stream of positive numbers (0 or above)." << endl;
		cout << "Enter a negative number when you are finished." << endl;
		cin >> input;
		if (input >= 0)
		{
			total = input + total;
			count++;
		}

	} 
	while (input >= 0);
	average = total / count;
	
	return average;
	
	char answer;

	do
	{
		cout << "Would you like to repeat?  Enter Y/N";
		cin >> answer;
		if ('N')
			cout << "Thanks for playing!";
	} 
	while (answer = 'Y');

	cin >> chr;
	
	return 0;
	
}
Well one way would be to use the goto statement. Pretty much, you type any word, then ":" after it. Then, you can later go to it.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Function( ){
	beginning:
	/* Code
	----- code -----
	----- code -----
	*/
	
	// Before ending the function
	std::cout<<"Are you sure you want to quit? (y/n?) \n";	// Prompt User
	std::cin>>input;	// Get Answer
	
	if(input == "n")	goto beginning;	// This will go back to line 2, restarting the function.
	else return;
}


Another way, of course, is just to use recursion. Pretty much, in the if statement on line 12 (my example above), replace
if(input == "y") goto beginning;
with
if(input == "y") Function();
What this does is just call the function inside the function, hence the name "recursion".

You can always just use a while loop, but I usually use these. It is just easier (to me) to just do this than a while loop.
AFAIK, there are no catches to these techniques, or in other words, there are no consequences. If there are, then I need to rethink my answer...

If you want a reference to the goto statement, here you go:
http://msdn.microsoft.com/en-us/library/b34dt9cd.aspx
Your function "average" will never get to line 33 -- line 32 is an absolute return of the value to main()

All you need to do is put the do and bracket from lines 36 and 37 after the beginning main { bracket, and lines 38 through 42 just before the ending main } bracket.

Eliminate everything in the function after line 32.

The use of goto, in almost any computer language, is . . . um. . . . not appreciated by all. It is unnecessary in almost any context.

Expression 'N' doesn't look like a boolean term, but C++ will always call it true (<char value> 'N' == <int value> 78, and != 0 is true ). You'll need to do this instead:

if (answer == "N") // probably should include "||" for lower case "n" too

One last thing:

It looks strange to say "The Average is: " and then ask for the inputs - maybe you could consider:

1
2
double avg = average();
cout << endl << "The Average is : << avg; 


This puts your statement together with the average instead of separating them by a whole lot of input values.
Last edited on
@PCrumley:


You'll need to do this instead:

 
if (answer == "N") // probably should include "||" for lower case "n" too  



it should be:
 
if (answer == 'N')
Yup - if answer is char; if answer is string, double quotes.

I sometimes get that mixed up :(

My main programming background is in a dialect that doesn't care about double or single quotes, as long as they match.

It is fine with:

print 'This is a "Quoted phrase" '

or

print "This is a 'Quoted phrase' "

C and its relatives aren't so forgiving.
Topic archived. No new replies allowed.