### How do I detect if the user enter a number only or number and words?

I got an assignment where it's optional requirements require me to convert x(real number) from degree to radian if the user's input has the word 'degree' appended at the end(example: sin 30 degree will be automatically converted to sin 0.523599). If the word 'degree' is absent from the input, no conversion is required.

How can i do this in C++

#define PI 3.14159265
string check;

If(check.empty())
number = number;
Else if(!check.empty && check == "degree")
number = number*PI/180;

Then using some calculation coding the program will display the answer appropriately (example coding: sin (number);)

But these way doesn't seem to works as intended.
How should i do it?
`<cctype>` provides functions for this sort of thing.
http://www.cplusplus.com/reference/cctype/

Just save the input in a string, then parse it. Use `std::string::find` to check if the word degree is present.
Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include #include #include int main() { std::string fn_name ; double number ; if( std::cin >> fn_name && std::cin >> number ) { // skip characters in the input buffer till a new line // or a non-white-space character is extracted char c ; while( std::cin.get(c) && c != '\n' && std::isspace(c) ) ; if( c != '\n' ) // if it is not a new line { // put the non-white-space character back into the input buffer std::cin.putback(c) ; // read it in as a string std::string deg ; std::cin >> deg ; if( deg == "degrees" ) { // convert number from degrees to radians static constexpr double pi = 3.1415926535897932385 ; std::cout << number << " degrees == " ; number *= pi / 180 ; std::cout << number << " radians\n" ; } } std::cout << fn_name << '(' << number << ") == " ; if( fn_name == "sin" ) std::cout << std::sin(number) << '\n' ; // etc } }``````

http://coliru.stacked-crooked.com/a/ba77e10cb56d623f
Or read the entire input as a single string. Then use a stringstream to parse it.
 ``12345678910111213141516171819`` `````` string input; cout << "Enter your input:\n"; getline(cin, input); string name, units; double value; istringstream ss(input); if (ss >> name >> value) { if (ss >> units && units == "degree") { value = value * PI / 180.0; } if (name == "sin") cout << sin(value) << '\n'; } ``````

(requires `#include <sstream> ` to use stringstream).
 ``1234567891011121314151617`` ``````#include #include #include #include using namespace std; int main() { constexpr double DEGTORAD = 3.14159265359 / 180; string func, rest; double angle; map op = { { "sin", sin }, { "cos", cos }, { "tan", tan } }; cout << "Input trig function and angle: "; cin >> func >> angle; getline( cin, rest ); if ( rest.find( "degree" ) != string::npos ) angle *= DEGTORAD; cout << op[func]( angle ); }``````

 ```Input trig function and angle: sin 30 degree 0.5```

 ```Input trig function and angle: tan 135 degree -1 ```

 ```Input trig function and angle: cos 3.14159265 -1```
Last edited on
What if the user enters something like sin 30 radian degree?
Topic archived. No new replies allowed.