0

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.

2
  • use float or double instead of int Commented Jan 11, 2011 at 4:44
  • I just realized that, thank you! I think I'm going to change it to a double. Commented Jan 11, 2011 at 4:50

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

probably nothing, but you should explain that n is initialized with int n;.
I'm using that, but whenever I retrieve a string variable, I have to use cin.getline(). I figured it would be the same way with integers. Perhaps I'm wrong, I'm just a bit confused on this issue (as I said, I feel stupid). I'm going to update the post with my code.
cin.getline() reads until it hits a newline character. cin >> n reads until it sees whitespace. So if the user enters 10 20 30 you would have to do cin >> n three times to retrieve all the input. However, cin.getline() would retrieve the entire string. Along this same train of thought, if you were to define std:string s and then do cin >> s then you would only get "10".
@milkypostman: It doesn't read until whitespace per se, but until the input no longer makes sense for what it's reading or until it gets whitespace. So you can still read the integer 5 if the input was "5asdasd".
Yes, I'm going to change it to double. This was a stupid question, I apologize.
3

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

3 Comments

Whoops, I meant to @ reply to you. You gave me an idea, but it says that the std namespace doesn't have a getline() function.
@Abluescarab: give that one a shot. I've modified it to make it simpler.
Thank you, sorry for the trouble and the late reply!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.