C++ ortalama hesaplarken gitgide artıyor

RSK06

Hectopat
Katılım
29 Aralık 2020
Mesajlar
2.285
Çözümler
4
Daha fazla  
Cinsiyet
Erkek
1618843218260.png


Ortalamayı hesapla dediğimde hep artıyor, bunu nasıl önleyebilirim?
 
average = total / list.size()

Satırını For'un Scope'undan çıkarın.

Teşekkür ederim C++ gerçekten sizin ve vavienin yardımları dokundu.
[CODE title="C++"]#include <iostream>
#include <vector>
using namespace std;


int main() {
char selection{};
vector<int> list{ 1, 2, 3, 4, 5 };
int new_num{};
double average{};
double total {};

do {
cout << "Menu options: \n" << endl;
cout << "P - Print numbers\nA - Add a number\nM - Display mean of the numbers\nS - Display the smallest number\nL - Display the largest number\n" << endl;
cout << "Enter your choice: ";
cin >> selection;
if (selection == 'P' || selection == 'p') {
cout << "displaying all the elements in list: [ ";
for (int i = 0 ; i < 5; i++)
cout << list << " ";

cout << " ]" << endl;
}
else if(selection == 'A' || selection == 'a') {
cout << "Add integer: ";
cin >> new_num;
cout << "Adding new number to the list: [ ";
for (int i = 0 ; i < 5; i++)
cout << list << " ";
cout << new_num << " ]" << endl;
cout << "\n" << new_num << " added.\n" << endl;


}
else if (selection == 'M' || selection == 'm') {
cout << "\nCalculating average of the list... " << endl;
for (auto ttl: list) {
total += ttl;
}
average = total / list.size();
cout << "\nThe Average is: " << average << "\n" << endl;
}





} while (selection != 'q' && selection != 'Q');
cout << endl;

return 0;
}[/CODE]
average = total / list.size()

Satırını For'un Scope'undan çıkarın.

Burada ekler misiniz rica etsem.
Cevap bekliyorum yeni konu açmak yasak olduğu için.
 

Dosya Ekleri

  • 1618845543552.png
    1618845543552.png
    45,6 KB · Görüntüleme: 17
Son düzenleme:

Yeni konular

Geri
Yukarı