Hello,
Like many people on these boards I am new to C++ programming, and also like many, I have come across a problem for which I'm having difficulty finding the answer.
I've had a check around the boards and other sites and it seems quite a familiar problem - the one whereby a program closes before you get to see the result. Well I'm having a similar problem.
After reading a few tutorials I decided to try to write a program which computes powers of integers. The user enters 'a' and 'b' and the program will return a^b. I thought about using a for loop to do this. Here is what I came up with
Now the program lets the user enter the number and the power but once you hit return to get the result...poof!...the program closes. I have tried solutions such as cin.get() and getch() but to no avail. I would greatly appreciate some help on the matter. Even if you only tell me where I'm going wrong.Code:#include <iostream> #include <conio.h> using namespace std; int main(){ int val; int pow; int res = 1; cout << "Please enter your number: "; cin >> val; cin.ignore(); cout << "Please enter your power: "; cin >> pow; cin.ignore(); for(int i=0; i<pow; i++){ res = res * val; return res; } cout << "The answer is " << res; getch(); return 0; }
Thanks in advance
Stonehambey![]()



LinkBack URL
About LinkBacks




