Hi im working on a function lab that was givin to use, there are compile erros on it im not sure how to fix, most likely caused by mistakes by me not wrinting correctly. If you can help me out on this would be appreciated.

Discription:
The purpose of the lab is to ask the user to choose if they want to Power function or square root function. then have them enter in 1 number if power or 2 numbers if square root. Then call the functions to calc power or square root, display results and ask for new numbers.

#include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    //prototypes for functions for lab10
    float Square_root(int);
    void Base_exp(int, int);
    
    int main()
    {
    
    //local constants
    float Power  = 0;
    int Base_Exp1 = 0;
    char Power_Num = 0;
    char Square_Root_Num  = 0;
    
    //local variables
    int Num1; //Squar root number
    int Num2; //number for base 
    char Letter_code;
    /***********************************************************/
    
    cout << "Enter "P" for Power or "S" for Square Root";
    cin  >> Letter_code;
    
    while (Letter_code = P || Letter_code = S);
    {
          if ( Letter_code = S);
          {
             cout << "Enter a number";
             cin  >> Num1;
             
             //Call function to find Square root
             float Square_root = Square_Root_Num = sqrt(Num1);
             
             //Display Square root
             cout << Square_root;
             }
          else if (Letter_code = P)
          {
               cout << "Enter in two numbers";
               cin >> Num1 >> Num2;
               
               //Call function to find Power number
               void Base_exp (int, int) = Base_Exp1 = pow(Num1, Num2)
               
               //Display Power number
               cout << void Base_exp (int, int)
          }
          else break;
               
         }
    {
    cout << "Enter "P" for Power or "S" for Square Root";
    cin  >> Letter_code;
}
    end while
}
    return 0;

Recommended Answers

All 18 Replies

line 55: should be }, not {

lines 59, 60 and 61: just delete them.

line 25: If you want to display double quotes then you have to escape them, like this: cout << "Enter \"P\" for Power or \"S\" for Square Root"; line 28: put surround P and S with single quotes if( Letter_code == 'P') . Same problem on other if statements

The cmath library already has the power function so you don't have to
create a function. Plus your syntax is horrible.

You got the idea correct but not the logic and the syntax.

This is what you should be doing

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
 float input = 0.0f;
 bool goAgain = true;

 while(goAgain){  
 //ask user for which function to use
  cout <<"1) Power function\n";
  cout <<"2 Sqrt function\n";  
  int choice = 0;
  cin >> choice;

  //ask user to enter a number to either use pow or sqrt
  cout<<"Enter a number : ";
  cin >> input;

  if(choice == 1){
   //print the power of input
  }
  else if(choice == 2){  
   //print the sqrt root of input
  }
 
  cout<<"Try again (0 = no), (1 = yes ) : ";
  cin >> goAgain;  

  }

}

THANKS FOR REPLYS HELPS A LOT!

Ok so i have errors that i dont know how to fix... and im not even sure if it runs right yet either...

#include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    //prototypes for functions for lab10
    float Square_root(int);
    void Base_exp(int, int);
    
    int main()
    {
    
    //local constants
    float Power  = 0;
    int Base_Exp1 = 0;
    int Power_Num = 0;
    double Square_Root_Num  = 0;
    
    //local variables
    int Num1; //Squar root number
    int Num2; //number for base 
    char Letter_code;
    /***********************************************************/
    
    cout << "Enter \"P\" for Power or \"S\" for Square Root";
    cin  >> Letter_code;
    
    while (Letter_code == 'P' || Letter_code == 'S');
    {
          if ( Letter_code == 'S');
          {
             cout << "Enter a number";
             cin  >> Num1;
             
             //Call function to find Square root
             float Square_root = Square_Root_Num = sqrt(Num1);
             
             //Display Square root
             cout << Square_root;
             }
          else if (Letter_code == 'P')
          {
               cout << "Enter in two numbers";
               cin >> Num1 >> Num2;
               
               //Call function to find Power number
               void Base_exp (int, int) = Base_Exp1 = pow(Num1, Num2);
               
               //Display Power number
               cout << void Base_exp (int, int);
          }
          else break;
               
    }
    
}
    cout << "Enter "P" for Power or "S" for Square Root";
    cin  >> Letter_code;
}
    return 0;

ERRORS:

In function `int main()':
42 expected primary-expression before "else"
42 expected `;' before "else"
53 expected primary-expression before "else"
53 expected `;' before "else"
53 At global scope:
58 expected constructor, destructor, or type conversion before '<<' token
58 expected `,' or `;' before '<<' token
59 expected constructor, destructor, or type conversion before '>>' token
59 expected `,' or `;' before '>>' token
60 expected declaration before '}' token


EDIT:
P.S.

This program must use TWO functions

You have mis-aligned closing braces }. Delete lines 54 and 60.

line 57: already told you about that one too.

42 expected primary-expression before "else

remove ";" at line 30.
it must:

if ( Letter_code == 'S')

Thanks for replys, i fixed the errors that were pointed out.

still getting a few more..

#include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    //prototypes for functions for lab10
    float Square_root(int);
    void Base_exp(int, int);
    
    int main()
    {
    
    //local constants
    float Power  = 0;
    int Base_Exp1 = 0;
    int Power_Num = 0;
    double Square_Root_Num  = 0;
    
    //local variables
    int Num1; //Squar root number
    int Num2; //number for base 
    char Letter_code;
    /***********************************************************/
    
    cout << "Enter \"P\" for Power or \"S\" for Square Root";
    cin  >> Letter_code;
    
    while (Letter_code == 'P' || Letter_code == 'S');
    {
          if ( Letter_code == 'S')
          {
             cout << "Enter a number";
             cin  >> Num1;
             
             //Call function to find Square root
             float Square_root = Square_Root_Num = sqrt(Num1);
             
             //Display Square root
             cout << Square_root;
             }
          else if (Letter_code == 'P')
          {
               cout << "Enter in two numbers";
               cin >> Num1 >> Num2;
               
               //Call function to find Power number
               void Base_exp (int, int) = Base_Exp1 = pow(Num1, Num2);
               
               //Display Power number
               cout << void Base_exp (int, int);
          }
          else break;
               
    
    cout << "Enter "P" for Power or "S" for Square Root";
    cin  >> Letter_code;
}
    return 0;

Erros:

In function `int main()':
48 function `void Base_exp(int, int)' is initialized like a variable
48 call of overloaded `pow(int&, int&)' is ambiguous
note C:\Dev-Cpp\include\math.h:150 candidates are: double pow(double, double)
note C:\Dev-Cpp\include\math.h:150 long double std::pow(long double, int)
note C:\Dev-Cpp\include\math.h:150 float std::pow(float, int)
note C:\Dev-Cpp\include\math.h:150 double std::pow(double, int)
note C:\Dev-Cpp\include\math.h:150 long double std::pow(long double, long double)
note C:\Dev-Cpp\include\math.h:150 float std::pow(float, float)
51 expected primary-expression before "void"
51 expected `;' before "void"
53 break statement not within loop or switch
56 `;' before "P"
59 `}' at end of input

line 47: delete void Base_exp (int, int) = because that is illegal syntax.

at line 55:

cout << "Enter  "<<"P"<<" for Power or "<<"S"<< "for Square Root";

at line 52:
remove the "break" because your not in a loop or switch

Thanks for info helpede clear a few lines of error.

Am i using a function correctly?

#include <iostream>    
#include <iomanip>    
#include <cmath>    
using namespace std;     

//prototypes for functions for lab10    
float Square_root(int);   
 void Base_exp(int, int);     
 int main()    
 {     
       //local constants    
       float Power  = 0;   
        int Base_Exp1 = 0;    
        int Power_Num = 0;    
        double Square_Root_Num  = 0;     
        
        //local variables    
        int Num1; //Squar root number    
        int Num2; //number for base     
        char Letter_code;    
        
        /***********************************************************/ 
        
        cout << "Enter \"P\" for Power or \"S\" for Square Root";   
        cin  >> Letter_code;     while (Letter_code == 'P' || Letter_code == 'S');
        {          
        if ( Letter_code == 'S')         
        {            
        cout << "Enter a number";           
        cin  >> Num1;             
                                             
        //Call function to find Square root  
        float Square_root = Square_Root_Num = sqrt(Num1);              
        
        //Display Square root             
        cout << Square_root;             
        }         
         else if (Letter_code == 'P')          
         {               
         cout << "Enter in two numbers";               
         cin >> Num1 >> Num2;                
         
         //Call function to find Power number               
          Base_Exp1 = pow(Num1, Num2);
                          
         //Display Power number               
         cout << void Base_exp (int, int);          
         
         else break;
         }          
               
         
         cout << "Enter  "<<"P"<<" for Power or "<<"S"<< "for Square Root";    
         cin  >> Letter_code;
         }    
         return 0;

Errors:

In function `int main()':
44 call of overloaded `pow(int&, int&)' is ambiguous
note candidates are: double pow(double, double)
note long double std::pow(long double, int)
note float std::pow(float, int)
note double std::pow(double, int)
note long double std::pow(long double, long double)
note float std::pow(float, float)
47 expected primary-expression before "void"
47 expected `;' before "void"
49 expected primary-expression before "else"
49 expected `;' before "else"
56 expected `}' at end of input

1. Why do the prototypes

float Square_root(int);   
 void Base_exp(int, int);

even exist? You don't call them anywhere...

2. The default signature/prototype for pow() is double pow([B]double[/B], int) . You either need to change the type of Num1 to double or cast it to double when you use it.

3. Sort out your braces so that you can actually see your blocks and understand where you are within your structure. For readability, the open and close braces of blocks really should be at the same indent level and nested blocks should be at deeper indent levels. Many of your control flow statements are not in the correct location because you don't know where your blocks are.

4. Study your textbook to find out how to define and call functions. I see multiple attempts at function calls/definitions that aren't even close (lines 33, 44 was, 47).

5. Your output line on line 53 is all wrong. Take another look at it. You know how to fix this already (see line 24).

Ancient Dragon and valtikz have covered the rest, I don't suggest ignoring them.

i took it to school and it compiled, fails to compile on my labtop though, wont worry about it. but when i do compile its not working correctly wonder if anyone could figure were i went wrong.

#include <iostream>    
#include <iomanip>    
#include <cmath> 
#include <cctype>  
using namespace std;     


int main()     
{            

 //local constants 
 const char QUIT        = 'Q';
 const char POWER       = 'P';
 const char SQUARE_ROOT = 'S';

 float Power  = 0;           
 int Base_Exp = 0;            
 int Power_Num = 0;            
 double Square_Root_Num  = 0;              


 //local variables            
 int Base_Num; 
 int Exp_Num;
 int Result;

 //Squar root number            

 char Letter_Code;     //number for base          

 /***********************************************************/          

 cout << "\n\n\n\n\n\n\n";
 cout << setw(61) << "Enter \"P\" for Power or \"S\" for Square Root";  
 cout << "\n\n\n";
 cin  >> Letter_Code; 
    Letter_Code = toupper(Letter_Code);

 while (Letter_Code != QUIT)        
 {                  
  if ( Letter_Code == SQUARE_ROOT)                 
  {                    
   cout << setw(43) << "Enter a number";
   cout << "\n\n\n";
   cin  >> Square_Root_Num; 
   Letter_Code = toupper(Letter_Code);
   Result = sqrt(Square_Root_Num);

  }                  
  else (Letter_Code == POWER)   ;             
  {                        

   cout << setw(61) << "Enter in Base number then Exponate number ";                        
   cin >> Base_Num >> Exp_Num;

   Result = pow(Base_Num, Exp_Num);
  }

  //Call function to find Power number                         



   //Display results                        
   cout << Result;

  //end if         
  cout << "\n\n\n"  ;                   
  cout << setw(61) << "Enter  "<<"P"<<" for Power or "<<"S"<< "for Square Root";
  cin  >> Letter_Code;         
 }             

 return 0;
}

Mind elaborating on "it's not working correctly"? What is it doing wrong? We're not mind readers, we can look at code and have a pretty good idea what a program DOES, not what it's SUPPOSED TO DO, you need to tell us that.

I will say that line 46 is worthless in it's current location, take it out and re-insert it between lines 69 and 70.

Lines 16, 17, 18, I don't see where you use "Power", "Base_Exp", or "Power_Num". If you're not going to use them, get rid of them, less opportunity for a SNAFU.

the question is that when it says to enter p or s for power or square root, entering in either p or s both go to base exponate question. square root is being ignored.

#include <iostream>    
#include <iomanip>    
#include <cmath> 
#include <cctype>  
using namespace std;     

      
int main()     
{            
 
 //local constants 
 const char QUIT        = 'Q';
 const char POWER       = 'P';
 const char SQUARE_ROOT = 'S';

     
 double Square_Root_Num  = 0;              
 

 //local variables            
 double Base_Num; 
 double Exp_Num;
 int Result;
 
 //Squar root number            
    
 char Letter_Code;     //number for base          
 
 /***********************************************************/          
 
 cout << "\n\n\n\n\n\n\n";
 cout << setw(61) << "Enter \"P\" for Power or \"S\" for Square Root";  
 cout << "\n\n\n";
 cin  >> Letter_Code; 
    Letter_Code = toupper(Letter_Code);
 
 while (Letter_Code != QUIT)        
 {                  
  if ( Letter_Code == SQUARE_ROOT)                 
  {                    
   cout << setw(43) << "Enter a number";
   cout << "\n\n\n";
   cin  >> Square_Root_Num; 
   Result = sqrt(Square_Root_Num);

  }                  
  else (Letter_Code == POWER)   ;             
  {                        
   
   cout << setw(51) << "Enter in Base number then Exponate number ";                        
   cin >> Base_Num >> Exp_Num;

   Result = pow(Base_Num, Exp_Num);
  }
 
  //Call function to find Power number                         
             
   

   //Display results                        
   cout << Result;
   
  //end if         
  cout << "\n\n\n"  ;                   
  cout << setw(51) << "Enter  "<<"P"<<" for Power or "<<"S"<< "for Square Root";
  cin  >> Letter_Code;         
 }             
 
 return 0;
}

>> else (Letter_Code == POWER) ;

That's wrong. should be else if (Letter_Code == POWER)

>> else (Letter_Code == POWER) ;

That's wrong. should be else if (Letter_Code == POWER)

Notice that there is NO SEMICOLON. You don't use one directly after any conditional test or loop control (except do...while, but only after the while's conditional test), it will mess up the compiler's interpretation of the program flow.

You will also need to insert a copy of line 35 after line 66 (on line 67). If you don't, a "valid" lower-case input will not get converted and the program will not behave as expected.

Line 65 is still written wrong. I'm not going to tell you how to fix it. You have been told at least 2 times before.

Thank you for all ofyour help lab is completed and handed in on time with a sure passing grade!

Thank you for all ofyour help lab is completed and handed in on time with a sure passing grade!

Good -- did we all get A's :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.