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.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//HW4_Reyes  14 November 2011

#include<iostream>
#include<iomanip>
#include<math.h>
#include<time.h>  //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<<randnum[num]<<" ";
    }

    cout <<endl<<endl;
    cout <<"A[verage]      E[ven]      M[inimum]      Q[uit]" <<endl;
    cout <<"Select an option: "; //Request of  user input

    cin>>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<<endl<<endl;

return 0;
}


//Begin of user defined function for determining the average of the array
float average (int a[9])
{
  float avgnum = 0;

  for (int num=0;num<10;num++)   
  //For loop used to call each element of the array
  {
    avgnum += a[num]; //Formula will add all ten numbers of the array
  }

  avgnum /= 10; 
  //Formula devides all ten numbers of the array by ten, to determine average

  cout<<endl;
  cout<<"The average value of the array is: ";
  cout<<fixed<<setprecision(2)<<avgnum<<endl<<endl; //Displays the average
  cout<<"-------------------------------------------------------------------" 
  <<endl<<endl;

return NULL;
}


//Begin of user defined function for displaying even numbers of the array
int even (int a[9])
{
  cout<<endl;
  cout<<"The even numbers of the array are: ";

  for (int num=0;num<10;num++)  
  //For loop used to call each element of the array
  {
  if ((a[num]%2)==0)  
  //If statement determining if each element of the array is even
  cout<<a[num]<<" ";  //If the number is even, it will be displayed here
  }

  cout<<endl<<endl;
  cout<<"-------------------------------------------------------------------"
  <<endl<<endl;

return NULL;
}


//Begin of user defined function for determining the minimum number of the array
int minimum (int a[9])
{
  int min = a[0];  
  //Initially sets the minimum number to the first number in the array a[0]
	
  for (int num=1;num<10;num++) //For loop used to call each element of the array
  {
    if (a[num] < min) //If statement to determine if the selected number of the array is less than the current minimum number
	min = a[num]; //If the selected number is less than the current minimum number, the selected number is made the new minimum number
  }

  cout<<endl;
  cout<<"The minimum number of the array is: ";
  cout<<min;  //The minimum number of the array is displayed
  cout<<endl<<endl;
  cout<<"-------------------------------------------------------------------"
  <<endl<<endl;

return NULL;
}
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.

I previously had:
 
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.