c++ while within a y/n while loop

I'm doing a while loop within another while. The while within the while works fine if I eliminate the while surrounding it. The problem is the exterior while. I know I'm doing something wrong, but I can't seem to know what to do with the exterior while I need to fix.

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
  #include <iostream>

using namespace std;

int main ()
{
	int n, i=0;
    int num, sum=0;
    char ans;
    
	while((ans == 'Y') || (ans == 'y'))
	{
		cout << "Enter the number of the desired amount numbers to calculate: ";
		cin >> n;
    
		cout<< "Enter numbers for calculation: " << endl;

		while(i < n)
		{
			cin >> num;
			sum = sum + num;
			i++;
    	}
    
		cout<< "Answer: " << sum << endl;
		
		cout<< "Do you want to continue (Y/N)?" << endl;
		cout<< "You must type a 'Y' or an 'N' : " << endl;
    	cin >> ans;
    	cout<< endl;	
	}
    
    return 0;
}
line 10 should say:
ans='Y';

I'm not sure why it's missing. But there it isn't, line 10 is blank in your program.
Hello DigiLei,

I believe what zaphraud is trying to say is to initialize line 9 to 'Y' so that the while loop will work. Or you could change this to a do/while loop and that will work just fine.

Watch your indenting. Line 23 needs indented more and lines 25 - 28 need unindented.

Other than line 3 that is best not to use the program looks good.

Hope that helps,

Andy
Thank you all.
In my way I can say that you may not use the ((ans=='y', (ans=='y')) format to execute the the result at While(i<n).

while((ans == 'Y') || (ans == 'y'))

while(i < n)
For the proper result you need to change the condition of loop.

For more proper knowledge in programming language you can visit here: http://www.traininginlucknow.in/best-c-language-training-in-Lucknow.html
Probably want to re-initialize i too for that second run...
Topic archived. No new replies allowed.