Skip to content

Instantly share code, notes, and snippets.

@greggroth
Created December 1, 2011 20:03
Show Gist options
  • Save greggroth/1419423 to your computer and use it in GitHub Desktop.
Save greggroth/1419423 to your computer and use it in GitHub Desktop.
how I do it.
#include <iostream>
#include <string>
#include <math.h>
using namespace std ;
bool checkForDouble(string const& s) {
istringstream ss(s);
double d;
return (ss >> d) && (ss >> std::ws).eof();
}
int main (void) {
string inS;
double doubS;
while (1) {
cout << "S (must be between 0 and 1): ";
getline(cin, inS);
if (!checkForDouble(inS)) {
cout << "I need a number for this to work. Try again." << endl;
continue;
}
stringstream strStream;
strStream << inS;
strStream >> doubS;
if (doubS < 0 || doubS > 1) {
cout << "It must be between 0 and 1. Try again." << endl ;
continue;
}
break;
}
cout << "CONGRATS ON FOLLOWING DIRECTIONS! YOU ENTERED: " << doubS << endl ;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment