Created
December 1, 2011 20:03
-
-
Save greggroth/1419423 to your computer and use it in GitHub Desktop.
how I do it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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