### Help with ending Do While Loop

Okay, so this is my homework assignment. It uses a user input of the characters
A,E,M, or Q for quit. The program runs perfectly with the exception of quiting. When q or Q is selected, a debug error is displayed, it says something about the variable "randnum". Can anyone determine what the fault is? Thanks in advance with any help.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142`` ``````//HW4_Reyes 14 November 2011 #include #include #include #include //Time library used for generation of random numbers using namespace std; //Declaration of user defined functions float average(int[9]);//Determines and displays the average of the array int even(int[9]); //Displays all even integers int minimum(int[9]);//Determines and displayes the minimum number of the array int main() { char option; //Declare variable for user input and selection of option srand(time(NULL)); //Expression allowing the creation of a random list of numbers do //Do While loop which will continue to run until 'q' or 'Q' have been selected { int randnum[9]; //Declaring the variable for the array of ten integers const int minnum = 50; //Declaring a constant integer of 50 for the minimum integer to be selected const int maxnum = 150; //Declaring a constant integer of 150 for the maximum integer to be selected //For loop to create the random numbers in the array for (int num=0;num<10;num++) { randnum[num]=50+rand()%(101); //Designates a random number to each array element with the use of the for loop } cout<<"The random generated array is: "; //For loop used to display each element of the array for (int num=0;num<10;num++) { cout<>option; //User inputs option selection //If statements used to determine which user defined function to run if (option == 'a' || option == 'A') average(randnum); else if (option == 'e' || option == 'E') even(randnum); else if (option == 'm' || option == 'M') minimum(randnum); } while(option !='q' && option !='Q' ); //Determines when to end the Do While loop cout<
Last edited on
your while loop asks for 'q' and 'Q' whereas i think it should be 'q' or 'Q'
Perhaps add a couple extra brackets in your test condition (Line 67), and add a space after both cases of the != check. Maybe, without the space, the compiler is not recognizing that line as a comparison.

 `` `` ``while((option != 'q') && (option != 'Q') ); ``

Hope this helps.
That is not the problem,

but I did find out the problem myself. Since I said above, I was getting a "randnum" error. so I decided to change some things for the variable randnum.

The problem was in the declaration of the variable.

 `` `` ``int randnum[9];``

I simply changed it to:
 `` `` ``int randnum[10];``

If someone could explain why it needs to be 10 instead of 9, I would greatly appreciate it. I though I should have it at 9 since I only needed 10 elements in the array, and since elements 0 through 9 are a total of 10 elements.

The number in square brackets [] is how many elements are in the array, not the index of the last element.

So `int numbers[10]` declares an array with ten ints, indexed from `numbers[0]` to `numbers[9]`
I see, I completely understand it now. Thank you very much.
Topic archived. No new replies allowed.