I have more programming before this, but everything else works fine. My else loop has an infinite output. i also want it to output an error message if the value is not an integer and return to the input
I tried but could not replicate the issue. If I type 1,2 (with the comma) or 1.2 (with the dot) the request for a correct output is repeated only once and nothing strange happens.
What do you exactly type to make the error appear?
float X[10];
float Y[10];
int sideNo;
float lengthOfside[10];
float perimeter = 0;
float area = 0;
int material = 0;
float cost;
string inputChoice;
//Ask user to enter number of coordinates
cout << "Please enter the number of sides the fence will have: " << endl;
cin >> sideNo;
while (sideNo < 3 || sideNo>10) {
cout << "Please input a value between 3 and 10" << endl;
cin >> sideNo;
}
//Ask user to enter X coordinates
for (int i = 0; i < sideNo; i++){
cout << "Please enter X coordinate " << i + 1 << ":" << endl;
cin >> X[i];
//Ask user to enter Y coordinates
cout << "Please enter Y coordinate " << i + 1 << ":" << endl;
cin >> Y[i];
//Check that coordinates are positive
if (X[i] < 0 || Y[i] < 0){
cout << "coordinates cannot be negative" << endl;
i--;
}
else
cout << "Coordinate " << i + 1 << " is: " << "(" << X[i] << "," << Y[i] << ")" << endl;
}
// find distance between each coordinate
for (int i = 0; i < sideNo - 1; i++){
//Distance between first and last coordinate
lengthOfside[i] = sqrt(powf((X[i + 1] - X[i]), 2) + powf((Y[i + 1] - Y[i]), 2));
perimeter = perimeter + lengthOfside[i];
area = (sideNo*powf(lengthOfside[i], 2)) / 4 * (tan(M_PI / sideNo));
cout << "The total perimeter of the fence is: " << perimeter << " m" << endl;
cout << "The total area of the field is: " << area << " m" << endl;
do {
cout << "Select number corresponding material for fencing:" << endl;
cout << "wood = 1" << endl;
cout << "stone = 2" << endl;
cout << "aluminium = 3" << endl;
cin << material
getline(cin, inputChoice);
if (inputChoice.length() > 1 || !isdigit(inputChoice[0]))
material = 0;
else material = inputChoice[0] - '0';
} while (material < 1 || material > 3);
if (material == 2){
cost = 100 * perimeter;
cout << "You have selected stone, the cost of the fencing is: " << char(156) << " " << cost << endl;
}
else if (material == 3){
cost = 85 * perimeter;
cout << "you haave selected aluminium, the cost of the fencing is: " << char(156) << " " << cost << endl;
}
else if (material == 1){
cost = 45 * perimeter;
cout << "You have selected wood, the cost of the fencing is: " << char(156) << " " << cost << endl;
}
return 0;