Do-While loop

So I've searched the forums a bit and tried to find a solution to my problem without success. I've been trying to do some text exercises from an old test in my programming course in preparation for an upcoming exam. The link to the test can be found here:

http://webstaff.itn.liu.se/~aidvi/courses/10/C++%28basic%29/Exams/October21%28I%29.pdf

The exercise in question is the second with a beautiful looking mathematical formula. Anyway, I'm having trouble with the part where it should check to make sure the input is greater than zero as my array will always have a zero in any of the spaces I haven't filled up. I'm supposed to be using arrays in this particular exercise. Here is the code I've written so far:

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
#include <iostream>
#include <iomanip>
using namespace std;

int a=0;
int mult(int S[], int a);

int add(int S[], int a);

int sort(int S[], int a);
int main()
{
    cout << "Sequence: ";
    int X[100]={};
    for (int i=0; i<100; i++){
        cin >> X[i];

            if (isdigit(X[i]))
            break;

            if(X[i]<=0)
                do{
                cout << "Numbers in the sequence must be greater than 0! \nTry again: ";
                cin >> X[i];
                } while (X[i]==0); //this particular do-while loop will run once and then never ask for an input. I really just need to make sure a number that's been input is over zero. The sequence should stop once the user enters anything other than a number.

            if (X[i]!=0)
            a++;
    }
    int S[a];
    for(int j=0;j<a;j++){
        S[j] = X[j];
    }
    double Ep, Pi, result;
    S[a] = sort(S,a);
    Ep=add(S,a);
    Pi=mult(S,a);

    result = ((S[a-2]*Pi)-(S[a-1]*Ep))/(a*(a-1));
    cout << fixed << setprecision(3) << "Result: " << result;
}
int mult(int S[], int a){
    int prod = 1;
    for (int i=0; i<a; i++){
        prod *= S[i];
    }
    return prod;
}

int add(int S[], int a){
    int sum = 0;
    for (int i=0; i<a; i++){
        sum += S[i];
    }
    return sum;
}

int sort(int S[], int a){
        int temp;
        for (int pass = 0; pass <= (a - 2); pass++) {
            for (int i = 0; i < (a - 1); i++) {
                if (S[i] > S[i + 1]) {
                     temp = S[i];
                     S[i] = S[i + 1];
                     S[i + 1] = temp;
                }
            }
        }
    return S[a];
}


Any help would be greatly appreciated!

Last edited on
Why is your loop condition for when the user enters 0? I thought you did not want negative numbers?
well i guess i made a change before i posted it to the forum but the values of all the numbers in the sequence must be greater than zero. So both negative numbers and zero are disallowed. Sorry for the confusion...
Bump
How did that not answer your question?
Topic archived. No new replies allowed.