У меня есть следующий код, в котором я пытаюсь проверить радиус. Если радиус равен нулю или отрицателен, я должен дать пользователю бесконечные возможности снова написать радиус, пока он не станет действительным. Когда я тестировал код, я написал отрицательное значение, и оно работало просто отлично, оно давало мне бесконечные возможности, пока я не дал правильное значение, но если я написал правильное значение с самого начала, это заставило меня снова записать значение, как будто онобыло неверно.
#include <iostream>
#include <stdlib.h>
#include "listaEnlazada.h"
using namespace std;
using namespace listaenlazada;
void menu()
{
cout << "\n\t\tLISTA ENLAZADA SIMPLE\n\n";
cout << "1. Insert at begginig " << endl;
cout << "2. Insert at the end " << endl;
cout << "3. Insert in a specific position " << endl;
cout << "4. Write list " << endl;
cout << "5. Search element " << endl;
cout << "6. Exit " << endl;
}
int main() {
CircPtr cabezaLista = NULL, position;
int op;
string name;
double radius;
double x;
double y;
int pos;
do {
menu();
cin >> op;
switch(op)
{
case 1:
cout << "Write the name of the circle: ";
cin >> name;
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while(radius <= 0);
cout << "\n Write center: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
insertarCabeza(cabezaLista, name, radius, x, y);
break;
case 2:
cout << "Write the name of the circle: ";
cin >> name;
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while(radius <= 0);
cout << "\n Write center: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
insertarFinal(cabezaLista, name, radius, x, y);
break;
case 3:
cout << "Write the name of the circle: ";
cin >> name;
cout << "\n Write radius: ";
cin >> radius;
do{
cout << "Invalid input. ";
cin >> radius;
}while (radius <= 0);
cout << "\n Write center: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
cout << "Position : ";
cin >> pos;
posicion = buscarPosicion(cabezaLista, pos-1);
if (posicion != NULL)
insertarPosicion(posicion, name, radius, x, y);
break;
case 4:
cout << "\n\n Showing list : ";
escribirLista(cabezaLista);
break;
case 5:
cout << "\n Center of circle to search: ";
cout << "X: ";
cin >> x;
cout << "Y: ";
cin >> y;
buscarElemento(cabezaLista, x, y);
break;
case 6:
cout << "\n End of the program. ";
break;
}
cout << endl;
}while(op != 6);
return 0;
}