Retrieve the Value of An Integer Variable
This is probably easily figured out, but I can't find a solution anywhere, for some reason. Perhaps I'm not searching for the right thing. And maybe it's in some beginner tutorial I haven't watched.
Anyway, I was wondering how to retrieve the value of an integer variable in C++? I know you can use cin.getline()
for string variables, but I received an error message when I attempted that with an integer var开发者_运维问答iable (and rightfully so, I know it was wrong, but I was looking for a solution).
My project is a Win32 console application. What I'm trying to do is ask a user to input a number, stored in the variable n
. Then I take the value of n
and perform various math functions with it. In my header file, I have string
, windows
, iostream
, stdio
, math
, and fstream
. Do I need to add another library?
EDIT:
cout << "TEST SINE";
cout << "\nPlease enter a number.\n\n";
cin >> n;
break;
Here's the code I'm trying to use. Is this all I need to do? If so, how do I incorporate the variable so I can test it using sin, cos, and tan?
Yet again, thanks ahead of time.
what is the problem with this?
cin>>n;
For math functions, float or double would be better option.
int main()
{
double number;
double result;
cout<<"Enter a number:"<<endl;
cin>>number;
result = sin (number); //if you consider number is in radians
//result = sin(number*3.14159265/180.0) //if you consider number is in degrees
cout<<result;
return 0;
}
If you want an integer, you can use:
cin >> n;
but you had better have control of the input data if you want a robust application.
Perhaps a better idea would be to input it as a string as you already know how to do with getline()
, then validate that the string consists of all numeric characters before calling a conversion function like atoi()
or strtol()
.
That way, you get robust input plus the data types you want.
But, if you want to use trigonometric functions, you're probably better off working with doubles, with atof()
, rather than integers.
Here's a sample program to get you started:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
int main (void) {
char s[256];
// Get and check line.
cout << "Enter angle in degrees: ";
cin.getline(s,100);
for (char *ps = s; *ps != 0; ps++) {
if (!isdigit (*ps)) {
cout << "Not numeric" << endl;
return 1;
}
}
// Output string, float, sine and cosine (convert to radians first).
float f = atof (s);
cout << "String : '" << s << "'" << endl;
cout << "Float : " << f << endl;
f = f * 3.141592653589 / 180.0;
cout << "Sine : " << fixed << sin (f) << endl;
cout << "Cosine : " << fixed << cos (f) << endl;
return 0;
}
Sample runs shown below:
Enter angle in degrees: 30
String : '30'
Float : 30
Sine : 0.500000
Cosine : 0.866025
Enter angle in degrees: 45
String : '45'
Float : 45
Sine : 0.707107
Cosine : 0.707107
Enter angle in degrees: 90
String : '90'
Float : 90
Sine : 1.000000
Cosine : -0.000000 (caused by floating point inaccuaracy).
精彩评论