0

it seems that I'm stuck at c++ yet again. woohoo.

#include <iostream>
#include <fstream> 

using namespace std;

int main() {


    int amount = 0;
    int inputNumber = 0;
    int temp = 0;
    int n = 0;
    int lowest;
    int highest;
    double averages;

    cout << "Welcome to simple calculator. Where you can use for average, highest amd lowest value.";
    cout << "Please input all the number you plan to use to calculate. ";
    cout << "Finish your input by enter any letter and then press on enter. " << endl;

    while(cin >> inputNumber)
    {
        

        if (inputNumber < lowest) {

            int (lowest = inputNumber);

        }

        else if(inputNumber > highest){

            int (highest = inputNumber);
            
        }
        
        amount = amount += inputNumber;
        n++;
    }

    double averages = amount / n;

    cout << "Your lowest value is: " << lowest << endl;
    cout << "Your highest value is: " << highest << endl;
    cout << "Your average value is: " << averages << endl;
    cout << "Amount is " << amount << endl;
    cout << n << endl;
    return 0;

}

but when I put in numbers it doesn't work properly. like

3
5
d
Your lowest value is: 3
Your Highest value is: 5
Your average value is: 4
2
-2
-3
a
Your lowest value is: -3
Your Highest value is: 0
Your average value is: -2
2

Idk why it do this and I'm still new to C++, I also have to code it in Linux and use G++

Thanks in advance if you could help me out.

0x5453
  • 12,753
  • 1
  • 32
  • 61

2 Answers2

0

try average = double(amount)/double(n); Because average is double and n and amount

0

There are few errors in your code:

  1. variable averages is defined twice.

Since for calculating amount you are doing int/int. which in result will yield int. Thus your answer for -3 2 d will come wrong.

#include<iostream>

using namespace std;

int main()
{
    int amount = 0;
    int inputNumber = 0;
    int temp = 0;
    int n = 0;
    int lowest=INT_MAX;
    int highest=INT_MIN;

    cout << "Welcome to simple calculator. Where you can use for average, highest amd lowest value.";
    cout << "Please input all the number you plan to use to calculate. ";
    cout << "Finish your input by enter any letter and then press on enter. " << endl;

    while(cin >> inputNumber)
    {


        if (inputNumber < lowest) {

            int (lowest = inputNumber);

        }

        else if(inputNumber > highest){

            int (highest = inputNumber);

        }

        amount += inputNumber;
        n++;
    }

    double averages = (double)amount / n;

    cout << "Your lowest value is: " << lowest << endl;
    cout << "Your highest value is: " << highest << endl;
    cout << "Your average value is: " << averages << endl;
    cout << "Amount is " << amount << endl;
    cout << n << endl;
    return 0;
}

This above code is correct code

Abhinav
  • 1
  • 1
  • Still not fully working. when I put 2 + 3 I got Your lowest value is: 2 Your Highest value is: 0 Your average value is: 2 n = 2 – AlphaInterWoof Dec 08 '20 at 19:04
  • I don't understand what you are saying. When I ran the above program using ``` 2 3 d ``` as input. I am getting 2.5 as output. It is working fine for me – Abhinav Dec 09 '20 at 11:28