bugfix, numbers out of range in vector
This commit is contained in:
32
main.cpp
32
main.cpp
@@ -17,26 +17,29 @@ int main() {
|
|||||||
|
|
||||||
int min, max;
|
int min, max;
|
||||||
|
|
||||||
// Prendo in input il vettore x
|
|
||||||
cout << "Inserisci gli elementi di X, un numero per riga." << endl
|
|
||||||
<< "Per terminare l'inserimento inserire una riga vuota o non valida." << endl;
|
|
||||||
while (!vectorEnd) {
|
|
||||||
cout << "> ";
|
|
||||||
getline(cin, line);
|
|
||||||
try {
|
|
||||||
number = std::stoi(line);
|
|
||||||
x.push_back(number);
|
|
||||||
} catch (std::exception ex) {
|
|
||||||
vectorEnd = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prendo in input min e max
|
// Prendo in input min e max
|
||||||
cout << "Min = ";
|
cout << "Min = ";
|
||||||
cin >> min;
|
cin >> min;
|
||||||
cout << "Max = ";
|
cout << "Max = ";
|
||||||
cin >> max;
|
cin >> max;
|
||||||
|
|
||||||
|
// Prendo in input il vettore x
|
||||||
|
cout << "Inserisci gli elementi di X, un numero per riga." << endl
|
||||||
|
<< "Per terminare l'inserimento inserire una riga vuota o non valida." << endl;
|
||||||
|
|
||||||
|
getline(cin, line);
|
||||||
|
while (!vectorEnd) {
|
||||||
|
cout << "> ";
|
||||||
|
getline(cin, line);
|
||||||
|
try {
|
||||||
|
number = std::stoi(line);
|
||||||
|
if(number >= min && number <= max) {
|
||||||
|
x.push_back(number);
|
||||||
|
}
|
||||||
|
} catch (std::exception ex) {
|
||||||
|
vectorEnd = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (max > min) { // Verifico input
|
if (max > min) { // Verifico input
|
||||||
std::sort(x.begin(), x.end());
|
std::sort(x.begin(), x.end());
|
||||||
@@ -50,7 +53,6 @@ int main() {
|
|||||||
x.erase(x.begin());
|
x.erase(x.begin());
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; i <= max; i++) {
|
for (; i <= max; i++) {
|
||||||
cout << i << endl;
|
cout << i << endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user