Solved

Needs help in syntax

Posted on 2011-02-23
6
347 Views
Last Modified: 2012-05-11
Hi guys: Can any one please tell me how to raise the power of any number for eg. 2 raise to power 3 = 8

Thanks.

#include <iostream>
using namespace std;

int main()
{
      int number = 0;
        int power1 = 0;
        cout << "Enter Number to raise==>";
        cin >> number;
        cout << "Enter Power==>";
        cin >> power1;
        cout << number^power1;  // syntax error
      system("pause");
      return 0;
}  
0
Comment
Question by:mustish1
  • 3
  • 2
6 Comments
 

Author Comment

by:mustish1
ID: 34966974
I fix the syntax but it still wrong answer

#include <iostream>
using namespace std;

int main()
{
      int number = 0;
        int power1 = 0;
        int result = 0;
        cout << "Enter Number to raise==>";
        cin >> number;
        cout << "Enter Power==>";
        cin >> power1;
        result = number^power1;
        cout << result << endl;
      system("pause");
      return 0;
}  
0
 
LVL 7

Accepted Solution

by:
MrNed earned 500 total points
ID: 34967000
0
 

Author Comment

by:mustish1
ID: 34967012
Why it gives syntax error
  result = pow(number,power1);

-----------------------
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
      int number = 0;
        int power1 = 0;
        int result = 0;
        cout << "Enter Number to raise==>";
        cin >> number;
        cout << "Enter Power==>";
        cin >> power1;
        result = pow(number,power1);
        cout << result << endl;
      system("pause");
      return 0;
}  
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Expert Comment

by:MrNed
ID: 34967026
No idea about a syntax error, but check your types. It returns a float/double and the first parameter might need to be casted to a double. Try this:

result = (int)pow((double)number,power1);
0
 

Author Closing Comment

by:mustish1
ID: 34967036
Thanks.
0
 
LVL 8

Expert Comment

by:crysallus
ID: 34967039
It might not know which version of the function to call as your number variable is an int, which doesn't match any of the pow methods exactly. Try:

result = pow((double)number, power1);

which casts the variable number to a double before calling the function, which specifies that it should call that version of the pow function.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now