infinite cout loop in else statement

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


cout << "Select number corresponding material for fencing:" << endl;
cout << "wood = 1" << endl;
cout << "stone = 2" << endl;
cout << "aluminium = 3" << endl;

cin >> material;



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;
}
else (!(material == 1 || material == 2 || material == 3)) {
cout << "please input 1, 2 or 3 for your material choices"
cin >> material;
}


return 0;

}
You can just write

1
2
3
4
5
...
else {
    cout << "please...";
    cin >> material;
}
i've tried that but if i enter a number with a decimal the output is still infinite "please input 1,2 or 3 for your material choices"
In this way the code should be much more robust:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
    int material = 0;
    string inputChoice;
    do {
        cout << "Select number corresponding material for fencing:" << endl;
        cout << "wood = 1" << endl;
        cout << "stone = 2" << endl;
        cout << "aluminium = 3" << endl;

        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) {
        cout << "You have selected stone" << endl;

    } else if (material == 3) {
        cout << "You have selected aluminium" << endl;

    } else if (material == 1) {
        cout << "You have selected wood" << endl;
    }


    return 0;
}


Try it and let me know...
Last edited on
thanks, but the output still repeats with decimal values.
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?
i typed in 1.2 however this is the whole program, i don't know if anything else would make a difference here:

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <math.h>
#include <cctype>
#include <string>

using namespace std;

int main() {

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 distance between coordinate " << "(" << X[i] << "," << Y[i] << ")" << " and " << "(" << X[i + 1] << "," << Y[i + 1] << ")" << " is " << lengthOfside[i] << " m" << endl;
}

//distance of connecting first and last coordinate
lengthOfside[sideNo - 1] = sqrt(powf((X[0] - X[sideNo - 1]), 2) + powf((Y[0] - Y[sideNo - 1]), 2));

cout << "The distance between coordinate " << "(" << X[sideNo - 1] << "," << Y[sideNo - 1] << ")" << " and " << "(" << X[0] << "," << Y[0] << ")" << " is " << lengthOfside[sideNo - 1] << " m" << endl;

perimeter = perimeter + lengthOfside[sideNo - 1];


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;

}
You have to remove cin>>material before the getline.
Topic archived. No new replies allowed.