do-while loop help

Hey everyone. I'm trying to make a program where people can input from 2-5 numbers then choose the option to find the maximum number, get the numbers added together or averaged out.
After that, i'm supposed to ask if they user wants to "try again" and if yes it starts over from the beginning. I'm trying to do a do-while loop so it can start over but i keep getting this:

prog.cpp: In function ‘int main()’:
prog.cpp:111:7: error: ‘ans’ was not declared in this scope
while(ans ==y);
^
prog.cpp:111:13: error: ‘y’ was not declared in this scope
while(ans ==y);

not exactly sure what that means because i thought i identified but using
char ans;

how do i get this program to repeat itself? thanks in advance

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double getMax(double firstnum, double secondnum)
{
	if( firstnum > secondnum) return firstnum;
	else return secondnum;
	}



int main()
{
	
	do
	{
	
 double firstnum, secondnum, thirdnum, fourthnum, fifthnum;
 char operation;
 char ans;

cout << "Hello there! Please enter up to five numbers. If you wish to enter less than \n five, please make your last number negative. Please enter a minimum of \ntwo numbers" << endl;

cout << "please enter your first number: " << endl;

cin >> firstnum;

cout << "please enter your second number: " << endl;

cin >> secondnum;

if (secondnum < 0) { 

cout << "please enter + for addition, M for maximum, or A for average: " << endl;
cin >> operation;

if (operation == '+')
cout << setiosflags(ios::fixed) << "The numbers add to: " << abs(secondnum)+ abs(firstnum) << endl;

else if(operation == 'A')
cout << setiosflags(ios::fixed) << "The average of the numbers is: "<< (abs(secondnum)+abs(firstnum))/2 << endl;

else if(operation == 'M')
	cout << setiosflags(ios::fixed) << "The maximum number is: " << getMax(abs(firstnum), abs(secondnum)) << endl;

cout << " would you like to try again (y/n)?";
cin >> ans;

}

else
cout << "please enter your third number: " << endl;
cin >> thirdnum;

if (thirdnum < 0) {
cout << setiosflags(ios::fixed) << "please enter + for addition, M for maximum, or A for average: " << endl;
cin >> operation;

if (operation == '+')
cout << setiosflags(ios::fixed) << "The numbers add to: " << abs(secondnum)+abs(firstnum)+abs(thirdnum) << endl;

else if(operation == 'A')
cout << setiosflags(ios::fixed) << "The average of the numbers is: "<< (abs(secondnum)+abs(firstnum)+abs(thirdnum))/2 << endl;

cout << " would you like to try again (y/n)?";
cin >> ans;


}
else
cout << "please enter your fourth number:" << endl;
cin >> fourthnum;

if (fourthnum < 0) {
cout << setiosflags(ios::fixed) << "please enter + for addition, M for maximum, or A for average: " << endl;
cin >> operation;

if (operation == '+')
cout << setiosflags(ios::fixed) << "The numbers add to: " << abs(secondnum)+abs(firstnum)+abs(thirdnum)+abs(fourthnum) << endl;

else if(operation == 'A')
cout << setiosflags(ios::fixed) << "The average of the numbers is: "<< (abs(secondnum)+abs(firstnum)+abs(thirdnum)+abs(fourthnum))/2 << endl;

cout << " would you like to try again (y/n)?";
cin >> ans;

}

else
cout << "please enter your fifth number:" << endl;
cin >> fifthnum;

{
cout << "please enter + for addition, M for maximum, or A for average: " << endl;
cin >> operation;

if (operation == '+')
cout << setiosflags(ios::fixed) << "The numbers add to: " << abs(secondnum)+abs(firstnum)+abs(thirdnum)+abs(fourthnum)+abs(fifthnum) << endl;

else if(operation == 'A')
cout << setiosflags(ios::fixed) << "The average of the numbers is: "<< (abs(secondnum)+abs(firstnum)+abs(thirdnum)+abs(fourthnum)+abs(fifthnum))/2 << endl;

cout << " would you like to try again (y/n)?";
cin >> ans;

}
}
while(ans == y);

system("pause");
return 0;
}
Looks like you only declared ans within the loop, not within the main.. Try moving it to the main, see if that works.
I think it works now thank you! I don't have a compiler to actually see.

one more question, can i use getMax to find the maximum of more than two numbers or do i need to use something different, thanks again
Topic archived. No new replies allowed.