Is there a way to detect ENTER key press at "cin"

Hi,

I am trying to write a program where I have to take 2 numbers as inputs from user. They are say m and n. I want to calculate m to the power n. And if user doesn't enter any input (n) and just presses ENTER key, I want to detect this and call function result = power(m); .

Is there a way to detect the key ENTER when we accept input from cin and perform a specific step? Right now what happens is that when I try to press ENTER key only without giving any input the curser stays on the screen and doesn't move on.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 /*Program to calculate m to power n using function*/

#include <iostream>
#include <cmath>

using namespace std;

double power(double m, int n=2)
{
	return(pow(m,n));
}

int main()
{
	double m, result;
	int n;
	
	double power(double m, int n=2);	//function declaration
	
	cout << "\n----------------------------------------------------------------\n";
	cout << "This program is to calculate 'n'th power of a number 'm'!\n";
	cout << "------------------------------------------------------------------\n";
	cout << endl;
	cout << "Please enter number 'm' : ";
	cin >> m;
	cout << "Please enter power of number 'm' i.e. 'n' : ";
	cin >> n; //here can we detect key ENTER and perform a specific step?
	
	result = power(m,n);	//function call
	cout << "The result of " << n << "th power of " << m << " is : " << result;
	
	return 0;
} 
Just search for empty string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

string input = "";
int n, m;

cout << "Please enter number 'm' : ";
while(true)
{
    cin >> input;
    if (input=="")
    {
        // specific step here
        continue;
    }
    m=atoi(input.c_str());
    break;
}

input = "";
cout << "Please enter power of number 'm' i.e. 'n' : ";
while(true)
{
    cin >> input;
    if (input=="")
    {
        // specific step here
        continue;
    }
    n=atoi(input.c_str());
    break;
}


You should do extra checking with this to make sure that the input was a number... I'll leave that to you as an exercise.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

double power(double m, int n = 2)
{
	return(pow(m, n));
}

int main()
{
	double m, result;
	int n;
	string temp;

	double power(double m, int n = 2);	//function declaration

	cout << "\n----------------------------------------------------------------\n";
	cout << "This program is to calculate 'n'th power of a number 'm'!\n";
	cout << "------------------------------------------------------------------\n";
	cout << endl;
	cout << "Please enter number 'm' : ";
	cin >> m;
	cout << "Please enter power of number 'm' i.e. 'n' : ";
	cin.ignore(numeric_limits<int>::max(), '\n');
	getline(cin, temp);
	if (temp.empty())
		n = 0;//Something
	else
		n = atoi(temp.c_str());//to int
	result = power(m, n);	//function call
	cout << "The result of " << n << "th power of " << m << " is : " << result << endl;

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>

double power(double m, int n)
{
    return std::pow(m, n);
}

template <typename num_type>
num_type get(const char* prompt)
{
    typedef std::istringstream istr;

    std::string input;
    num_type value = {};

    std::cout << prompt;    
    while (std::getline(std::cin, input) && !(istr(input) >> value))
    {
        std::cout << "Invalid input!  Try again!\n" << prompt;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    return value;
}


int main()
{
    using std::cout;
    using std::cin;

    cout << "\n----------------------------------------------------------------\n";
    cout << "This program is to calculate 'n'th power of a number 'm'!\n";
    cout << "------------------------------------------------------------------\n";

    double m = get<decltype(m)>("Please enter number 'm' : ");
    int n = get<decltype(n)>("Please enter power of number 'm' i.e. 'n' : ");

    double result = power(m, n);	//function call
    cout << "The result of " << n << "th power of " << m << " is : " << result;
}


Note that there are still issues with the above code. 9.3 is happily taken for the 'n' variable and silently eats the .3

For a slightly different version of this see:
http://www.cplusplus.com/forum/beginner/108849/#msg592118
which doesn't do exactly what you want as it skips leading whitespace, but does address the "extra input" issue. Perhaps you can combine the two, depending on what you want to actually do.
Thank you for your inputs ValliusDax, mobotus and cire. I got it working with help of your inputs.
Topic archived. No new replies allowed.