I'm attempting to make a program where it asks the user to enter a number that I will eventually compute, but if the user were to enter a "x" the loop would end. I don't have much but if I run this program and type in a "x" it bugs out because the data type it is looking for is a double so it doesn't work the way I want it. Is there an alternate way than the way I have it here to make it so that the loop will end instead of the program bugging out?
#include "stdafx.h" #include <iostream> #include <string> using namespace std; //function //main function int main() { //variables double n1, n2; //input do { cout << "Enter the first number \n"; cin >> n1; cout << "Enter the second number \n"; cin >> n2; //output } while (true); return 0; }
if(cin >> n1)will be false if they do not enter a number.