check input and store value in array

This program works but i need to build an additional loop that will re-prompt a user if his initial entry (int y_n) is other than 'Y', 'y', 'N', 'n'. For instance if a user tries to enter 'Yup', he will be prompted "That isn't a valid entry" and then re-asked to enter int y_n.

Note: If user answers Y he is asked to enter a value that is entered into an array. If the user at any point answers N, the program ends and final stats are cout.

-------------------------------------------------

#include <iostream>
using namespace std;

int main(){
char y_n;
int i = 0;
float input_value;
float myarray[100];
float num_count = 0;
float array_sum = 0;
float array_avg = 0;

for(i=0;i<=100;i++){

cout << "Do you want to input a value (Y or N)" ;
cin >> y_n;

if((y_n == 'n') || (y_n == 'N'))
break;

else if((y_n == 'Y') || (y_n == 'y'))
cout << "Enter Value: ";
cin >> input_value;
myarray[i]= input_value;
num_count ++;
array_sum = (input_value + array_sum);
array_avg = (array_sum/num_count);
continue;}

cout << "N means loop is done" << endl;
cout << "Value for the 4th element is " << myarray[3] << endl;
cout << "There were " << num_count << " values entered" << endl;
cout << "The sum of the values entered is " << array_sum << endl;
cout << "The average of the values entered is " << array_avg << endl;
return 0;
}

Please use code tags. You can get them from the <> button to the right of the posting box.

Shouldn't your else if statement have some brackets?

What you could do is add an else condition to your if-else-if chain. Then all you'd need to do is have it print a message and reduce i by one (so that the loop doesn't move ahead while the user hasn't inputted valid data).

-Albatross
Topic archived. No new replies allowed.