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.
#include <iostream>
usingnamespace 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;
}
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.