How to make a loop exit and stop putting values into an array

The goal of my program is to have the user input sales from a given day and the number of iterations is unknown. How do I make the program stop inputting values into the array if the user enters -1? Also, the program is supposed to validate if the amount entered is positive and produce the max sale, minimum sale, and average sale.

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

using namespace std;

int main() {
    int sales;
    double sum=0;
    double mean, max=0, min;
    double numbers[sales];
    
    for (int sales = 0;; sales++){
    
        cout << "Enter the sale amount. ";
        cin >> numbers[sales];
        
            if (numbers[sales]<-1){
            cout << "Invalid amount, try again! ";
            cin >> numbers[sales];
            }
            else if (numbers[sales]==-1){
                break;
                
            }
    }
    
    for (int sales = 0;; sales++){
        sum+=numbers[sales];
        mean = sum/sales;
    }
        
    for (int sales = 0 ;; sales++){
        
        if (numbers[sales] > max){
            max = numbers[sales];
        }
    }
    
    for (int sales = 0 ;; sales++){
        
        if (numbers[sales] < min){
            min = numbers[sales];
        }
    }
}
Last edited on
1
2
3
4
5
6
#include <vector>

int value;
std::vector<int> sales;
while(std::cin >> value and value not_eq -1)
   sales.push_back(value);
I am supposed to use arrays though. @ne555
Last edited on
Where do you set the value for sales (the one declared on line 6, not the ones in the for loops).
Last edited on
Wait a sec, where in your requirements does it say you need an array? Nothing in your actual description seems to imply you need an array.
What you need are four important variables:

1. a variable to save the current sum
2. a variable to save the number of items entered so far (just the count)
3. a variable to save the minimum number entered so far
4. a variable to save the maximum number entered so far


In other words, I don't think you need an array or vector (or other container) to solve this.
Last edited on
I declared sales as an int, but the size is unknown because the user determines the number of times they enter a sale. Also, I know I don't have to use arrays, but it's required for the assignment. @Ganado
So... what does "use" mean in this instance? Can you declare an array but just not use it? Does that meet the requirements? Or do you have to store elements in it?

Are you allowed to dynamically allocate array sizes?

Three options:

1. Don't use arrays at all (it's really unneeded)
2. Use std::vector (it's basically an array with a nice wrapper)
3. Use dynamic arrays that you have to manually resize, since you don't know how many elements the user will input
Last edited on
I need to use an array and store the user inputs insides and when the user types -1 the program stops inputting numbers into the array. Then it proceeds to output the max, min and mean.
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
// Example program
#include <iostream>

// Assumptions: arr is either a nullptr and old_size is 0,
// or arr has previously been dynamically allocated using new[]
void add_to_array(int value, int*& arr, int old_size)
{
    int* new_arr = new int[old_size + 1];
    for (int i = 0; i < old_size; i++)
    {
        new_arr[i] = arr[i];
    }
    
    new_arr[old_size] = value;
    
    delete[] arr;
    arr = new_arr;
}

int main()
{
    /// I need to use an array and store the user inputs insides
    /// and when the user types -1 the program stops inputting numbers into the array.
    /// Then it proceeds to output the max, min and mean.
    
    int* arr = nullptr;
    
    int size = 0;
    
    while (true)
    {
        int value;
        
        std::cout << "Enter value: ";
        std::cin >> value;
        if (value == -1)
            break;
        
        add_to_array(value, arr, size++);
    }
    
    std::cout << "out of loop\n\n";
    
    
    
    std::cout << "Do more stuff here\n\n";
    
    for (int i = 0; i < size; i++)
    {
        std::cout << arr[i] << '\n';   
    }

    delete[] arr;
}


Enter value: 3
Enter value: 3
Enter value: 4
Enter value: 4
Enter value: 5
Enter value: 5
Enter value: 6
Enter value: 6
Enter value: 6
Enter value: 7
Enter value: -1
out of loop

Do more stuff here

3
3
4
4
5
5
6
6
6
7
Last edited on
Thank you.
1
2
3
4
5
const int capacity = a_big_enough_number;
double numbers[capacity];
int size = 0;
while(size < capacity and std::cin >> value and value not_eq -1)
   numbers[size++] = value;
Topic archived. No new replies allowed.