0

I want to find Maximum numbers from my "numbers.txt" file and amount of negative numbers. And i want to output the Total result to another .txt file and console and the rest to the console only. Im very new and just cant figure out how to do it. This is what i have now

a "numbers.txt" file with

-4
53
-5
-3
2

and

#include <iostream>
#include <fstream>
using namespace std;


int main() {



    int n = 0;
    int sum = 0, total = 0;


    fstream file("numbers.txt");
    while (file >> n) 
    {
        sum += n;
        total++;

        

    }

    int average = (float)sum / total;
    int AmountOfNumbersAdded = total;
    int Highest;
    int Negative;


    cout << "Total result: " << sum << endl;
    cout << "Numbers added: " << AmountOfNumbersAdded << endl;
    cout << "Average number: " << average << endl;
    cout << "Maxiumum number: " <<  endl;
    cout << "Negative numbers: " << endl;

    return 0;

}

i tried to do

float Highest = INT_MIN;
        if (Highest < num[i]) {
            Highest = num[i]; 

but it just wouldn't work.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • Did you save all the numbers in an array? If so put them in a vector and simple sort it and then take the first or last element according on how you sorted it. – Kevin Oct 26 '22 at 05:50
  • Have a look at the example here (there is also a max_element): https://en.cppreference.com/w/cpp/algorithm/min_element/. The approach by @Kevin will work too, look at : https://en.cppreference.com/w/cpp/algorithm/sort. Things that are often needed usually already have an implementation in C++'s standard library, tested and ready for you to reuse. – Pepijn Kramer Oct 26 '22 at 05:54
  • *"it just wouldn't work"* -- please be specific. As a problem description, this is useless. What was the result? What symptom did you observe? If the problem did not stop compilation, what was the expected result? Try to focus your code on just the problematic step (**one** step, not find the max *and* find the total *and* write one output to a file *and* write other results to the console). – JaMiT Oct 26 '22 at 05:56

2 Answers2

0

You don't need to store the numbers to find the maximum or the amount of negative numbers, but you need to track them inside the loop, like you're already doing with the sum and the total amount of numbers.

int Highest = INT_MIN;
int Negative = 0;
while (file >> n) 
{
    sum += n;
    total += 1;
    if (n < 0)
    {
         Negative += 1;
    }
    if (n > Highest)
    {
        Highest = n;
    }
}

float average = (float)sum / total;
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 4.5 is not an integer, and since `n` is an `int`, reading will fail and then read no more. Read more about this in a good C++ book. – molbdnilo Oct 27 '22 at 05:50
0

Here's what you're looking for.

#include <iostream>
#include <fstream>

int main()
{
  int n = 0;
  int sum = 0, total = 0;
  int highest = 0;
  int negatives = 0;


  std::fstream file("numbers.txt");
  while (file >> n)
  {
    if (n > highest) highest = n;
    if (n < 0) ++negatives;
    sum += n;
    ++total;
  }

  int average = (float)sum / total;
  int AmountOfNumbersAdded = total;

  std::cout << "Total result: " << sum << "\n";
  std::cout << "Numbers added: " << AmountOfNumbersAdded << "\n";
  std::cout << "Average number: " << average << "\n";
  std::cout << "Maxiumum number: " << highest << "\n";
  std::cout << "Negative numbers: " << negatives << "\n";

  file.close();
  return 0;
}

As you're new, few advices for you:

  1. Never use using namespace std.
  2. Prefer using "\n" instead of std::endl.
  3. Don't forget to close any files/database after opening them like you did in your code.
  4. Always try to avoid macros.
SuperNoob
  • 170
  • 1
  • 10
  • Thanks ! Appreciate it Any reason to not use namespace std? – SharkFish423 Oct 26 '22 at 22:16
  • @SharkFish423 https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice#:~:text=It%20is%20considered%20%22bad%22%20only,use%20many%20using%20namespace%20xyz%3B%20. here are broad answers. If you find my answer helpful, don't mind upvoting. – SuperNoob Oct 27 '22 at 08:34