Çözüldü C++ adding list değişmiyor

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

RSK06

Hectopat
Katılım
29 Aralık 2020
Mesajlar
2.283
Çözümler
4
Daha fazla  
Cinsiyet
Erkek
[CODE lang="cpp" title="C++"]#include <iostream>
#include <vector>
using namespace std;

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

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\nQ - Quit\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;
}
else if (selection == 'S' || selection == 's') {
vector<int> min_element();
cout << "\nThe Smallest element in the list is: " << min_element << "\n" << endl;
}

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

return 0;
}[/CODE]

Kod bu, bunu çalıştırınca mesela p basınca listeyi gösteriyor (12345) fakat a basıp yeni bir sayı ekledikten sonra liste değişmiyor, hep (12345).
Bunu nasıl düzeltebilirim?
 
Son düzenleyen: Moderatör:
Çözüm
Kodu atıyorum.
[CODE lang="cpp" title="C++"]#include <iostream>
#include <vector>
using namespace std;


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

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\nQ - Quit\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: [ ";
list.push_back(new_num);
cout<< "Showing the updated list: ";
for (unsigned i = 0 ; i < list.size(); i++)
cout<<list << " ";
cout << " ]" << 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;
}
else if (selection == 'S' || selection == 's') {
vector<int> min_element();
cout << "\nThe Smallest element in the list is: " << min_element << "\n" << endl;
}





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

return 0;
}[/CODE]

Bak dostum, sana iki tane seçenek sunacağım. M tuşuna atadığın şartta C++11 ile dile eklenen foreach tarzı döngü kullanıyorsun. Liste elemanlarını göstermek için ya liste boyutunun büyüklüğüne kadar çıkan döngüyü kullan, ya da bu foreach tarzı döngüyü kullan. Seçim senin. P tuşuna atadığın şarttaki döngü maksimum 5'e kadar çıkıyor, o 5'i list.size() olarak düzenler isen, veya döngüyü foreach mantığı ile yaparsan P tuşu düzgünce çalışacaktır.

Ayrıca S tuşuna atadığın şartta neden tekrar boş bir liste oluşturduğunu anlamadım. Bubble sort gibi sort algoritmaları ile listedeki en küçüğü arayabilirsin.
[CODE lang="cpp" title="C++"]
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;

}
[/CODE]

Bu kodda bir değer ataması yaptığını göremedim açıkçası. Sadece listede var olan elemanları ve yeni girilen sayıyı gösteriyor ve yeni numaranın eklendiğini söylüyor. Şöyle bir şey işini görür:

C++:
else if(selection == 'A' || selection == 'a') {
cout << "Add integer: ";
cin >> new_num;
list.push_back(new_num);
cout<<"Showing the updated list: \n";
for (int i = 0 ; i < list.size(); i++){
cout<<list[i]<<"\n";
}
}
 
Son düzenleme:
Bu kodda bir değer ataması yaptığını göremedim açıkçası. Sadece listede var olan elemanları ve yeni girilen sayıyı gösteriyor ve yeni numaranın eklendiğini söylüyor. Şöyle bir şey işini görür:

C++:
else if(selection == 'A' || selection == 'a') {
cout << "Add integer: ";
cin >> new_num;
list[list.size()] = new_num;
cout<<"Showing the updated list: \n";
for (int i = 0 ; i < list.size(); i++){
cout<<list[i]<<"\n";
}
}

Showing the updated list dedikten sonra kapanıyor nedense.
1618874338263.png


Ve uyarı veriyor nedense.
1618874503879.png
 
Çünkü p tuşundaki şartta kullandığın döngüde i değerin maksimum 5'e çıkıyor. For döngülerinde list.size() kullan 5 yerine.
Kodu atıyorum.
[CODE lang="cpp" title="C++"]#include <iostream>
#include <vector>
using namespace std;


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

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\nQ - Quit\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: [ ";
list.push_back(new_num);
cout<< "Showing the updated list: ";
for (unsigned i = 0 ; i < list.size(); i++)
cout<<list << " ";
cout << " ]" << 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;
}
else if (selection == 'S' || selection == 's') {
vector<int> min_element();
cout << "\nThe Smallest element in the list is: " << min_element << "\n" << endl;
}





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

return 0;
}[/CODE]
 
Kodu atıyorum.
[CODE lang="cpp" title="C++"]#include <iostream>
#include <vector>
using namespace std;


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

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\nQ - Quit\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: [ ";
list.push_back(new_num);
cout<< "Showing the updated list: ";
for (unsigned i = 0 ; i < list.size(); i++)
cout<<list << " ";
cout << " ]" << 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;
}
else if (selection == 'S' || selection == 's') {
vector<int> min_element();
cout << "\nThe Smallest element in the list is: " << min_element << "\n" << endl;
}





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

return 0;
}[/CODE]

Bak dostum, sana iki tane seçenek sunacağım. M tuşuna atadığın şartta C++11 ile dile eklenen foreach tarzı döngü kullanıyorsun. Liste elemanlarını göstermek için ya liste boyutunun büyüklüğüne kadar çıkan döngüyü kullan, ya da bu foreach tarzı döngüyü kullan. Seçim senin. P tuşuna atadığın şarttaki döngü maksimum 5'e kadar çıkıyor, o 5'i list.size() olarak düzenler isen, veya döngüyü foreach mantığı ile yaparsan P tuşu düzgünce çalışacaktır.

Ayrıca S tuşuna atadığın şartta neden tekrar boş bir liste oluşturduğunu anlamadım. Bubble sort gibi sort algoritmaları ile listedeki en küçüğü arayabilirsin.
 
Çözüm
Bak dostum, sana iki tane seçenek sunacağım. M tuşuna atadığın şartta C++11 ile dile eklenen foreach tarzı döngü kullanıyorsun. Liste elemanlarını göstermek için ya liste boyutunun büyüklüğüne kadar çıkan döngüyü kullan, ya da bu foreach tarzı döngüyü kullan. Seçim senin. P tuşuna atadığın şarttaki döngü maksimum 5'e kadar çıkıyor, o 5'i list.size() olarak düzenler isen, veya döngüyü foreach mantığı ile yaparsan p tuşu düzgünce çalışacaktır.

Ayrıca s tuşuna atadığın şartta neden tekrar boş bir liste oluşturduğunu anlamadım. Bubble sort gibi sort algoritmaları ile listedeki en küçüğü arayabilirsin.

Teşekkür ederim çok yardımcı oldunuz.
 

Yeni konular

Geri
Yukarı