1

here is my program:

#include <iostream>
using namespace std;

int main(){
    int num;
    int numtotal = 0;
    int numcount = 0;
    int big = 0;
    int low = 0;

    cout<<"enter number or 0 to exit"<<endl;
    cin>>num;
    while(num != 0){
        numtotal = numtotal + num;
        numcount++;
        big = num;
        low = num;

        cout<<"enter number or 0 to extit"<<endl;
        cin>>num;

        if(num < low){
            low = num;
        }
        else if(num > big){
            big = num;
        }
    }

    cout<<"total of numbers: "<<numtotal<<endl;
    cout<<"totoal of numbers entered: "<<numcount<<endl;
    cout<<"biggest number: "<<big<<endl;
    cout<<"lowest number: "<<low<<endl;
}

the "low" and "big" outputs are always the last 2 numbers the user inputs, for example:

( 5, 4, 3, 2)

biggest number: 2

lowest number: 0

what am I doing wrong here? thank you

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
Sirius 26
  • 43
  • 1
  • 7
  • 2
    You always overwrite `big` and `low` near the top of your loop. – François Andrieux Mar 20 '19 at 20:45
  • It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Mar 20 '19 at 20:46
  • You always set big and low at the top of your loop. Move them outside and it should work. –  Mar 20 '19 at 20:46

3 Answers3

1

You are always overwriting big and low in your loop with the current num..

This can be fixed by initializing big and low once (ontside of the loop) like this:

#include <limits>
int big = std::numeric_limits<int>::min();
int low = std::numeric_limits<int>::max();

and by changing your loop to

while(num != 0) {
    numtotal += num;
    numcount++;

    cout << "enter number or 0 to exit" << endl;
    cin >> num;

    if(num < low)
        low = num;
    // note that this isn't in the else branch anymore
    // to also work if num is the biggest and lowest number
    if(num > big)
        big = num;
}
AlbertM
  • 1,246
  • 14
  • 25
  • The first problem you propose doesn't apply since `big` and `low` are intended to be set to the first value. Moving the `big = num;` and `low = num;` to just before the loop fixes the first two problems. – François Andrieux Mar 20 '19 at 20:56
  • Depends on how you solve the problem, I chose to not set big/low to the first number at all and instead init them to min-/max-int. Updated my answer to clarify. If you choose to init them with the first `num`you are right of course. – AlbertM Mar 20 '19 at 21:13
  • If the user enters `0` straight away won't your answer output `"biggest number: 2147483647"` and similarly `"lowest number: -2147483648"`? Also if the user enters in only positive numbers the lowest number will always be `0`. – Sash Sinha Mar 20 '19 at 21:16
  • 1
    Totally right, but the person asking the question didn't clarify what should happen in this case. Initializing big/low with num would output big and low as 0, which I would consider equally incorrect since 0 is only used to exit the program. I don't want to overcomplicate my answer with a case that isn't handled in the origial code. – AlbertM Mar 20 '19 at 21:19
1

You are always setting them at the top of your loop. Try putting them outside the loop like this:

    cout<<"enter number or 0 to exit"<<endl;
    cin>>num;

    // move them here
    big = num;
    low = num;


    while(num != 0){
        numtotal = numtotal + num;
        numcount++;

        cout<<"enter number or 0 to extit"<<endl;
        cin>>num;

        if(num < low){
            low = num;
        } else if(num > big){
            big = num;
        }
    }

1

You need to not set big and low to num if the user enters 0 inside the while loop to achieve your desired result.

Otherwise, if you don't do this, your output lowest number will always be 0 in the case the user enters only positive numbers, which I do not think you want:

#include <iostream>
using namespace std;

int main() {
    int num;
    int total = 0;
    int count = 0;

    cout << "Enter number or 0 to exit" << endl;
    cin >> num;

    int biggest = num;
    int lowest = num;

    while (num != 0) {
        total += num;
        count++;
        cout << "Enter number or 0 to exit" << endl;
        cin >> num;
        if (num != 0) {
            if (num < lowest) {
                lowest = num;
            } else if (num > biggest) {
                biggest = num;
            }
        }
    }

    cout << "Total of numbers: " << total << endl;
    cout << "Amount of numbers entered: " << count << endl;
    cout << "Biggest number: " << biggest << endl;
    cout << "Lowest number: " << lowest << endl;

    return 0;
}

Example Usage:

Enter number or 0 to exit
5
Enter number or 0 to exit
4
Enter number or 0 to exit
3
Enter number or 0 to exit
2
Enter number or 0 to exit
0
Total of numbers: 14
Amount of numbers entered: 4
Biggest number: 5
Lowest number: 2
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40