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.
/*Program to calculate m to power n using function*/
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.
#include <iostream>
#include <cmath>
#include <string>
usingnamespace 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;
}
#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(constchar* 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.