#include<iostream>intmain(){intselection{0};boolinvalid{true};// new variable just to gate the loopwhile(invalid){std::cout<<"Please make a selection: \n";std::cout<<"1) Addition\n";std::cout<<"2) Subtraction\n";std::cout<<"3) Multiplication\n";std::cout<<"4) Division\n";std::cin>>selection;invalid=(selection!=1&&selection!=2&&selection!=3&&selection!=4);}// do something with selection here// such as a switch statementstd::cout<<"You selected option #"<<selection<<'\n';return0;}
#include<iostream>intmain(){// selection must be declared outside of the do-while so we can use it laterintselection{};do{std::cout<<"Please make a selection: \n";std::cout<<"1) Addition\n";std::cout<<"2) Subtraction\n";std::cout<<"3) Multiplication\n";std::cout<<"4) Division\n";std::cin>>selection;}while(selection!=1&&selection!=2&&selection!=3&&selection!=4);// do something with selection here// such as a switch statementstd::cout<<"You selected option #"<<selection<<'\n';return0;}