Help! C++ task

Hi, am asking for help!

There is a picture with result what I need:
http://postimage.org/image/d8xpdxb23/

and my code:
Hope you understund what I need, because to me its difficul to explain.. my eng is not good enough.. Just look at picture..

Thanks ;)


#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
float x, n, p;


cout << "Insert a number x!\n";
cin >> x;

cout << "Type an exponent n!\n";
cin >> n;
{
p = pow(x, n);
cout.precision(2);
cout.setf (ios::fixed);


cout << "Degree is: " <<p <<endl;
if (n<0);
cout << "Programm is complited because it was given a negative exponent!" <<endl;
}
if (n<0)
system ("pause");
return 0;
}


Hi,

Just change the type of the variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#include <iostream>
#include <cmath>
using namespace std;


int main ()
{
    int x, n, p;
    cout << "Insert a number x!\n";
    cin >> x;
    cout << "Type an exponent n!\n";
    cin >> n;
    p = pow(x, n);
    cout.precision(2);
    cout.setf (ios::fixed);
    if (n < 0)
        cout << "Programm is complited because it was given a negative exponent!" <<endl;
    else
        cout << "Degree is: " <<p <<endl;
    return 0;
}
Thanks,
but its not work or I didn't get something :D
Yes, the pic. Add a while loop

1
2
3
4
5
 
while (n > 0)
{
    // ... 
}
Wouldn't it be better to validate the exponent as you read it in?

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
#include <iostream>
#include <cmath>

inline void ClearBuffer()
{
   std::cin.clear();
   std::cin.ignore( 256, '\n' );
}

int main( int argc, char* argv[] )
{
   double num, exp;

   do
   {
      std::cout << "Please enter a number: ";
      while( !( std::cin >> num ) )
      {
         std::cout << "Invalid input, enter a number: ";
         ClearBuffer();
      }

      std::cout << "Please enter an exponent (must be positive): ";
      while( !( std::cin >> exp ) || exp < 0 )
      {
         std::cout << "Invalid input, enter an exponent (must be positive): ";
         ClearBuffer();
      }

      std::cout << "Degree: " << std::pow( num, exp ) << std::endl;

   } while( num > 0 );

   return 0;
}
Now its complite.. THANK YOU GUYS :):):)



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
#include <iostream>
#include <cmath>
using namespace std;


int main ()
{
    double x, n, p;
    do
    {
    cout << "Insert a number x!\n";
    cin >> x;
    cout << "Type an exponent n!\n";
    cin >> n;
    p = pow(x, n);
    cout.precision(2);
    cout.setf (ios::fixed);
    if (n < 0)
    cout << "Programm is complited because it was given a negative exponent!" <<endl;
    else
    cout << "Degree is: " <<p <<endl;
    }while (n > 0);
    system ("pause");
    return 0;
}
Topic archived. No new replies allowed.