检索整数变量的值

发布于 2024-10-11 12:18:01 字数 680 浏览 0 评论 0原文

这可能很容易弄清楚,但由于某种原因,我在任何地方都找不到解决方案。也许我没有在寻找正确的东西。也许是在一些我没有看过的初学者教程中。

不管怎样,我想知道如何在 C++ 中检索整数变量的值?我知道您可以使用 cin.getline() 来表示字符串变量,但是当我尝试使用整数变量时,我收到了一条错误消息(理所当然,我知道这是错误的,但我正在寻找以获得解决方案)。

我的项目是一个 Win32 控制台应用程序。我想做的是要求用户输入一个数字,存储在变量n中。然后我获取 n 的值并用它执行各种数学函数。在我的头文件中,我有 stringwindowsiostreamstdiomath > 和fstream。我需要添加另一个库吗?

编辑:

cout << "TEST SINE";
cout << "\nPlease enter a number.\n\n";
cin >> n;
break;

这是我尝试使用的代码。这就是我需要做的全部吗?如果是这样,我如何合并该变量,以便可以使用 sin、cos 和 tan 对其进行测试?

再次提前致谢。

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 variable (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-10-18 12:18:01

这有什么问题吗?

cin>>n;

对于数学函数,float 或 double 是更好的选择。

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;
}

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;
}
勿挽旧人 2024-10-18 12:18:01

如果你想要一个整数,你可以使用:

cin >> n;

但是如果你想要一个健壮的应用程序,你最好控制输入数据。

也许更好的想法是将其作为字符串输入,就像您已经知道如何使用 getline() 一样,然后在调用 等转换函数之前验证该字符串是否包含所有数字字符atoi() 或 strtol()。

这样,您可以获得可靠的输入以及所需的数据类型。

但是,如果您想使用三角函数,那么最好使用双精度数(atof()),而不是整数。


下面是一个帮助您入门的示例程序:

#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;
}

示例运行如下所示:

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).

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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文