Output sorted array

Hi,

I am writing a program where the user is asked to input values that should be stored in an array (a range of min 0 values and max 100 values). After user decides not to input any new values, the array should be sorted an the program should output the array in ascending order. Something is wrong with me code. After I input a value and hit enter after the first input, nothing more happens and I can't figure out what the error is. It doesn't ask if user wants to input another value and it doesn't output anything. Any help will be greatly appreciated! (I am very very new to this.)

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

using namespace std;

int main()
{
    float value, sum = 0, largestValue, smallestValue, average, median,
        variance;
    float myarray[100];
    int countTotalValues = 0;
    char answer = 'y';

    while (i <= 100)
        {
        cout << "Would you like to add a value? (Y or N)." << endl;
        cin >> answer;

        if (answer == 'y' || answer == 'Y')
        cout << "Enter a value: ";
        cin >> value;

        for (int i = 0; i <= 100; i++)
        {
            cin >> myarray[i];              
            
            ++countTotalValues;
            sum = +value;
        }

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

        if (answer != 'y' || answer != 'n')

            cout << "Invalid input. Please enter Y or N." << endl;
        }

    if (myarray[i] > myarray[i + 1])         //sort
        swap(myarray[i], myarray[i + 1]);
In line 13, 'i' is an undeclared variable. You must first declare it and maybe give it an initial value before the loop block.
For the if block(line 18), it will be advisable to bring the curly brackets, since you want to execute more than a line based on a condition.

Aceix.
Aceix,

Thank you so much! Based on the global variables at the top, should 'i' be substituted by 'countTotalValues' throughout the code? Isn't that the variable I am really referring to every time 'i' is used in this code?

Then at the bottom in the 'if' construct used for sorting, should 'i' be substituted for '100' since this is the max value? Thanks again!
Topic archived. No new replies allowed.